Restone binary compatibility with registry substitution rework.

Modders please switch to using the provided RegistryBuilder to ensure more forward compatbility. Closes #3144
This commit is contained in:
LexManos 2016-08-02 15:55:18 -07:00
parent f088e3df6b
commit f4e8804c8a
3 changed files with 264 additions and 7 deletions

View File

@ -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 <T extends IForgeRegistryEntry<T>> FMLControlledNamespacedRegistry<T> createRegistry(ResourceLocation registryName, Class<T> registryType, ResourceLocation optionalDefaultKey, int minId, int maxId, boolean hasDelegates, IForgeRegistry.AddCallback<T> addCallback, IForgeRegistry.ClearCallback<T> clearCallback, IForgeRegistry.CreateCallback<T> createCallback, IForgeRegistry.SubstitutionCallback<T> substitutionCallback)
@Deprecated //Use RegistryBuilder TODO: Remove in 1.11
public static <T extends IForgeRegistryEntry<T>> FMLControlledNamespacedRegistry<T> createRegistry(
ResourceLocation registryName, Class<T> registryType, ResourceLocation optionalDefaultKey,
int minId, int maxId, boolean hasDelegates,
IForgeRegistry.AddCallback<T> addCallback,
IForgeRegistry.ClearCallback<T> clearCallback,
IForgeRegistry.CreateCallback<T> 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 <T extends IForgeRegistryEntry<T>> FMLControlledNamespacedRegistry<T> createRegistry(
ResourceLocation registryName, Class<T> registryType, ResourceLocation optionalDefaultKey,
int minId, int maxId, boolean hasDelegates,
IForgeRegistry.AddCallback<T> addCallback,
IForgeRegistry.ClearCallback<T> clearCallback,
IForgeRegistry.CreateCallback<T> createCallback,
IForgeRegistry.SubstitutionCallback<T> 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 <T extends IForgeRegistryEntry<T>> IForgeRegistry.ClearCallback<T> getLegacyClear(final IForgeRegistry.ClearCallback<T> cb)
{
if (cb == null)
return null;
try {
final Method mtd = cb.getClass().getMethod("onClear", Map.class);
return new IForgeRegistry.ClearCallback<T>()
{
@Override
public void onClear(IForgeRegistry<T> is, Map<ResourceLocation, ?> 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 <T extends IForgeRegistryEntry<T>> IForgeRegistry.CreateCallback<T> getLegacyCreate(final IForgeRegistry.CreateCallback<T> cb)
{
if (cb == null)
return null;
try {
final Method mtd = cb.getClass().getMethod("onClear", Map.class);
return new IForgeRegistry.CreateCallback<T>()
{
@Override
public void onCreate(Map<ResourceLocation, ?> slaveset, BiMap<ResourceLocation, ? extends IForgeRegistry<?>> 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...
}
}

View File

@ -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<T extends IForgeRegistryEntry<T>>
{
private ResourceLocation registryName;
private Class<T> registryType;
private ResourceLocation optionalDefaultKey;
private int minId;
private int maxId;
private List<AddCallback<T>> addCallback = Lists.newArrayList();
private List<ClearCallback<T>> clearCallback = Lists.newArrayList();
private List<CreateCallback<T>> createCallback = Lists.newArrayList();
private List<SubstitutionCallback<T>> substitutionCallback = Lists.newArrayList();
public RegistryBuilder<T> setName(ResourceLocation name)
{
this.registryName = name;
return this;
}
public RegistryBuilder<T> setType(Class<T> type)
{
this.registryType = type;
return this;
}
public RegistryBuilder<T> setIDRange(int min, int max)
{
this.minId = min;
this.maxId = max;
return this;
}
@SuppressWarnings("unchecked")
public RegistryBuilder<T> addCallback(Object inst)
{
if (inst instanceof AddCallback)
this.add((AddCallback<T>)inst);
if (inst instanceof ClearCallback)
this.add((ClearCallback<T>)inst);
if (inst instanceof CreateCallback)
this.add((CreateCallback<T>)inst);
if (inst instanceof SubstitutionCallback)
this.add((SubstitutionCallback<T>)inst);
return this;
}
public RegistryBuilder<T> add(AddCallback<T> add)
{
this.addCallback.add(add);
return this;
}
public RegistryBuilder<T> add(ClearCallback<T> clear)
{
this.clearCallback.add(clear);
return this;
}
public RegistryBuilder<T> add(CreateCallback<T> create)
{
this.createCallback.add(create);
return this;
}
public RegistryBuilder<T> add(SubstitutionCallback<T> sub)
{
this.substitutionCallback.add(sub);
return this;
}
@SuppressWarnings("deprecation")
public IForgeRegistry<T> create()
{
return PersistentRegistryManager.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, false,
getAdd(), getClear(), getCreate(), getSubstitution());
}
private AddCallback<T> getAdd()
{
if (this.addCallback.isEmpty())
return null;
if (this.addCallback.size() == 1)
return this.addCallback.get(0);
return new AddCallback<T>()
{
@Override
public void onAdd(T obj, int id, Map<ResourceLocation, ?> slaveset)
{
for (AddCallback<T> cb : RegistryBuilder.this.addCallback)
cb.onAdd(obj, id, slaveset);
}
};
}
private ClearCallback<T> getClear()
{
if (this.clearCallback.isEmpty())
return null;
if (this.clearCallback.size() == 1)
return this.clearCallback.get(0);
return new ClearCallback<T>()
{
@Override
public void onClear(IForgeRegistry<T> is, Map<ResourceLocation, ?> slaveset)
{
for (ClearCallback<T> cb : RegistryBuilder.this.clearCallback)
cb.onClear(is, slaveset);
}
};
}
private CreateCallback<T> getCreate()
{
if (this.createCallback.isEmpty())
return null;
if (this.createCallback.size() == 1)
return this.createCallback.get(0);
return new CreateCallback<T>()
{
@Override
public void onCreate(Map<ResourceLocation, ?> slaveset, BiMap<ResourceLocation, ? extends IForgeRegistry<?>> registries)
{
for (CreateCallback<T> cb : RegistryBuilder.this.createCallback)
cb.onCreate(slaveset, registries);
}
};
}
private SubstitutionCallback<T> getSubstitution()
{
if (this.substitutionCallback.isEmpty())
return null;
if (this.substitutionCallback.size() == 1)
return this.substitutionCallback.get(0);
return new SubstitutionCallback<T>()
{
@Override
public void onSubstituteActivated(Map<ResourceLocation, ?> slaveset, T original, T replacement, ResourceLocation name)
{
for (SubstitutionCallback<T> cb : RegistryBuilder.this.substitutionCallback)
cb.onSubstituteActivated(slaveset, original, replacement, name);
}
};
}
}

View File

@ -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);
}
}