From 6dcd276de1bd8ea5c28429895ec47bfad81c9c6d Mon Sep 17 00:00:00 2001 From: LexManos Date: Wed, 16 Nov 2016 16:54:34 -0800 Subject: [PATCH] Restore the ability to register modded TileEntities. --- .../tileentity/TileEntity.java.patch | 8 ++- .../fml/common/registry/GameData.java | 12 +++++ .../fml/common/registry/GameRegistry.java | 10 ++-- .../registry/LegacyNamespacedRegistry.java | 54 +++++++++++++++++++ 4 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 src/main/java/net/minecraftforge/fml/common/registry/LegacyNamespacedRegistry.java diff --git a/patches/minecraft/net/minecraft/tileentity/TileEntity.java.patch b/patches/minecraft/net/minecraft/tileentity/TileEntity.java.patch index fb74b3183..550d4f02e 100644 --- a/patches/minecraft/net/minecraft/tileentity/TileEntity.java.patch +++ b/patches/minecraft/net/minecraft/tileentity/TileEntity.java.patch @@ -1,6 +1,6 @@ --- ../src-base/minecraft/net/minecraft/tileentity/TileEntity.java +++ ../src-work/minecraft/net/minecraft/tileentity/TileEntity.java -@@ -21,7 +21,7 @@ +@@ -21,10 +21,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -8,7 +8,11 @@ +public abstract class TileEntity implements net.minecraftforge.common.capabilities.ICapabilitySerializable { private static final Logger field_145852_a = LogManager.getLogger(); - private static final RegistryNamespaced < ResourceLocation, Class > field_190562_f = new RegistryNamespaced(); +- private static final RegistryNamespaced < ResourceLocation, Class > field_190562_f = new RegistryNamespaced(); ++ private static final RegistryNamespaced < ResourceLocation, Class > field_190562_f = net.minecraftforge.fml.common.registry.GameData.getTileEntityRegistry(); + protected World field_145850_b; + protected BlockPos field_174879_c = BlockPos.field_177992_a; + protected boolean field_145846_f; @@ -60,6 +60,8 @@ public void func_145839_a(NBTTagCompound p_145839_1_) { diff --git a/src/main/java/net/minecraftforge/fml/common/registry/GameData.java b/src/main/java/net/minecraftforge/fml/common/registry/GameData.java index 438da4be4..a657d56ef 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/GameData.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/GameData.java @@ -31,6 +31,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.potion.Potion; import net.minecraft.potion.PotionType; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ObjectIntIdentityMap; import net.minecraft.util.ResourceLocation; import net.minecraft.util.SoundEvent; @@ -87,6 +88,8 @@ public class GameData FMLLog.log(Level.FATAL, e, "Cannot access the 'block' field from ItemBlock, this is fatal!"); throw Throwables.propagate(e); } + + iTileEntityRegistry = new LegacyNamespacedRegistry>(); } // internal registry objects private final FMLControlledNamespacedRegistry iBlockRegistry; @@ -97,6 +100,11 @@ public class GameData private final FMLControlledNamespacedRegistry iPotionTypeRegistry; private final FMLControlledNamespacedRegistry iEnchantmentRegistry; + //TODO: ? These are never used by ID, so they don't need to be full registries/persisted. + //Need cpw to decide how we want to go about this as they are generic registries that + //don't follow the same patterns as the other ones. + private final LegacyNamespacedRegistry> iTileEntityRegistry; + /** INTERNAL ONLY */ @Deprecated public static FMLControlledNamespacedRegistry getBlockRegistry() @@ -133,6 +141,10 @@ public class GameData @Deprecated public static FMLControlledNamespacedRegistry getEnchantmentRegistry() { return getMain().iEnchantmentRegistry; } + /** INTERNAL ONLY */ + @Deprecated + public static LegacyNamespacedRegistry> getTileEntityRegistry() { return getMain().iTileEntityRegistry; } + @Deprecated static Item findItem(String modId, String name) { diff --git a/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java index 864fa6b43..606c7b646 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java @@ -257,7 +257,7 @@ public class GameRegistry public static void registerTileEntity(Class tileEntityClass, String id) { -// TileEntity.addMapping(tileEntityClass, id); + GameData.getTileEntityRegistry().putObject(new ResourceLocation(id), tileEntityClass); } /** @@ -270,14 +270,10 @@ public class GameRegistry */ public static void registerTileEntityWithAlternatives(Class tileEntityClass, String id, String... alternatives) { -// TileEntity.addMapping(tileEntityClass, id); - Map> teMappings = ObfuscationReflectionHelper.getPrivateValue(TileEntity.class, null, "field_" + "145855_i", "nameToClassMap"); + GameRegistry.registerTileEntity(tileEntityClass, id); for (String s : alternatives) { - if (!teMappings.containsKey(s)) - { - teMappings.put(s, tileEntityClass); - } + GameData.getTileEntityRegistry().addLegacyName(new ResourceLocation(s), new ResourceLocation(id)); } } diff --git a/src/main/java/net/minecraftforge/fml/common/registry/LegacyNamespacedRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/LegacyNamespacedRegistry.java new file mode 100644 index 000000000..403baee01 --- /dev/null +++ b/src/main/java/net/minecraftforge/fml/common/registry/LegacyNamespacedRegistry.java @@ -0,0 +1,54 @@ +/* + * Minecraft Forge + * Copyright (c) 2016. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.fml.common.registry; + +import java.util.Map; + +import javax.annotation.Nullable; + +import com.google.common.collect.Maps; + +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.registry.RegistryNamespaced; + +public class LegacyNamespacedRegistry extends RegistryNamespaced +{ + private Map legacy_names = Maps.newHashMap(); + + @Nullable + public V getObject(@Nullable ResourceLocation name) + { + V ret = super.getObject(name); + + if (ret == null) + { + ResourceLocation new_name = legacy_names.get(name); + if (new_name != null) + ret = super.getObject(new_name); + } + + return ret; + } + + public void addLegacyName(ResourceLocation old_name, ResourceLocation new_name) + { + legacy_names.put(old_name, new_name); + } +}