Skip to content

Commit

Permalink
Namespaced MTE Registries (GregTechCEu#2505)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 authored Jul 4, 2024
1 parent 375af9e commit e79ad8f
Show file tree
Hide file tree
Showing 78 changed files with 1,237 additions and 184 deletions.
11 changes: 6 additions & 5 deletions src/main/java/gregtech/api/GregTechAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import gregtech.api.advancement.IAdvancementManager;
import gregtech.api.block.ICleanroomFilter;
import gregtech.api.block.IHeatingCoilBlockStats;
import gregtech.api.block.machines.BlockMachine;
import gregtech.api.command.ICommandManager;
import gregtech.api.cover.CoverDefinition;
import gregtech.api.event.HighTierEvent;
import gregtech.api.gui.UIFactory;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.multiblock.IBatteryData;
import gregtech.api.metatileentity.registry.MTEManager;
import gregtech.api.modules.IModuleManager;
import gregtech.api.network.INetworkHandler;
import gregtech.api.sound.ISoundManager;
Expand All @@ -21,6 +20,7 @@
import gregtech.api.util.GTLog;
import gregtech.api.util.IBlockOre;
import gregtech.common.ConfigHolder;
import gregtech.datafix.migration.lib.MigrationAPI;

import net.minecraft.block.state.IBlockState;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -52,20 +52,21 @@ public class GregTechAPI {
public static IMaterialRegistryManager materialManager;
/** Will be available at the Pre-Initialization stage */
public static MarkerMaterialRegistry markerMaterialRegistry;
/** Will be available at the Pre-Initialization stage */
public static MTEManager mteManager;
/** GT's data migrations API */
public static final MigrationAPI MIGRATIONS = new MigrationAPI();

/** Will be available at the Pre-Initialization stage */
private static boolean highTier;
private static boolean highTierInitialized;

public static final GTControlledRegistry<ResourceLocation, MetaTileEntity> MTE_REGISTRY = new GTControlledRegistry<>(
Short.MAX_VALUE);
@Deprecated
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<>();
public static final Object2ObjectMap<IBlockState, IHeatingCoilBlockStats> HEATING_COILS = new Object2ObjectOpenHashMap<>();
public static final Object2ObjectMap<IBlockState, IBatteryData> PSS_BATTERIES = new Object2ObjectOpenHashMap<>();
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/gregtech/api/block/machines/BlockMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import gregtech.api.metatileentity.MetaTileEntityHolder;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.multiblock.MultiblockControllerBase;
import gregtech.api.metatileentity.registry.MTERegistry;
import gregtech.api.pipenet.IBlockAppearance;
import gregtech.api.util.GTUtility;
import gregtech.api.util.Mods;
Expand Down Expand Up @@ -72,6 +73,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.Set;

Expand Down Expand Up @@ -256,7 +258,10 @@ public boolean recolorBlock(@NotNull World world, @NotNull BlockPos pos, @NotNul
public void onBlockPlacedBy(World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state,
@NotNull EntityLivingBase placer, ItemStack stack) {
IGregTechTileEntity holder = (IGregTechTileEntity) worldIn.getTileEntity(pos);
MetaTileEntity sampleMetaTileEntity = GregTechAPI.MTE_REGISTRY.getObjectById(stack.getItemDamage());
MTERegistry registry = GregTechAPI.mteManager.getRegistry(
Objects.requireNonNull(stack.getItem().getRegistryName()).getNamespace());

MetaTileEntity sampleMetaTileEntity = registry.getObjectById(stack.getItemDamage());
if (holder != null && sampleMetaTileEntity != null) {
// TODO Fix this
if (stack.hasDisplayName() && holder instanceof MetaTileEntityHolder) {
Expand Down Expand Up @@ -506,7 +511,10 @@ public int getLightOpacity(@NotNull IBlockState state, @NotNull IBlockAccess wor

@Override
public void getSubBlocks(@NotNull CreativeTabs tab, @NotNull NonNullList<ItemStack> items) {
for (MetaTileEntity metaTileEntity : GregTechAPI.MTE_REGISTRY) {
MTERegistry registry = GregTechAPI.mteManager
.getRegistry(Objects.requireNonNull(getRegistryName()).getNamespace());

for (MetaTileEntity metaTileEntity : registry) {
if (metaTileEntity.isInCreativeTab(tab)) {
metaTileEntity.getSubItems(tab, items);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gregtech.api.block.machines;

import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
import gregtech.api.metatileentity.ITieredMetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.pipenet.block.BlockPipe;
Expand Down Expand Up @@ -48,7 +47,7 @@ public class MachineItemBlock extends ItemBlock {

/**
* Adds another creative tab for the machine item. Additional tabs added by this method are checked along with
* default tabs ({@link GregTechAPI#MACHINE} and {@link CreativeTabs#SEARCH}) during
* default tabs ({@link GTCreativeTabs#TAB_GREGTECH_MACHINES} and {@link CreativeTabs#SEARCH}) during
* {@link net.minecraft.item.Item#getSubItems(CreativeTabs, NonNullList) Item#getSubItems()} operation.<br>
* Note that, for machines to be properly registered on the creative tab, a matching implementation of
* {@link MetaTileEntity#isInCreativeTab(CreativeTabs)} should be provided as well.
Expand Down Expand Up @@ -203,4 +202,9 @@ public int getItemStackLimit(@NotNull ItemStack stack) {
MetaTileEntity metaTileEntity = GTUtility.getMetaTileEntity(stack);
return metaTileEntity != null ? metaTileEntity.getItemStackLimit(stack) : super.getItemStackLimit(stack);
}

@Override
public @NotNull BlockMachine getBlock() {
return (BlockMachine) super.getBlock();
}
}
20 changes: 16 additions & 4 deletions src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import gregtech.api.items.toolitem.ToolHelper;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.interfaces.ISyncedTileEntity;
import gregtech.api.metatileentity.registry.MTERegistry;
import gregtech.api.mui.GTGuiTheme;
import gregtech.api.mui.GregTechGuiScreen;
import gregtech.api.mui.factory.MetaTileEntityGuiFactory;
Expand Down Expand Up @@ -120,6 +121,8 @@ public abstract class MetaTileEntity implements ISyncedTileEntity, CoverHolder,
public static final String TAG_KEY_PAINTING_COLOR = "PaintingColor";
public static final String TAG_KEY_MUFFLED = "Muffled";

private final MTERegistry registry;

public final ResourceLocation metaTileEntityId;
IGregTechTileEntity holder;

Expand Down Expand Up @@ -157,8 +160,9 @@ public abstract class MetaTileEntity implements ISyncedTileEntity, CoverHolder,
private int playSoundCooldown = 0;
private int lastTick = 0;

public MetaTileEntity(ResourceLocation metaTileEntityId) {
protected MetaTileEntity(@NotNull ResourceLocation metaTileEntityId) {
this.metaTileEntityId = metaTileEntityId;
this.registry = GregTechAPI.mteManager.getRegistry(metaTileEntityId.getNamespace());
initializeInventory();
}

Expand Down Expand Up @@ -884,16 +888,24 @@ private void updateSound() {
}
}

public final ItemStack getStackForm(int amount) {
int metaTileEntityIntId = GregTechAPI.MTE_REGISTRY.getIdByObjectName(metaTileEntityId);
return new ItemStack(GregTechAPI.MACHINE, amount, metaTileEntityIntId);
public final @NotNull ItemStack getStackForm(int amount) {
int metaTileEntityIntId = registry.getIdByObjectName(metaTileEntityId);
return new ItemStack(registry.getBlock(), amount, metaTileEntityIntId);
}

@Override
public final @NotNull ItemStack getStackForm() {
return getStackForm(1);
}

public final @NotNull MTERegistry getRegistry() {
return registry;
}

public final @NotNull BlockMachine getBlock() {
return registry.getBlock();
}

/**
* Add special drops which this meta tile entity contains here
* Meta tile entity item is ALREADY added into this list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import gregtech.api.cover.Cover;
import gregtech.api.gui.IUIHolder;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.registry.MTERegistry;
import gregtech.api.util.GTLog;
import gregtech.api.util.Mods;
import gregtech.api.util.TextFormattingUtil;
Expand All @@ -24,7 +25,11 @@
import net.minecraft.util.Rotation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.*;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.IWorldNameable;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
Expand Down Expand Up @@ -90,7 +95,9 @@ public MetaTileEntity setMetaTileEntity(MetaTileEntity sampleMetaTileEntity) {
if (hasWorld() && !getWorld().isRemote) {
updateBlockOpacity();
writeCustomData(INITIALIZE_MTE, buffer -> {
buffer.writeVarInt(GregTechAPI.MTE_REGISTRY.getIdByObjectName(getMetaTileEntity().metaTileEntityId));
buffer.writeVarInt(sampleMetaTileEntity.getRegistry().getNetworkId());
buffer.writeVarInt(
sampleMetaTileEntity.getRegistry().getIdByObjectName(getMetaTileEntity().metaTileEntityId));
getMetaTileEntity().writeInitialSyncData(buffer);
});
// just to update neighbours so cables and other things will work properly
Expand Down Expand Up @@ -126,7 +133,8 @@ public void readFromNBT(@NotNull NBTTagCompound compound) {
if (compound.hasKey("MetaId", NBT.TAG_STRING)) {
String metaTileEntityIdRaw = compound.getString("MetaId");
ResourceLocation metaTileEntityId = new ResourceLocation(metaTileEntityIdRaw);
MetaTileEntity sampleMetaTileEntity = GregTechAPI.MTE_REGISTRY.getObject(metaTileEntityId);
MTERegistry registry = GregTechAPI.mteManager.getRegistry(metaTileEntityId.getNamespace());
MetaTileEntity sampleMetaTileEntity = registry.getObject(metaTileEntityId);
NBTTagCompound metaTileEntityData = compound.getCompoundTag("MetaTileEntity");
if (sampleMetaTileEntity != null) {
setRawMetaTileEntity(sampleMetaTileEntity.createMetaTileEntity(this));
Expand All @@ -136,7 +144,7 @@ public void readFromNBT(@NotNull NBTTagCompound compound) {
*/
this.metaTileEntity.readFromNBT(metaTileEntityData);
} else {
GTLog.logger.error("Failed to load MetaTileEntity with invalid ID " + metaTileEntityIdRaw);
GTLog.logger.error("Failed to load MetaTileEntity with invalid ID {}", metaTileEntityIdRaw);
}
if (Mods.AppliedEnergistics2.isModLoaded()) {
readFromNBT_AENetwork(compound);
Expand Down Expand Up @@ -297,7 +305,8 @@ public void writeInitialSyncData(@NotNull PacketBuffer buf) {
buf.writeString(getName());
if (metaTileEntity != null) {
buf.writeBoolean(true);
buf.writeVarInt(GregTechAPI.MTE_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
buf.writeVarInt(metaTileEntity.getRegistry().getNetworkId());
buf.writeVarInt(metaTileEntity.getRegistry().getIdByObjectName(metaTileEntity.metaTileEntityId));
metaTileEntity.writeInitialSyncData(buf);
} else buf.writeBoolean(false);
}
Expand Down Expand Up @@ -325,8 +334,10 @@ public void receiveCustomData(int discriminator, @NotNull PacketBuffer buffer) {
* @param buf the buffer to read data from
*/
private void receiveMTEInitializationData(@NotNull PacketBuffer buf) {
int networkId = buf.readVarInt();
int metaTileEntityId = buf.readVarInt();
setMetaTileEntity(GregTechAPI.MTE_REGISTRY.getObjectById(metaTileEntityId));
MTERegistry registry = GregTechAPI.mteManager.getRegistry(networkId);
setMetaTileEntity(registry.getObjectById(metaTileEntityId));
this.metaTileEntity.onPlacement();
this.metaTileEntity.receiveInitialSyncData(buf);
scheduleRenderUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntityHolder;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.pattern.*;
import gregtech.api.pattern.BlockPattern;
import gregtech.api.pattern.BlockWorldState;
import gregtech.api.pattern.MultiblockShapeInfo;
import gregtech.api.pattern.PatternMatchContext;
import gregtech.api.pattern.TraceabilityPredicate;
import gregtech.api.pipenet.tile.IPipeTile;
import gregtech.api.unification.material.Material;
import gregtech.api.util.BlockInfo;
Expand Down Expand Up @@ -52,7 +56,18 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.Stack;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -206,7 +221,7 @@ private static Supplier<BlockInfo[]> getCandidates(MetaTileEntity... metaTileEnt
holder.setMetaTileEntity(tile);
holder.getMetaTileEntity().onPlacement();
holder.getMetaTileEntity().setFrontFacing(EnumFacing.SOUTH);
return new BlockInfo(MetaBlocks.MACHINE.getDefaultState(), holder);
return new BlockInfo(tile.getBlock().getDefaultState(), holder);
}).toArray(BlockInfo[]::new);
}

Expand Down
91 changes: 91 additions & 0 deletions src/main/java/gregtech/api/metatileentity/registry/MTEManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package gregtech.api.metatileentity.registry;

import gregtech.api.GTValues;

import net.minecraftforge.fml.common.eventhandler.Event;

import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.Collection;
import java.util.Map;

public final class MTEManager {

private static MTEManager instance;

private static int networkId;
private static MTERegistry internalRegistry;

private final Map<String, MTERegistry> registryMap = new Object2ObjectOpenHashMap<>();
private final Int2ObjectMap<MTERegistry> networkMap = new Int2ObjectOpenHashMap<>();

/**
* @return the global MTE Manager instance
*/
@ApiStatus.Internal
public static @NotNull MTEManager getInstance() {
if (instance == null) {
instance = new MTEManager();
internalRegistry = instance.createRegistry(GTValues.MODID);
}
return instance;
}

private MTEManager() {}

/**
* @param modid the modid of the registry
* @return the registry associated with the modid, otherwise the default registry
*/
public @NotNull MTERegistry getRegistry(@NotNull String modid) {
MTERegistry registry = registryMap.get(modid);
if (registry == null) {
throw new IllegalArgumentException("No MTE registry exists for modid " + modid);
}
return registry;
}

/**
* Create an MTE Registry
*
* @param modid the modid for the registry
* @return the created registry
*/
public @NotNull MTERegistry createRegistry(@NotNull String modid) {
if (registryMap.containsKey(modid)) {
throw new IllegalArgumentException("MTE Registry for modid " + modid + " is already registered");
}
MTERegistry registry = new MTERegistry(modid, ++networkId);
registryMap.put(modid, registry);
networkMap.put(networkId, registry);
return registry;
}

/**
* @param networkId the network id of the registry
* @return the registry associated with the network id, otherwise the default registry
*/
public @NotNull MTERegistry getRegistry(int networkId) {
MTERegistry registry = networkMap.get(networkId);
return registry == null ? internalRegistry : registry;
}

/**
* @return all the available MTE registries
*/
public @NotNull @UnmodifiableView Collection<@NotNull MTERegistry> getRegistries() {
return registryMap.values();
}

/**
* Event during which MTE Registries should be added by mods.
* <p>
* Use {@link #createRegistry(String)} to create a new MTE registry.
*/
public static class MTERegistryEvent extends Event {}
}
Loading

0 comments on commit e79ad8f

Please sign in to comment.