diff --git a/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java b/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java index 2ab4869d2e..7790453eb2 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java @@ -20,6 +20,8 @@ package net.minecraftforge.fml.common.registry; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -42,6 +44,7 @@ import net.minecraftforge.fml.common.event.FMLMissingMappingsEvent; import org.apache.logging.log4j.Level; import com.google.common.base.Function; +import com.google.common.base.Throwables; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableList; @@ -154,9 +157,28 @@ public class PersistentRegistryManager public static final ResourceLocation ENCHANTMENTS = new ResourceLocation("minecraft:enchantments"); static final ResourceLocation SUBSTITUTION_ORIGINALS = new ResourceLocation("fml:suboriginals"); - public static > FMLControlledNamespacedRegistry createRegistry(ResourceLocation registryName, Class registryType, ResourceLocation optionalDefaultKey, int minId, int maxId, boolean hasDelegates, IForgeRegistry.AddCallback addCallback, IForgeRegistry.ClearCallback clearCallback, IForgeRegistry.CreateCallback createCallback, IForgeRegistry.SubstitutionCallback substitutionCallback) + @Deprecated //Use RegistryBuilder TODO: Remove in 1.11 + public static > FMLControlledNamespacedRegistry createRegistry( + ResourceLocation registryName, Class registryType, ResourceLocation optionalDefaultKey, + int minId, int maxId, boolean hasDelegates, + IForgeRegistry.AddCallback addCallback, + IForgeRegistry.ClearCallback clearCallback, + IForgeRegistry.CreateCallback createCallback) { - return PersistentRegistry.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, addCallback, clearCallback, createCallback, substitutionCallback); + return PersistentRegistry.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, + addCallback, getLegacyClear(clearCallback), getLegacyCreate(createCallback), null); + } + @Deprecated //Use RegistryBuilder TODO: Remove in 1.11 {Make package private so only builder can use it} + public static > FMLControlledNamespacedRegistry createRegistry( + ResourceLocation registryName, Class registryType, ResourceLocation optionalDefaultKey, + int minId, int maxId, boolean hasDelegates, + IForgeRegistry.AddCallback addCallback, + IForgeRegistry.ClearCallback clearCallback, + IForgeRegistry.CreateCallback createCallback, + IForgeRegistry.SubstitutionCallback substitutionCallback) + { + return PersistentRegistry.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, + addCallback, clearCallback, createCallback, substitutionCallback); } @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -707,4 +729,64 @@ public class PersistentRegistryManager } } + + //TODO: Remove in 1.11, creates wrappers for API breakage cpw did in registry re-work. + private static > IForgeRegistry.ClearCallback getLegacyClear(final IForgeRegistry.ClearCallback cb) + { + if (cb == null) + return null; + try { + final Method mtd = cb.getClass().getMethod("onClear", Map.class); + return new IForgeRegistry.ClearCallback() + { + @Override + public void onClear(IForgeRegistry is, Map slaveset) + { + try { + mtd.invoke(cb, slaveset); + } catch (Exception e) { + e.printStackTrace( ); + Throwables.propagate(e); + } + } + }; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return cb; //Assume they are ussing modern API + } catch (SecurityException e) { + e.printStackTrace(); + Throwables.propagate(e); + } + return null; //Will never get here unless things go wonkey... + } + + //TODO: Remove in 1.11, creates wrappers for API breakage cpw did in registry re-work. + private static > IForgeRegistry.CreateCallback getLegacyCreate(final IForgeRegistry.CreateCallback cb) + { + if (cb == null) + return null; + try { + final Method mtd = cb.getClass().getMethod("onClear", Map.class); + return new IForgeRegistry.CreateCallback() + { + @Override + public void onCreate(Map slaveset, BiMap> registries) + { + try { + mtd.invoke(cb, slaveset); + } catch (Exception e) { + e.printStackTrace( ); + Throwables.propagate(e); + } + } + }; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return cb; //Assume they are ussing modern API + } catch (SecurityException e) { + e.printStackTrace(); + Throwables.propagate(e); + } + return null; //Will never get here unless things go wonkey... + } } diff --git a/src/main/java/net/minecraftforge/fml/common/registry/RegistryBuilder.java b/src/main/java/net/minecraftforge/fml/common/registry/RegistryBuilder.java new file mode 100644 index 0000000000..cfad0b4dea --- /dev/null +++ b/src/main/java/net/minecraftforge/fml/common/registry/RegistryBuilder.java @@ -0,0 +1,175 @@ +/* + * 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.List; +import java.util.Map; + +import com.google.common.collect.BiMap; +import com.google.common.collect.Lists; + +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.common.registry.IForgeRegistry.*; + +public class RegistryBuilder> +{ + private ResourceLocation registryName; + private Class registryType; + private ResourceLocation optionalDefaultKey; + private int minId; + private int maxId; + private List> addCallback = Lists.newArrayList(); + private List> clearCallback = Lists.newArrayList(); + private List> createCallback = Lists.newArrayList(); + private List> substitutionCallback = Lists.newArrayList(); + + public RegistryBuilder setName(ResourceLocation name) + { + this.registryName = name; + return this; + } + + public RegistryBuilder setType(Class type) + { + this.registryType = type; + return this; + } + + public RegistryBuilder setIDRange(int min, int max) + { + this.minId = min; + this.maxId = max; + return this; + } + + @SuppressWarnings("unchecked") + public RegistryBuilder addCallback(Object inst) + { + if (inst instanceof AddCallback) + this.add((AddCallback)inst); + if (inst instanceof ClearCallback) + this.add((ClearCallback)inst); + if (inst instanceof CreateCallback) + this.add((CreateCallback)inst); + if (inst instanceof SubstitutionCallback) + this.add((SubstitutionCallback)inst); + return this; + } + + public RegistryBuilder add(AddCallback add) + { + this.addCallback.add(add); + return this; + } + + public RegistryBuilder add(ClearCallback clear) + { + this.clearCallback.add(clear); + return this; + } + + public RegistryBuilder add(CreateCallback create) + { + this.createCallback.add(create); + return this; + } + + public RegistryBuilder add(SubstitutionCallback sub) + { + this.substitutionCallback.add(sub); + return this; + } + + @SuppressWarnings("deprecation") + public IForgeRegistry create() + { + return PersistentRegistryManager.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, false, + getAdd(), getClear(), getCreate(), getSubstitution()); + } + + private AddCallback getAdd() + { + if (this.addCallback.isEmpty()) + return null; + if (this.addCallback.size() == 1) + return this.addCallback.get(0); + + return new AddCallback() + { + @Override + public void onAdd(T obj, int id, Map slaveset) + { + for (AddCallback cb : RegistryBuilder.this.addCallback) + cb.onAdd(obj, id, slaveset); + } + }; + } + private ClearCallback getClear() + { + if (this.clearCallback.isEmpty()) + return null; + if (this.clearCallback.size() == 1) + return this.clearCallback.get(0); + + return new ClearCallback() + { + @Override + public void onClear(IForgeRegistry is, Map slaveset) + { + for (ClearCallback cb : RegistryBuilder.this.clearCallback) + cb.onClear(is, slaveset); + } + }; + } + private CreateCallback getCreate() + { + if (this.createCallback.isEmpty()) + return null; + if (this.createCallback.size() == 1) + return this.createCallback.get(0); + + return new CreateCallback() + { + @Override + public void onCreate(Map slaveset, BiMap> registries) + { + for (CreateCallback cb : RegistryBuilder.this.createCallback) + cb.onCreate(slaveset, registries); + } + }; + } + private SubstitutionCallback getSubstitution() + { + if (this.substitutionCallback.isEmpty()) + return null; + if (this.substitutionCallback.size() == 1) + return this.substitutionCallback.get(0); + + return new SubstitutionCallback() + { + @Override + public void onSubstituteActivated(Map slaveset, T original, T replacement, ResourceLocation name) + { + for (SubstitutionCallback cb : RegistryBuilder.this.substitutionCallback) + cb.onSubstituteActivated(slaveset, original, replacement, name); + } + }; + } +} diff --git a/src/test/java/net/minecraftforge/debug/DecorateEventDebug.java b/src/test/java/net/minecraftforge/debug/DecorateEventDebug.java index 88ec8a3179..bcb8a954f4 100644 --- a/src/test/java/net/minecraftforge/debug/DecorateEventDebug.java +++ b/src/test/java/net/minecraftforge/debug/DecorateEventDebug.java @@ -9,7 +9,7 @@ import net.minecraftforge.fml.common.eventhandler.Event.Result; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @Mod(modid = DecorateEventDebug.MODID, name = DecorateEventDebug.NAME, version = DecorateEventDebug.VERSION) -public class DecorateEventDebug +public class DecorateEventDebug { public static final String MODID = "decorateeventdebug"; @@ -17,14 +17,14 @@ public class DecorateEventDebug public static final String VERSION = "1.0.0"; @EventHandler - public void init(FMLInitializationEvent event) + public void init(FMLInitializationEvent event) { - MinecraftForge.TERRAIN_GEN_BUS.register(this); + MinecraftForge.TERRAIN_GEN_BUS.register(this); } @SubscribeEvent - public void decorateEvent(DecorateBiomeEvent.Decorate event) + public void decorateEvent(DecorateBiomeEvent.Decorate event) { - event.setResult(Result.DENY); + event.setResult(Result.DENY); } }