Browse Source

Moved Aether DimensionType registration to AetherDimensions.java

pull/3/head
Raptor 6 months ago
parent
commit
810d59f101
3 changed files with 77 additions and 40 deletions
  1. +0
    -0
      .classpath-old
  2. +3
    -31
      src/main/java/com/aether/Aether.java
  3. +74
    -9
      src/main/java/com/aether/world/AetherDimensions.java

+ 0
- 0
.classpath-old View File


+ 3
- 31
src/main/java/com/aether/Aether.java View File

@@ -2,29 +2,19 @@ package com.aether;

import com.aether.biome.AetherBiomes;
import com.aether.client.ClientProxy;

import com.aether.world.AetherDimensions;

import net.minecraft.item.Rarity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.ModDimension;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.world.RegisterDimensionsEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.ObjectHolder;

@Mod(Aether.MODID)
public class Aether {

public static final String MODID = "aether";

public static DimensionType aether_dimension;
private static CommonProxy proxy = DistExecutor.runForDist(() -> ClientProxy::new, () -> CommonProxy::new);;
@@ -32,28 +22,10 @@ public class Aether {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
modEventBus.addListener(proxy::commonSetup);
modEventBus.addListener(proxy::clientSetup);
AetherDimensions.MOD_DIMENSIONS.register(modEventBus);
AetherBiomes.BIOMES.register(modEventBus);
AetherDimensions.DIMENSIONS.register(modEventBus);
AetherBiomes.BIOMES.register(modEventBus);
}
public static final Rarity AETHER_LOOT = Rarity.create("AETHER_LOOT", TextFormatting.GREEN);

@Mod.EventBusSubscriber(modid = MODID)
public static class ForgeEventBus {

@SubscribeEvent
public static void registerModDimension(final RegisterDimensionsEvent event) {
ResourceLocation aetherLoc = new ResourceLocation(MODID, "aether");
if(DimensionType.byName(aetherLoc) == null) {
aether_dimension = DimensionManager.registerDimension(aetherLoc, AetherDimensions.AETHER.get(), null, true);
DimensionManager.keepLoaded(aether_dimension, false);
}
else {
aether_dimension = DimensionType.byName(aetherLoc);
}

}

}
}

+ 74
- 9
src/main/java/com/aether/world/AetherDimensions.java View File

@@ -1,24 +1,89 @@
package com.aether.world;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.function.BiFunction;

import com.aether.Aether;

import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraft.world.dimension.Dimension;
import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.ModDimension;
import net.minecraftforge.event.world.RegisterDimensionsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.function.BiFunction;

public class AetherDimensions {

public static final DeferredRegister<ModDimension> MOD_DIMENSIONS = new DeferredRegister<>(ForgeRegistries.MOD_DIMENSIONS, Aether.MODID);
public static final DeferredRegister<ModDimension> DIMENSIONS = new DeferredRegister<>(ForgeRegistries.MOD_DIMENSIONS, Aether.MODID);

private static final RegistryObject<ModDimension> AETHER_DIMENSION_REGISTRY_OBJECT = DIMENSIONS.register("aether",
() -> new ModDimension() {
@Override
public BiFunction<World, DimensionType, ? extends Dimension> getFactory() {
return AetherDimension::new;
}
}
);

public static final DimensionType AETHER = null;

@EventBusSubscriber(modid = Aether.MODID)
public static final class Registration {

private static final ResourceLocation AETHER_ID = new ResourceLocation(Aether.MODID, "aether");

@SubscribeEvent
public static void registerDimensions(RegisterDimensionsEvent event) {
DimensionType dimensiontype = DimensionType.byName(AETHER_ID);
if (dimensiontype == null) {
dimensiontype = DimensionManager.registerDimension(AETHER_ID, AETHER_DIMENSION_REGISTRY_OBJECT.get(),
null, true);
DimensionManager.keepLoaded(dimensiontype, false);
}
// Use reflection to set the final field AetherDimensions.AETHER = dimensiontype
try {
field_AetherDimensions__AETHER.set(null, dimensiontype);
}
catch (IllegalArgumentException | IllegalAccessException e) {
throw new AssertionError(e);
}
}

private static final Field field_AetherDimensions__AETHER;

static {
Field field_AETHER = null;

try {
// Get the Field instance for the final field Field.modifiers
Field field_modifiers = Field.class.getDeclaredField("modifiers");
// Change it to public
field_modifiers.setAccessible(true);
// Get the Field instance for the final field AetherDimensions.AETHER
field_AETHER = AetherDimensions.class.getDeclaredField("AETHER");
// Change it to public
field_AETHER.setAccessible(true);
// Get its current modifiers bit field
int mods = field_AETHER.getModifiers();
// Use reflection to set the final field Field.modifiers = mods & ~Modifiers.FINAL, which
// results in unsetting the FINAL flag for the AetherDimensions.AETHER field instance.
// This will let us use .set() with the object.
field_modifiers.setInt(field_AETHER, mods & ~Modifier.FINAL);
}
catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
throw new AssertionError(e);
}

field_AetherDimensions__AETHER = field_AETHER;
}

}

public static final RegistryObject<ModDimension> AETHER = MOD_DIMENSIONS.register("aether", () -> new ModDimension() {
@Override
public BiFunction<World, DimensionType, ? extends Dimension> getFactory() {
return AetherDimension::new;
}
});
}

Loading…
Cancel
Save