Skip to content

Commit

Permalink
move registries to GregTechAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
DStrand1 committed Sep 28, 2021
1 parent 45e798f commit ba511f0
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 86 deletions.
16 changes: 7 additions & 9 deletions src/main/java/gregtech/GregTechMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import codechicken.lib.CodeChickenLib;
import crafttweaker.CraftTweakerAPI;
import gregtech.api.GTValues;
import gregtech.api.IGTAddon;
import gregtech.api.capability.SimpleCapabilityManager;
import gregtech.api.cover.CoverBehaviorUIFactory;
import gregtech.api.cover.CoverDefinition;
Expand Down Expand Up @@ -43,18 +44,15 @@
import net.minecraftforge.classloading.FMLForgePlugin;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.LoaderException;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.*;
import net.minecraftforge.fml.common.Optional.Method;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

import static gregtech.api.GregTechRegistries.*;
import static gregtech.api.GregTechAPI.*;

@Mod(modid = GTValues.MODID,
name = "GregTech",
Expand Down Expand Up @@ -129,14 +127,13 @@ public void onPreInit(FMLPreInitializationEvent event) {
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 */
/* End CEu MetaTileEntity Registration */
/* Addons not done via an Event due to how much must be initialized for MTEs to register */

MetaEntities.init();

// discover annotated crafting component handlers
// todo do this differently
AnnotatedComponentHandlerLoader.discoverAndLoadAnnotatedComponentHandlers(event.getAsmData());

proxy.onPreLoad();
Expand All @@ -145,6 +142,7 @@ public void onPreInit(FMLPreInitializationEvent event) {

@Mod.EventHandler
public void onInit(FMLInitializationEvent event) {
MTE_REGISTRY.freeze(); // freeze once addon preInit is finished
proxy.onLoad();
if (RecipeMap.isFoundInvalidRecipe()) {
GTLog.logger.fatal("Seems like invalid recipe was found.");
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/gregtech/api/GregTechAPI.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package gregtech.api;

import gregtech.api.block.machines.BlockMachine;
import gregtech.api.cover.CoverDefinition;
import gregtech.api.gui.UIFactory;
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 net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.eventhandler.GenericEvent;

import java.util.HashMap;
import java.util.Map;

public class GregTechAPI {

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 BlockMachine MACHINE;
public static final Map<Material, Map<StoneType, IBlockOre>> oreBlockTable = new HashMap<>();

Expand All @@ -24,4 +35,22 @@ 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 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);
}
}
}
34 changes: 0 additions & 34 deletions src/main/java/gregtech/api/GregTechRegistries.java

This file was deleted.

5 changes: 2 additions & 3 deletions src/main/java/gregtech/api/block/machines/BlockMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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 @@ -235,7 +234,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 = GregTechRegistries.MTE_REGISTRY.getObjectById(stack.getItemDamage());
MetaTileEntity sampleMetaTileEntity = GregTechAPI.MTE_REGISTRY.getObjectById(stack.getItemDamage());
if (holder != null && sampleMetaTileEntity != null) {
MetaTileEntity metaTileEntity = holder.setMetaTileEntity(sampleMetaTileEntity);
if (stack.hasTagCompound()) {
Expand Down Expand Up @@ -450,7 +449,7 @@ public int getLightOpacity(@Nonnull IBlockState state, @Nonnull IBlockAccess wor

@Override
public void getSubBlocks(@Nonnull CreativeTabs tab, @Nonnull NonNullList<ItemStack> items) {
for (MetaTileEntity metaTileEntity : GregTechRegistries.MTE_REGISTRY) {
for (MetaTileEntity metaTileEntity : GregTechAPI.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.GregTechRegistries;
import gregtech.api.GregTechAPI;
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 GregTechRegistries.MTE_REGISTRY.getObjectById(itemStack.getItemDamage());
return GregTechAPI.MTE_REGISTRY.getObjectById(itemStack.getItemDamage());
}

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

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

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

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/gregtech/api/cover/CoverDefinition.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gregtech.api.cover;

import gregtech.api.GregTechRegistries;
import gregtech.api.GregTechAPI;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
Expand All @@ -10,15 +10,15 @@
public final class CoverDefinition {

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

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

public static int getNetworkIdForCover(CoverDefinition definition) {
return GregTechRegistries.COVER_REGISTRY.getIDForObject(definition);
return GregTechAPI.COVER_REGISTRY.getIDForObject(definition);
}

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

import gregtech.api.GregTechRegistries;
import gregtech.api.GregTechAPI;
import gregtech.api.gui.impl.ModularUIContainer;
import gregtech.api.gui.impl.ModularUIGui;
import gregtech.api.net.NetworkHandler;
Expand All @@ -22,7 +22,7 @@
import java.util.List;

/**
* Implement and register on the {@link GregTechRegistries.RegisterEvent<UIFactory>} event to be able to create and open ModularUI's
* Implement and register on the {@link GregTechAPI.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
Expand All @@ -42,7 +42,7 @@ public final void openUI(E holder, EntityPlayerMP player) {

PacketBuffer serializedHolder = new PacketBuffer(Unpooled.buffer());
writeHolderToSyncData(serializedHolder, holder);
int uiFactoryId = GregTechRegistries.UI_FACTORY_REGISTRY.getIDForObject(this);
int uiFactoryId = GregTechAPI.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,7 +1,7 @@
package gregtech.api.items.gui;

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

public void init() {
GregTechRegistries.UI_FACTORY_REGISTRY.register(1, new ResourceLocation(GTValues.MODID, "player_inventory_factory"), this);
GregTechAPI.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,7 +10,6 @@
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 @@ -649,7 +648,7 @@ protected boolean shouldUpdate(MTETrait trait) {
}

public final ItemStack getStackForm(int amount) {
int metaTileEntityIntId = GregTechRegistries.MTE_REGISTRY.getIdByObjectName(metaTileEntityId);
int metaTileEntityIntId = GregTechAPI.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.GregTechRegistries;
import gregtech.api.GregTechAPI;
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(GregTechRegistries.MTE_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
buffer.writeVarInt(GregTechAPI.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 = GregTechRegistries.MTE_REGISTRY.getObject(metaTileEntityId);
MetaTileEntity sampleMetaTileEntity = GregTechAPI.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(GregTechRegistries.MTE_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
buf.writeVarInt(GregTechAPI.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(GregTechRegistries.MTE_REGISTRY.getObjectById(metaTileEntityId));
setMetaTileEntity(GregTechAPI.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(GregTechRegistries.MTE_REGISTRY.getObjectById(metaTileEntityId));
setMetaTileEntity(GregTechAPI.MTE_REGISTRY.getObjectById(metaTileEntityId));
this.metaTileEntity.receiveInitialSyncData(buffer);
scheduleChunkForRenderUpdate();
this.needToUpdateLightning = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gregtech.api.metatileentity;

import gregtech.api.GTValues;
import gregtech.api.GregTechRegistries;
import gregtech.api.GregTechAPI;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.UIFactory;
import net.minecraft.client.Minecraft;
Expand All @@ -22,7 +22,7 @@ private MetaTileEntityUIFactory() {
}

public void init() {
GregTechRegistries.UI_FACTORY_REGISTRY.register(0, new ResourceLocation(GTValues.MODID, "meta_tile_entity_factory"), this);
GregTechAPI.UI_FACTORY_REGISTRY.register(0, new ResourceLocation(GTValues.MODID, "meta_tile_entity_factory"), this);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gregtech/api/net/NetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import codechicken.lib.vec.Vector3;
import gregtech.api.GTValues;
import gregtech.api.GregTechRegistries;
import gregtech.api.GregTechAPI;
import gregtech.api.block.ICustomParticleBlock;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.UIFactory;
Expand Down Expand Up @@ -247,7 +247,7 @@ public static void init() {
@SideOnly(Side.CLIENT)
private static void initClient() {
registerClientExecutor(PacketUIOpen.class, (packet, handler) -> {
UIFactory<?> uiFactory = GregTechRegistries.UI_FACTORY_REGISTRY.getObjectById(packet.uiFactoryId);
UIFactory<?> uiFactory = GregTechAPI.UI_FACTORY_REGISTRY.getObjectById(packet.uiFactoryId);
if (uiFactory == null) {
GTLog.logger.warn("Couldn't find UI Factory with id '{}'", packet.uiFactoryId);
} else {
Expand Down
Loading

0 comments on commit ba511f0

Please sign in to comment.