use of net.minecraft.util.ResourceLocation in project MinecraftForge by MinecraftForge.
the class GameRegistry method makeItemStack.
/**
* Makes an {@link ItemStack} based on the itemName reference, with supplied meta, stackSize and nbt, if possible
* <p/>
* Will return null if the item doesn't exist (because it's not from a loaded mod for example)
* Will throw a {@link RuntimeException} if the nbtString is invalid for use in an {@link ItemStack}
*
* @param itemName a registry name reference
* @param meta the meta
* @param stackSize the stack size
* @param nbtString an nbt stack as a string, will be processed by {@link JsonToNBT}
* @return a new itemstack
*/
@Nonnull
public static ItemStack makeItemStack(String itemName, int meta, int stackSize, String nbtString) {
if (itemName == null) {
throw new IllegalArgumentException("The itemName cannot be null");
}
Item item = GameData.getItemRegistry().getObject(new ResourceLocation(itemName));
if (item == null) {
FMLLog.getLogger().log(Level.TRACE, "Unable to find item with name {}", itemName);
return ItemStack.EMPTY;
}
ItemStack is = new ItemStack(item, stackSize, meta);
if (!Strings.isNullOrEmpty(nbtString)) {
NBTBase nbttag = null;
try {
nbttag = JsonToNBT.getTagFromJson(nbtString);
} catch (NBTException e) {
FMLLog.getLogger().log(Level.WARN, "Encountered an exception parsing ItemStack NBT string {}", nbtString, e);
throw Throwables.propagate(e);
}
if (!(nbttag instanceof NBTTagCompound)) {
FMLLog.getLogger().log(Level.WARN, "Unexpected NBT string - multiple values {}", nbtString);
throw new RuntimeException("Invalid NBT JSON");
} else {
is.setTagCompound((NBTTagCompound) nbttag);
}
}
return is;
}
use of net.minecraft.util.ResourceLocation in project MinecraftForge by MinecraftForge.
the class PersistentRegistryManager method loadPersistentDataToStagingRegistry.
private static <T extends IForgeRegistryEntry<T>> void loadPersistentDataToStagingRegistry(boolean injectFrozenData, Map<ResourceLocation, Map<ResourceLocation, Integer[]>> remaps, LinkedHashMap<ResourceLocation, Map<ResourceLocation, Integer>> missing, Map.Entry<ResourceLocation, GameDataSnapshot.Entry> snapEntry, Class<T> regType) {
ResourceLocation registryName = getFixedName(snapEntry.getKey());
FMLControlledNamespacedRegistry<T> currentRegistry = PersistentRegistry.ACTIVE.getRegistry(registryName, regType);
if (currentRegistry == null) {
// We've already asked the user if they wish to continue. So if the reg isnt found just assume the user knows and accepted it.
return;
}
FMLControlledNamespacedRegistry<T> newRegistry = PersistentRegistry.STAGING.getOrShallowCopyRegistry(registryName, regType, currentRegistry);
// Copy the persistent substitution set from the currently active one into the new registry
newRegistry.getPersistentSubstitutions().putAll(currentRegistry.getPersistentSubstitutions());
GameDataSnapshot.Entry snapshotEntry = snapEntry.getValue();
Set<ResourceLocation> substitutions = snapshotEntry.substitutions;
if (injectFrozenData) {
substitutions = Sets.union(snapshotEntry.substitutions, currentRegistry.getActiveSubstitutions());
}
newRegistry.loadAliases(snapshotEntry.aliases);
newRegistry.loadBlocked(snapshotEntry.blocked);
missing.put(registryName, Maps.<ResourceLocation, Integer>newLinkedHashMap());
remaps.put(registryName, Maps.<ResourceLocation, Integer[]>newHashMap());
// Load current dummies BEFORE the snapshot is loaded so that add() will remove from the list.
// Potentially causes issues from cpw's previous comment. Must keep eye on.
newRegistry.loadDummied(snapshotEntry.dummied);
newRegistry.loadIds(snapshotEntry.ids, missing.get(registryName), remaps.get(registryName), currentRegistry, registryName);
newRegistry.loadSubstitutions(substitutions);
}
use of net.minecraft.util.ResourceLocation in project MinecraftForge by MinecraftForge.
the class PersistentRegistryManager method fireRegistryEvents.
public static void fireRegistryEvents() {
List<ResourceLocation> registryKeys = Lists.newArrayList(PersistentRegistry.ACTIVE.registries.keySet());
Collections.sort(registryKeys, new Comparator<ResourceLocation>() {
@Override
public int compare(ResourceLocation o1, ResourceLocation o2) {
return o1.toString().compareToIgnoreCase(o2.toString());
}
});
fireRegistryEvent(PersistentRegistry.ACTIVE.registries, BLOCKS);
// inject any blocks
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
fireRegistryEvent(PersistentRegistry.ACTIVE.registries, ITEMS);
// inject any items
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
for (ResourceLocation rl : registryKeys) {
if (rl == BLOCKS || rl == ITEMS)
continue;
fireRegistryEvent(PersistentRegistry.ACTIVE.registries, rl);
}
// inject everything else
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
}
use of net.minecraft.util.ResourceLocation in project MinecraftForge by MinecraftForge.
the class PersistentRegistryManager method loadRegistry.
private static <T extends IForgeRegistryEntry<T>> void loadRegistry(final ResourceLocation registryName, final PersistentRegistry from, final PersistentRegistry to, final Class<T> regType) {
FMLControlledNamespacedRegistry<T> fromRegistry = from.getRegistry(registryName, regType);
if (fromRegistry == null) {
FMLControlledNamespacedRegistry<T> toRegistry = to.getRegistry(registryName, regType);
if (toRegistry == null) {
throw new EnhancedRuntimeException("Could not find registry to load: " + registryName) {
private static final long serialVersionUID = 1L;
@Override
protected void printStackTrace(WrappedPrintStream stream) {
stream.println("Looking For: " + registryName);
stream.println("Found From:");
for (ResourceLocation name : from.registries.keySet()) stream.println(" " + name);
stream.println("Found To:");
for (ResourceLocation name : to.registries.keySet()) stream.println(" " + name);
}
};
}
// We found it in to, so lets trust to's state...
// This happens when connecting to a server that doesn't have this registry.
// Such as a 1.8.0 Forge server with 1.8.8+ Forge.
// We must however, re-fire the callbacks as some internal data may be corrupted {potions}
toRegistry.notifyCallbacks();
} else {
FMLControlledNamespacedRegistry<T> toRegistry = to.getOrShallowCopyRegistry(registryName, regType, fromRegistry);
toRegistry.set(fromRegistry);
}
}
use of net.minecraft.util.ResourceLocation in project MinecraftForge by MinecraftForge.
the class PersistentRegistryManager method injectSnapshot.
public static List<String> injectSnapshot(GameDataSnapshot snapshot, boolean injectFrozenData, boolean isLocalWorld) {
FMLLog.info("Injecting existing block and item data into this %s instance", FMLCommonHandler.instance().getEffectiveSide().isServer() ? "server" : "client");
final Map<ResourceLocation, Map<ResourceLocation, Integer[]>> remaps = Maps.newHashMap();
final LinkedHashMap<ResourceLocation, Map<ResourceLocation, Integer>> missing = Maps.newLinkedHashMap();
forAllRegistries(PersistentRegistry.ACTIVE, ValidateRegistryFunction.OPERATION);
forAllRegistries(PersistentRegistry.ACTIVE, DumpRegistryFunction.OPERATION);
forAllRegistries(PersistentRegistry.ACTIVE, ResetDelegatesFunction.OPERATION);
List<ResourceLocation> missingRegs = Lists.newArrayList();
for (ResourceLocation name : snapshot.entries.keySet()) {
name = getFixedName(name);
if (PersistentRegistry.ACTIVE.getRegistry(name, null) == null) {
missingRegs.add(name);
}
}
if (missingRegs.size() > 0) {
String text = "Forge Mod Loader detected missing/unknown registrie(s).\n\n" + "There are " + missingRegs.size() + " missing registries in this save.\n" + "If you continue the missing registries will get removed.\n" + "This may cause issues, it is advised that you create a world backup before continuing.\n\n" + "Missing Registries:\n";
for (ResourceLocation s : missingRegs) text += s.toString() + "\n";
boolean confirmed = StartupQuery.confirm(text);
if (!confirmed) {
StartupQuery.abort();
;
}
}
// Load the snapshot into the "STAGING" registry
for (Map.Entry<ResourceLocation, GameDataSnapshot.Entry> snapshotEntry : snapshot.entries.entrySet()) {
final Class<? extends IForgeRegistryEntry> registrySuperType = PersistentRegistry.ACTIVE.getRegistrySuperType(snapshotEntry.getKey());
loadPersistentDataToStagingRegistry(injectFrozenData, remaps, missing, snapshotEntry, registrySuperType);
}
// Handle dummied blocks
for (ResourceLocation dummy : snapshot.entries.get(BLOCKS).dummied) {
// Currently missing locally, we just inject and carry on
if (missing.get(BLOCKS).containsKey(dummy)) {
Integer id = missing.get(BLOCKS).remove(dummy);
// Mark this entry as a dummy
PersistentRegistry.STAGING.getRegistry(BLOCKS, Block.class).markDummy(dummy, id, new BlockDummyAir());
} else if (isLocalWorld) {
// Carry on, we resuscitated the block
if (FMLControlledNamespacedRegistry.DEBUG) {
FMLLog.log(Level.DEBUG, "Registry: Resuscitating dummy block %s", dummy);
}
} else {
Integer id = PersistentRegistry.STAGING.getRegistry(BLOCKS, Block.class).getId(dummy);
// The server believes this is a dummy block identity, but we seem to have one locally. This is likely a conflict
// in mod setup - Mark this entry as a dummy
FMLLog.log(Level.WARN, "The ID %d is currently locally mapped - it will be replaced with air for this session", id);
PersistentRegistry.STAGING.getRegistry(BLOCKS, Block.class).markDummy(dummy, id, new BlockDummyAir());
}
}
// If we have missed data, fire the missing mapping event
List<String> missedMappings = Loader.instance().fireMissingMappingEvent(missing.get(BLOCKS), missing.get(ITEMS), isLocalWorld, remaps.get(BLOCKS), remaps.get(ITEMS));
// If there's still missed mappings, we return, because that's an error
if (!missedMappings.isEmpty()) {
return missedMappings;
}
// the block comes back later
if (injectFrozenData) {
for (Map.Entry<ResourceLocation, Integer> missingBlock : missing.get(BLOCKS).entrySet()) {
ResourceLocation rl = missingBlock.getKey();
Integer id = missingBlock.getValue();
FMLLog.log(Level.DEBUG, "Replacing id %s named as %s with air block. If the mod becomes available again later, it can reload here", id, rl);
// Mark this entry as a dummy
PersistentRegistry.STAGING.getRegistry(BLOCKS, Block.class).markDummy(rl, id, new BlockDummyAir());
}
}
// If we're loading up the world from disk, we want to add in the new data that might have been provisioned by mods
if (injectFrozenData) {
// So we load it from the frozen persistent registry
for (Map.Entry<ResourceLocation, FMLControlledNamespacedRegistry<?>> r : PersistentRegistry.ACTIVE.registries.entrySet()) {
final Class<? extends IForgeRegistryEntry> registrySuperType = PersistentRegistry.ACTIVE.getRegistrySuperType(r.getKey());
loadFrozenDataToStagingRegistry(remaps, r.getKey(), registrySuperType);
}
}
// Validate that all the STAGING data is good
forAllRegistries(PersistentRegistry.STAGING, ValidateRegistryFunction.OPERATION);
// Load the STAGING registry into the ACTIVE registry
for (Map.Entry<ResourceLocation, FMLControlledNamespacedRegistry<?>> r : PersistentRegistry.ACTIVE.registries.entrySet()) {
final Class<? extends IForgeRegistryEntry> registrySuperType = PersistentRegistry.ACTIVE.getRegistrySuperType(r.getKey());
loadRegistry(r.getKey(), PersistentRegistry.STAGING, PersistentRegistry.ACTIVE, registrySuperType);
}
// Dump the active registry
forAllRegistries(PersistentRegistry.ACTIVE, DumpRegistryFunction.OPERATION);
// Tell mods that the ids have changed
Loader.instance().fireRemapEvent(remaps.get(BLOCKS), remaps.get(ITEMS), false);
// The id map changed, ensure we apply object holders
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
// Clean out the staging registry now, we're done with it
PersistentRegistry.STAGING.clean();
// Return an empty list, because we're good
return ImmutableList.of();
}
Aggregations