Skip to content

Commit

Permalink
reorganize registries
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Sep 17, 2021
1 parent 5ba4f44 commit 909186a
Show file tree
Hide file tree
Showing 23 changed files with 327 additions and 247 deletions.
36 changes: 35 additions & 1 deletion src/main/java/gregtech/GregTechMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import gregtech.api.GTValues;
import gregtech.api.capability.SimpleCapabilityManager;
import gregtech.api.cover.CoverBehaviorUIFactory;
import gregtech.api.cover.CoverDefinition;
import gregtech.api.gui.UIFactory;
import gregtech.api.items.gui.PlayerInventoryUIFactory;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntityUIFactory;
import gregtech.api.model.ResourcePackHook;
import gregtech.api.net.NetworkHandler;
import gregtech.api.recipes.RecipeMap;
import gregtech.api.terminal.util.GuideJsonLoader;
import gregtech.api.unification.OreDictUnifier;
import gregtech.api.unification.material.IMaterialHandler;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.MaterialRegistry;
import gregtech.api.unification.material.Materials;
import gregtech.api.util.AnnotatedMaterialHandlerLoader;
Expand Down Expand Up @@ -52,6 +56,8 @@
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

import static gregtech.api.GregTechRegistries.*;

@Mod(modid = GTValues.MODID,
name = "GregTech",
acceptedMinecraftVersions = "[1.12,1.13)",
Expand All @@ -78,14 +84,25 @@ public class GregTechMod {
@Mod.EventHandler
public void onPreInit(FMLPreInitializationEvent event) {
NetworkHandler.init();

GTLog.init(event.getModLog());

/* Start UI Factory Registration */
UI_FACTORY_REGISTRY.unfreeze();
GTLog.logger.info("Registering GTCEu UI Factories");
MetaTileEntityUIFactory.INSTANCE.init();
PlayerInventoryUIFactory.INSTANCE.init();
CoverBehaviorUIFactory.INSTANCE.init();
GTLog.logger.info("Registering addon UI Factories");
MinecraftForge.EVENT_BUS.post(new RegisterEvent<>(UI_FACTORY_REGISTRY, UIFactory.class));
UI_FACTORY_REGISTRY.freeze();
/* End UI Factory Registration */

SimpleCapabilityManager.init();

// TODO Update this to new registration method
/* Start Material Registration */
//first, register primary materials and run material handlers
MaterialRegistry.MATERIAL_REGISTRY.unfreeze();
Materials.register();
AnnotatedMaterialHandlerLoader.discoverAndLoadAnnotatedMaterialHandlers(event.getAsmData());
IMaterialHandler.runMaterialHandlers();
Expand All @@ -102,14 +119,24 @@ public void onPreInit(FMLPreInitializationEvent event) {

//freeze material registry before processing items, blocks and fluids
MaterialRegistry.finalizeMaterials(true);
/* End Material Registration */

OreDictUnifier.init();
NBTUtil.registerSerializers();

MetaBlocks.init();
MetaItems.init();
MetaFluids.init();

/* Start MetaTileEntity Registration */
MTE_REGISTRY.unfreeze();
GTLog.logger.info("Registering GTCEu Meta Tile Entities");
MetaTileEntities.init();
GTLog.logger.info("Registering addon Meta Tile Entities");
MinecraftForge.EVENT_BUS.post(new RegisterEvent<>(MTE_REGISTRY, MetaTileEntity.class));
MTE_REGISTRY.freeze();
/* End MetaTileEntity Registration */

MetaEntities.init();

// discover annotated crafting component handlers
Expand Down Expand Up @@ -150,7 +177,14 @@ public void onInit(FMLInitializationEvent event) {

LootTableHelper.initialize();
FilterTypeRegistry.init();

/* Start Cover Definition Registration */
COVER_REGISTRY.unfreeze();
CoverBehaviors.init();
MinecraftForge.EVENT_BUS.post(new RegisterEvent<>(COVER_REGISTRY, CoverDefinition.class));
COVER_REGISTRY.freeze();
/* End Cover Definition Registration */

DungeonLootLoader.init();
}

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/gregtech/api/GregTechAPI.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package gregtech.api;

import gregtech.api.block.machines.BlockMachine;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.unification.OreDictUnifier;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.Materials;
import gregtech.api.unification.ore.OrePrefix;
import gregtech.api.unification.ore.StoneType;
import gregtech.api.util.BaseCreativeTab;
import gregtech.api.util.GTControlledRegistry;
import gregtech.api.util.IBlockOre;
import gregtech.common.items.MetaItems;
import net.minecraft.util.ResourceLocation;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -27,11 +24,4 @@ public class GregTechAPI {
new BaseCreativeTab(GTValues.MODID + ".materials", () -> OreDictUnifier.get(OrePrefix.ingot, Materials.Aluminium), true);
public static final BaseCreativeTab TAB_GREGTECH_ORES =
new BaseCreativeTab(GTValues.MODID + ".ores", () -> MetaItems.DRILL_MV.getStackForm(), true);

public static final GTControlledRegistry<ResourceLocation, MetaTileEntity> META_TILE_ENTITY_REGISTRY = new GTControlledRegistry<>(Short.MAX_VALUE);

public static <T extends MetaTileEntity> T registerMetaTileEntity(int id, T sampleMetaTileEntity) {
META_TILE_ENTITY_REGISTRY.register(id, sampleMetaTileEntity.metaTileEntityId, sampleMetaTileEntity);
return sampleMetaTileEntity;
}
}
34 changes: 34 additions & 0 deletions src/main/java/gregtech/api/GregTechRegistries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gregtech.api;

import gregtech.api.cover.CoverDefinition;
import gregtech.api.gui.UIFactory;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.util.GTControlledRegistry;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.eventhandler.GenericEvent;

public class GregTechRegistries {

public static final GTControlledRegistry<ResourceLocation, MetaTileEntity> MTE_REGISTRY = new GTControlledRegistry<>(Short.MAX_VALUE);
public static final GTControlledRegistry<ResourceLocation, UIFactory> UI_FACTORY_REGISTRY = new GTControlledRegistry<>(Short.MAX_VALUE);
public static final GTControlledRegistry<ResourceLocation, CoverDefinition> COVER_REGISTRY = new GTControlledRegistry<>(Integer.MAX_VALUE);

public static class RegisterEvent<V> extends GenericEvent<V> {

private final GTControlledRegistry<ResourceLocation, V> registry;

public RegisterEvent(GTControlledRegistry<ResourceLocation, V> registry, Class<V> clazz) {
super(clazz);
this.registry = registry;
}

public void register(int id, ResourceLocation key, V value) {
registry.register(id, key, value);
}

public void register(int id, String key, V value) {
registry.register(id, new ResourceLocation(Loader.instance().activeModContainer().getModId(), key), value);
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/gregtech/api/block/machines/BlockMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import codechicken.lib.vec.Cuboid6;
import com.google.common.collect.Lists;
import gregtech.api.GregTechAPI;
import gregtech.api.GregTechRegistries;
import gregtech.api.block.BlockCustomParticle;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.tool.IScrewdriverItem;
Expand Down Expand Up @@ -234,7 +235,7 @@ public boolean recolorBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnul
@Override
public void onBlockPlacedBy(World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull EntityLivingBase placer, ItemStack stack) {
MetaTileEntityHolder holder = (MetaTileEntityHolder) worldIn.getTileEntity(pos);
MetaTileEntity sampleMetaTileEntity = GregTechAPI.META_TILE_ENTITY_REGISTRY.getObjectById(stack.getItemDamage());
MetaTileEntity sampleMetaTileEntity = GregTechRegistries.MTE_REGISTRY.getObjectById(stack.getItemDamage());
if (holder != null && sampleMetaTileEntity != null) {
MetaTileEntity metaTileEntity = holder.setMetaTileEntity(sampleMetaTileEntity);
if (stack.hasTagCompound()) {
Expand Down Expand Up @@ -449,7 +450,7 @@ public int getLightOpacity(@Nonnull IBlockState state, @Nonnull IBlockAccess wor

@Override
public void getSubBlocks(@Nonnull CreativeTabs tab, @Nonnull NonNullList<ItemStack> items) {
for (MetaTileEntity metaTileEntity : GregTechAPI.META_TILE_ENTITY_REGISTRY) {
for (MetaTileEntity metaTileEntity : GregTechRegistries.MTE_REGISTRY) {
metaTileEntity.getSubItems(tab, items);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gregtech.api.block.machines;

import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
import gregtech.api.GregTechRegistries;
import gregtech.api.metatileentity.ITieredMetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntity;
import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -32,7 +32,7 @@ public MachineItemBlock(BlockMachine block) {
}

public static MetaTileEntity getMetaTileEntity(ItemStack itemStack) {
return GregTechAPI.META_TILE_ENTITY_REGISTRY.getObjectById(itemStack.getItemDamage());
return GregTechRegistries.MTE_REGISTRY.getObjectById(itemStack.getItemDamage());
}

@Nonnull
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gregtech/api/cover/CoverBehaviorUIFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.cover;

import gregtech.api.GTValues;
import gregtech.api.GregTechRegistries;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.UIFactory;
Expand All @@ -20,7 +21,7 @@ private CoverBehaviorUIFactory() {
}

public void init() {
UIFactory.FACTORY_REGISTRY.register(2, new ResourceLocation(GTValues.MODID, "cover_behavior_factory"), this);
GregTechRegistries.UI_FACTORY_REGISTRY.register(2, new ResourceLocation(GTValues.MODID, "cover_behavior_factory"), this);
}

@Override
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/gregtech/api/cover/CoverDefinition.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
package gregtech.api.cover;

import gregtech.api.util.GTControlledRegistry;
import gregtech.api.GregTechRegistries;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.IForgeRegistryEntry;

import java.util.function.BiFunction;

public final class CoverDefinition extends IForgeRegistryEntry.Impl {

public static final GTControlledRegistry<ResourceLocation, CoverDefinition> registry = new GTControlledRegistry<>(Integer.MAX_VALUE);
public final class CoverDefinition {

public static CoverDefinition getCoverById(ResourceLocation id) {
return registry.getObject(id);
return GregTechRegistries.COVER_REGISTRY.getObject(id);
}

public static CoverDefinition getCoverByNetworkId(int networkId) {
return registry.getObjectById(networkId);
return GregTechRegistries.COVER_REGISTRY.getObjectById(networkId);
}

public static int getNetworkIdForCover(CoverDefinition definition) {
return registry.getIDForObject(definition);
}

public static void registerCover(int id, CoverDefinition coverDefinition) {
registry.register(id, coverDefinition.coverId, coverDefinition);
return GregTechRegistries.COVER_REGISTRY.getIDForObject(definition);
}

private final ResourceLocation coverId;
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/gregtech/api/gui/UIFactory.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package gregtech.api.gui;

import gregtech.api.GregTechRegistries;
import gregtech.api.gui.impl.ModularUIContainer;
import gregtech.api.gui.impl.ModularUIGui;
import gregtech.api.net.NetworkHandler;
import gregtech.api.net.PacketUIOpen;
import gregtech.api.net.PacketUIWidgetUpdate;
import gregtech.api.util.GTControlledRegistry;
import io.netty.buffer.Unpooled;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
Expand All @@ -23,15 +22,13 @@
import java.util.List;

/**
* Implement and register to {@link #FACTORY_REGISTRY} to be able to create and open ModularUI's
* Implement and register on the {@link GregTechRegistries.RegisterEvent<UIFactory>} event to be able to create and open ModularUI's
* createUITemplate should return equal gui both on server and client side, or sync will break!
*
* @param <E> UI holder type
*/
public abstract class UIFactory<E extends IUIHolder> {

public static final GTControlledRegistry<ResourceLocation, UIFactory<?>> FACTORY_REGISTRY = new GTControlledRegistry<>(Short.MAX_VALUE);

public final void openUI(E holder, EntityPlayerMP player) {
if (player instanceof FakePlayer) {
return;
Expand All @@ -45,7 +42,7 @@ public final void openUI(E holder, EntityPlayerMP player) {

PacketBuffer serializedHolder = new PacketBuffer(Unpooled.buffer());
writeHolderToSyncData(serializedHolder, holder);
int uiFactoryId = FACTORY_REGISTRY.getIDForObject(this);
int uiFactoryId = GregTechRegistries.UI_FACTORY_REGISTRY.getIDForObject(this);

ModularUIContainer container = new ModularUIContainer(uiTemplate);
container.windowId = currentWindowId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.items.gui;

import gregtech.api.GTValues;
import gregtech.api.GregTechRegistries;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.UIFactory;
import gregtech.api.items.metaitem.MetaItem;
Expand All @@ -26,7 +27,7 @@ private PlayerInventoryUIFactory() {
}

public void init() {
UIFactory.FACTORY_REGISTRY.register(1, new ResourceLocation(GTValues.MODID, "player_inventory_factory"), this);
GregTechRegistries.UI_FACTORY_REGISTRY.register(1, new ResourceLocation(GTValues.MODID, "player_inventory_factory"), this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import codechicken.lib.vec.Matrix4;
import com.google.common.base.Preconditions;
import gregtech.api.GregTechAPI;
import gregtech.api.GregTechRegistries;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.impl.FluidHandlerProxy;
Expand Down Expand Up @@ -644,7 +645,7 @@ protected boolean shouldUpdate(MTETrait trait) {
}

public final ItemStack getStackForm(int amount) {
int metaTileEntityIntId = GregTechAPI.META_TILE_ENTITY_REGISTRY.getIdByObjectName(metaTileEntityId);
int metaTileEntityIntId = GregTechRegistries.MTE_REGISTRY.getIdByObjectName(metaTileEntityId);
return new ItemStack(GregTechAPI.MACHINE, amount, metaTileEntityIntId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gregtech.api.metatileentity;

import com.google.common.base.Preconditions;
import gregtech.api.GregTechAPI;
import gregtech.api.GregTechRegistries;
import gregtech.api.block.machines.BlockMachine;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.gui.IUIHolder;
Expand Down Expand Up @@ -48,7 +48,7 @@ public MetaTileEntity setMetaTileEntity(MetaTileEntity sampleMetaTileEntity, Obj
if (hasWorld() && !getWorld().isRemote) {
updateBlockOpacity();
writeCustomData(-1, buffer -> {
buffer.writeVarInt(GregTechAPI.META_TILE_ENTITY_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
buffer.writeVarInt(GregTechRegistries.MTE_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
metaTileEntity.writeInitialSyncData(buffer);
});
//just to update neighbours so cables and other things will work properly
Expand Down Expand Up @@ -84,7 +84,7 @@ public void readFromNBT(@Nonnull NBTTagCompound compound) {
if (compound.hasKey("MetaId", NBT.TAG_STRING)) {
String metaTileEntityIdRaw = compound.getString("MetaId");
ResourceLocation metaTileEntityId = new ResourceLocation(metaTileEntityIdRaw);
MetaTileEntity sampleMetaTileEntity = GregTechAPI.META_TILE_ENTITY_REGISTRY.getObject(metaTileEntityId);
MetaTileEntity sampleMetaTileEntity = GregTechRegistries.MTE_REGISTRY.getObject(metaTileEntityId);
NBTTagCompound metaTileEntityData = compound.getCompoundTag("MetaTileEntity");
if (sampleMetaTileEntity != null) {
this.metaTileEntity = sampleMetaTileEntity.createMetaTileEntity(this);
Expand Down Expand Up @@ -140,7 +140,7 @@ public void update() {
public void writeInitialSyncData(PacketBuffer buf) {
if (metaTileEntity != null) {
buf.writeBoolean(true);
buf.writeVarInt(GregTechAPI.META_TILE_ENTITY_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
buf.writeVarInt(GregTechRegistries.MTE_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
metaTileEntity.writeInitialSyncData(buf);
} else buf.writeBoolean(false);
}
Expand All @@ -149,7 +149,7 @@ public void writeInitialSyncData(PacketBuffer buf) {
public void receiveInitialSyncData(PacketBuffer buf) {
if (buf.readBoolean()) {
int metaTileEntityId = buf.readVarInt();
setMetaTileEntity(GregTechAPI.META_TILE_ENTITY_REGISTRY.getObjectById(metaTileEntityId));
setMetaTileEntity(GregTechRegistries.MTE_REGISTRY.getObjectById(metaTileEntityId));
this.metaTileEntity.receiveInitialSyncData(buf);
scheduleChunkForRenderUpdate();
this.needToUpdateLightning = true;
Expand All @@ -160,7 +160,7 @@ public void receiveInitialSyncData(PacketBuffer buf) {
public void receiveCustomData(int discriminator, PacketBuffer buffer) {
if (discriminator == -1) {
int metaTileEntityId = buffer.readVarInt();
setMetaTileEntity(GregTechAPI.META_TILE_ENTITY_REGISTRY.getObjectById(metaTileEntityId));
setMetaTileEntity(GregTechRegistries.MTE_REGISTRY.getObjectById(metaTileEntityId));
this.metaTileEntity.receiveInitialSyncData(buffer);
scheduleChunkForRenderUpdate();
this.needToUpdateLightning = true;
Expand Down
Loading

0 comments on commit 909186a

Please sign in to comment.