Fix blockstate id map calculation. (#5279)

This commit is contained in:
David Quintana authored and LexManos committed 2019-01-11 14:52:30 -08:00
1 parent 3765d912a5
commit e98951ee65
11 files changed
+261 -459

No files matched your search

@@ -78,6 +78,7 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
private final AddCallback<V> add;
private final ClearCallback<V> clear;
private final ValidateCallback<V> validate;
private final BakeCallback<V> bake;
private final MissingFactory<V> missing;
private final BitSet availabilityMap;
private final Set<ResourceLocation> dummies = Sets.newHashSet();
@@ -94,23 +95,27 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
private V defaultValue = null;
boolean isFrozen = false;
ForgeRegistry(Class<V> superType, ResourceLocation defaultKey, int min, int max, @Nullable CreateCallback<V> create, @Nullable AddCallback<V> add, @Nullable ClearCallback<V> clear, @Nullable ValidateCallback<V> validate, RegistryManager stage, boolean allowOverrides, boolean isModifiable, @Nullable DummyFactory<V> dummyFactory, @Nullable MissingFactory<V> missing)
private final RegistryBuilder<V> builder;
ForgeRegistry(RegistryManager stage, RegistryBuilder<V> builder)
{
this.builder = builder;
this.stage = stage;
this.superType = superType;
this.defaultKey = defaultKey;
this.min = min;
this.max = max;
this.superType = builder.getType();
this.defaultKey = builder.getDefault();
this.min = builder.getMinId();
this.max = builder.getMaxId();
this.availabilityMap = new BitSet(Math.min(max + 1, 0x0FFF));
this.create = create;
this.add = add;
this.clear = clear;
this.validate = validate;
this.missing = missing;
this.create = builder.getCreate();
this.add = builder.getAdd();
this.clear = builder.getClear();
this.validate = builder.getValidate();
this.bake = builder.getBake();
this.missing = builder.getMissingFactory();
this.dummyFactory = builder.getDummyFactory();
this.isDelegated = ForgeRegistryEntry.class.isAssignableFrom(superType); //TODO: Make this IDelegatedRegistryEntry?
this.allowOverrides = allowOverrides;
this.isModifiable = isModifiable;
this.dummyFactory = dummyFactory;
this.allowOverrides = builder.getAllowOverrides();
this.isModifiable = builder.getAllowModifications();
if (this.create != null)
this.create.onCreate(this, stage);
}
@@ -268,7 +273,7 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
ForgeRegistry<V> copy(RegistryManager stage)
{
return new ForgeRegistry<V>(superType, defaultKey, min, max, create, add, clear, validate, stage, allowOverrides, isModifiable, dummyFactory, missing);
return new ForgeRegistry<>(stage, builder);
}
int add(int id, V value)
@@ -465,6 +470,12 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
}
}
public void bake()
{
if (this.bake != null)
this.bake.onBake(this, this.stage);
}
void sync(ResourceLocation name, ForgeRegistry<V> from)
{
LOGGER.debug(REGISTRIES,"Registry {} Sync: {} -> {}", this.superType.getSimpleName(), this.stage.getName(), from.stage.getName());
@@ -19,10 +19,7 @@
package net.minecraftforge.registries;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.*;
import net.minecraft.block.Block;
import net.minecraft.block.BlockAir;
import net.minecraft.block.material.Material;
@@ -33,6 +30,7 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionType;
import net.minecraft.state.StateContainer;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ObjectIntIdentityMap;
import net.minecraft.util.ResourceLocation;
@@ -43,14 +41,18 @@ import net.minecraft.world.biome.Biome;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.RegistryEvent.MissingMappings;
import net.minecraftforge.fml.common.EnhancedRuntimeException;
import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerProfession;
import net.minecraftforge.fml.ModThreadContext;
import net.minecraftforge.fml.StartupQuery;
import net.minecraftforge.fml.common.EnhancedRuntimeException;
import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerProfession;
import net.minecraftforge.fml.common.thread.EffectiveSide;
import org.apache.commons.lang3.Validate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
@@ -62,17 +64,6 @@ import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import net.minecraftforge.fml.common.thread.EffectiveSide;
import org.apache.commons.lang3.Validate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import net.minecraftforge.fml.common.EnhancedRuntimeException.WrappedPrintStream;
/**
* INTERNAL ONLY
* MODDERS SHOULD HAVE NO REASON TO USE THIS CLASS
@@ -226,7 +217,10 @@ public class GameData
reg.validateContent(name);
reg.freeze();
});
RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.freeze());
RegistryManager.ACTIVE.registries.forEach((name, reg) -> {
reg.freeze();
reg.bake();
});
// the id mapping is finalized, no ids actually changed but this is a good place to tell everyone to 'bake' their stuff.
//Loader.instance().fireRemapEvent(ImmutableMap.of(), true);
@@ -250,6 +244,7 @@ public class GameData
final Class<? extends IForgeRegistryEntry> clazz = RegistryManager.ACTIVE.getSuperType(r.getKey());
loadRegistry(r.getKey(), RegistryManager.FROZEN, RegistryManager.ACTIVE, clazz, true);
}
RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.bake());
// the id mapping has reverted, fire remap events for those that care about id changes
//Loader.instance().fireRemapEvent(ImmutableMap.of(), true);
@@ -273,23 +268,47 @@ public class GameData
{
this.identityMap.clear();
this.objectList.clear();
this.nextId = 0;
}
}
private static class BlockCallbacks implements IForgeRegistry.AddCallback<Block>, IForgeRegistry.ClearCallback<Block>, IForgeRegistry.CreateCallback<Block>, IForgeRegistry.DummyFactory<Block>
private static class BlockCallbacks implements IForgeRegistry.AddCallback<Block>, IForgeRegistry.ClearCallback<Block>, IForgeRegistry.BakeCallback<Block>, IForgeRegistry.CreateCallback<Block>, IForgeRegistry.DummyFactory<Block>
{
static final BlockCallbacks INSTANCE = new BlockCallbacks();
@Override
public void onAdd(IForgeRegistryInternal<Block> owner, RegistryManager stage, int id, Block block, @Nullable Block oldBlock)
{
@SuppressWarnings("unchecked")
ClearableObjectIntIdentityMap<IBlockState> blockstateMap = owner.getSlaveMap(BLOCKSTATE_TO_ID, ClearableObjectIntIdentityMap.class);
if (oldBlock != null)
{
StateContainer<Block, IBlockState> oldContainer = oldBlock.getStateContainer();
StateContainer<Block, IBlockState> newContainer = block.getStateContainer();
ImmutableList<IBlockState> oldValidStates = oldContainer.getValidStates();
ImmutableList<IBlockState> newValidStates = newContainer.getValidStates();
int offset = 0;
for (IBlockState state : block.getStateContainer().getValidStates())
blockstateMap.put(state, id + offset++);
// Test vanilla blockstates, if the number matches, make sure they also match in their string representations
if (block.getRegistryName().getNamespace().equals("minecraft") && (
oldValidStates.size() != newValidStates.size() ||
!Streams.zip(oldValidStates.stream().map(Object::toString),
newValidStates.stream().map(Object::toString),
String::equals).allMatch(v -> v)))
{
String oldSequence = oldContainer.getProperties().stream()
.map(s -> String.format("%s={%s}", s.getName(),
s.getAllowedValues().stream().map(Object::toString).collect(Collectors.joining( "," ))))
.collect(Collectors.joining(";"));
String newSequence = newContainer.getProperties().stream()
.map(s -> String.format("%s={%s}", s.getName(),
s.getAllowedValues().stream().map(Object::toString).collect(Collectors.joining( "," ))))
.collect(Collectors.joining(";"));
LOGGER.error("Registry replacements for vanilla block '{}' must not change the number or order of blockstates.\n"+
"\tOld: {}\n"+
"\tNew: {}", block.getRegistryName(), oldSequence, newSequence);
throw new RuntimeException("Invalid vanilla replacement. See log for details.");
}
}
/*
if ("minecraft:tripwire".equals(block.getRegistryName().toString())) //Tripwire is crap so we have to special case whee!
@@ -362,6 +381,22 @@ public class GameData
GameData.forceRegistryName(ret, key);
return ret;
}
@Override
public void onBake(IForgeRegistryInternal<Block> owner, RegistryManager stage)
{
@SuppressWarnings("unchecked")
ClearableObjectIntIdentityMap<IBlockState> blockstateMap = owner.getSlaveMap(BLOCKSTATE_TO_ID, ClearableObjectIntIdentityMap.class);
for (Block block : owner)
{
for (IBlockState state : block.getStateContainer().getValidStates())
{
blockstateMap.add(state);
}
}
}
private static class BlockDummyAir extends BlockAir //A named class so DummyBlockReplacementTest can detect if its a dummy
{
private BlockDummyAir(Block.Builder builder)
@@ -688,8 +723,12 @@ public class GameData
loadRegistry(key, STAGING, RegistryManager.ACTIVE, registrySuperType, true);
});
// Dump the active registry
RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.dump(name));
RegistryManager.ACTIVE.registries.forEach((name, reg) -> {
reg.bake();
// Dump the active registry
reg.dump(name);
});
// Tell mods that the ids have changed
//Loader.instance().fireRemapEvent(remaps, false);
@@ -95,6 +95,14 @@ public interface IForgeRegistry<V extends IForgeRegistryEntry<V>> extends Iterab
void onValidate(IForgeRegistryInternal<V> owner, RegistryManager stage, int id, ResourceLocation key, V obj);
}
/**
* Callback fired when the registry is done processing. Used to calculate state ID maps.
*/
interface BakeCallback<V extends IForgeRegistryEntry<V>>
{
void onBake(IForgeRegistryInternal<V> owner, RegistryManager stage);
}
/**
* Factory for creating dummy entries, allowing worlds to be loaded and keep the missing block references.
*/
@@ -41,6 +41,7 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
private List<ClearCallback<T>> clearCallback = Lists.newArrayList();
private List<CreateCallback<T>> createCallback = Lists.newArrayList();
private List<ValidateCallback<T>> validateCallback = Lists.newArrayList();
private List<BakeCallback<T>> bakeCallback = Lists.newArrayList();
private boolean saveToDisc = true;
private boolean allowOverrides = true;
private boolean allowModifications = false;
@@ -88,6 +89,8 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
this.add((CreateCallback<T>)inst);
if (inst instanceof ValidateCallback)
this.add((ValidateCallback<T>)inst);
if (inst instanceof BakeCallback)
this.add((BakeCallback<T>)inst);
if (inst instanceof DummyFactory)
this.set((DummyFactory<T>)inst);
if (inst instanceof MissingFactory)
@@ -119,6 +122,12 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
return this;
}
public RegistryBuilder<T> add(BakeCallback<T> bake)
{
this.bakeCallback.add(bake);
return this;
}
public RegistryBuilder<T> set(DummyFactory<T> factory)
{
this.dummyFactory = factory;
@@ -151,12 +160,11 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
public IForgeRegistry<T> create()
{
return RegistryManager.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId,
getAdd(), getClear(), getCreate(), getValidate(), saveToDisc, allowOverrides, allowModifications, dummyFactory, missingFactory);
return RegistryManager.ACTIVE.createRegistry(registryName, this);
}
@Nullable
private AddCallback<T> getAdd()
public AddCallback<T> getAdd()
{
if (addCallback.isEmpty())
return null;
@@ -171,7 +179,7 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
}
@Nullable
private ClearCallback<T> getClear()
public ClearCallback<T> getClear()
{
if (clearCallback.isEmpty())
return null;
@@ -186,7 +194,7 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
}
@Nullable
private CreateCallback<T> getCreate()
public CreateCallback<T> getCreate()
{
if (createCallback.isEmpty())
return null;
@@ -201,7 +209,7 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
}
@Nullable
private ValidateCallback<T> getValidate()
public ValidateCallback<T> getValidate()
{
if (validateCallback.isEmpty())
return null;
@@ -214,4 +222,67 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
cb.onValidate(owner, stage, id, key, obj);
};
}
@Nullable
public BakeCallback<T> getBake()
{
if (bakeCallback.isEmpty())
return null;
if (bakeCallback.size() == 1)
return bakeCallback.get(0);
return (owner, stage) ->
{
for (BakeCallback<T> cb : this.bakeCallback)
cb.onBake(owner, stage);
};
}
public Class<T> getType()
{
return registryType;
}
@Nullable
public ResourceLocation getDefault()
{
return this.optionalDefaultKey;
}
public int getMinId()
{
return minId;
}
public int getMaxId()
{
return maxId;
}
public boolean getAllowOverrides()
{
return allowOverrides;
}
public boolean getAllowModifications()
{
return allowModifications;
}
@Nullable
public DummyFactory<T> getDummyFactory()
{
return dummyFactory;
}
@Nullable
public MissingFactory<T> getMissingFactory()
{
return missingFactory;
}
public boolean getSaveToDisc()
{
return saveToDisc;
}
}
@@ -25,8 +25,6 @@ import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Maps;
@@ -37,7 +35,6 @@ import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.network.FMLHandshakeMessages;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.registries.ForgeRegistry.Snapshot;
import net.minecraftforge.registries.IForgeRegistry.*;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -101,23 +98,22 @@ public class RegistryManager
return getRegistry(key);
}
<V extends IForgeRegistryEntry<V>> ForgeRegistry<V> createRegistry(ResourceLocation name, Class<V> type, ResourceLocation defaultKey, int min, int max,
@Nullable AddCallback<V> add, @Nullable ClearCallback<V> clear, @Nullable CreateCallback<V> create, @Nullable ValidateCallback<V> validate,
boolean persisted, boolean allowOverrides, boolean isModifiable, @Nullable DummyFactory<V> dummyFactory, @Nullable MissingFactory<V> missing)
<V extends IForgeRegistryEntry<V>> ForgeRegistry<V> createRegistry(ResourceLocation name, RegistryBuilder<V> builder)
{
Set<Class<?>> parents = Sets.newHashSet();
findSuperTypes(type, parents);
findSuperTypes(builder.getType(), parents);
SetView<Class<?>> overlappedTypes = Sets.intersection(parents, superTypes.keySet());
if (!overlappedTypes.isEmpty())
{
Class<?> foundType = overlappedTypes.iterator().next();
LOGGER.error("Found existing registry of type {} named {}, you cannot create a new registry ({}) with type {}, as {} has a parent of that type", foundType, superTypes.get(foundType), name, type, type);
LOGGER.error("Found existing registry of type {} named {}, you cannot create a new registry ({}) with type {}, as {} has a parent of that type",
foundType, superTypes.get(foundType), name, builder.getType(), builder.getType());
throw new IllegalArgumentException("Duplicate registry parent type found - you can only have one registry for a particular super type");
}
ForgeRegistry<V> reg = new ForgeRegistry<V>(type, defaultKey, min, max, create, add, clear, validate, this, allowOverrides, isModifiable, dummyFactory, missing);
ForgeRegistry<V> reg = new ForgeRegistry<V>(this, builder);
registries.put(name, reg);
superTypes.put(type, name);
if (persisted)
superTypes.put(builder.getType(), name);
if (builder.getSaveToDisc())
this.persisted.add(name);
return getRegistry(name);
}