From 810b1f3075e6061ab189e1f6975bd77b20040d71 Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 30 Apr 2014 18:24:58 -0400 Subject: [PATCH] Clean some generic warnings up. Make a generic list handler. Helpful for others I think too. --- .../cpw/mods/fml/common/FMLContainer.java | 1 - .../java/cpw/mods/fml/common/ZipperUtil.java | 2 - .../functions/GenericIterableFactory.java | 10 +++ .../common/functions/TypeCastFunction.java | 17 +++++ .../FMLControlledNamespacedRegistry.java | 67 ++++++++++++++----- .../mods/fml/common/registry/GameData.java | 60 +++-------------- 6 files changed, 84 insertions(+), 73 deletions(-) create mode 100644 src/main/java/cpw/mods/fml/common/functions/GenericIterableFactory.java create mode 100644 src/main/java/cpw/mods/fml/common/functions/TypeCastFunction.java diff --git a/src/main/java/cpw/mods/fml/common/FMLContainer.java b/src/main/java/cpw/mods/fml/common/FMLContainer.java index 23f4327203..fd20bb7313 100644 --- a/src/main/java/cpw/mods/fml/common/FMLContainer.java +++ b/src/main/java/cpw/mods/fml/common/FMLContainer.java @@ -22,7 +22,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import net.minecraft.item.Item; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; diff --git a/src/main/java/cpw/mods/fml/common/ZipperUtil.java b/src/main/java/cpw/mods/fml/common/ZipperUtil.java index a92a3c32ca..b4943b166b 100644 --- a/src/main/java/cpw/mods/fml/common/ZipperUtil.java +++ b/src/main/java/cpw/mods/fml/common/ZipperUtil.java @@ -15,8 +15,6 @@ import org.apache.logging.log4j.Level; import com.google.common.io.Files; -import cpw.mods.fml.client.FMLClientHandler; - /** * Copied from http://stackoverflow.com/questions/1399126/java-util-zip-recreating-directory-structure * because the code looked very tidy and neat. Thanks, McDowell! diff --git a/src/main/java/cpw/mods/fml/common/functions/GenericIterableFactory.java b/src/main/java/cpw/mods/fml/common/functions/GenericIterableFactory.java new file mode 100644 index 0000000000..85be09b2eb --- /dev/null +++ b/src/main/java/cpw/mods/fml/common/functions/GenericIterableFactory.java @@ -0,0 +1,10 @@ +package cpw.mods.fml.common.functions; + +import com.google.common.collect.Iterables; + +public class GenericIterableFactory { + public static Iterable newCastingIterable(Iterable input, Class type) + { + return Iterables.transform(input, new TypeCastFunction(type)); + } +} diff --git a/src/main/java/cpw/mods/fml/common/functions/TypeCastFunction.java b/src/main/java/cpw/mods/fml/common/functions/TypeCastFunction.java new file mode 100644 index 0000000000..e82a212655 --- /dev/null +++ b/src/main/java/cpw/mods/fml/common/functions/TypeCastFunction.java @@ -0,0 +1,17 @@ +package cpw.mods.fml.common.functions; + +import com.google.common.base.Function; + +public class TypeCastFunction implements Function { + private Class type; + public TypeCastFunction(Class type) + { + this.type = type; + } + + @Override + public T apply(Object input) + { + return type.cast(input); + } +} diff --git a/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java b/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java index 72d338b9f7..9970e965b6 100644 --- a/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java +++ b/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java @@ -6,17 +6,17 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import net.minecraft.block.Block; +import net.minecraft.item.ItemBlock; import net.minecraft.util.ObjectIntIdentityMap; import net.minecraft.util.RegistryNamespaced; -import com.google.common.base.Function; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; import cpw.mods.fml.common.FMLLog; -import cpw.mods.fml.common.Loader; -import cpw.mods.fml.common.ModContainer; +import cpw.mods.fml.common.functions.GenericIterableFactory; public class FMLControlledNamespacedRegistry extends RegistryNamespaced { private final Class superType; @@ -38,7 +38,49 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { this.minId = minIdValue; } - @SuppressWarnings("unchecked") + void validateContent(int maxId, String type, BitSet availabilityMap, Set blockedIds, FMLControlledNamespacedRegistry iBlockRegistry) + { + for (I obj : typeSafeIterable()) + { + int id = getId(obj); + String name = getNameForObject(obj); + + // id lookup failed -> obj is not in the obj<->id map + if (id < 0) throw new IllegalStateException(String.format("Registry entry for %s %s, name %s, doesn't yield an id.", type, obj, name)); + // id is too high + if (id > maxId) throw new IllegalStateException(String.format("Registry entry for %s %s, name %s uses the too large id %d.", type, obj, name)); + // name lookup failed -> obj is not in the obj<->name map + if (name == null) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, doesn't yield a name.", type, obj, id)); + // empty name + if (name.isEmpty()) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, yields an empty name.", type, obj, id)); + // non-prefixed name + if (name.indexOf(':') == -1) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, has the non-prefixed name %s.", type, obj, id, name)); + // id -> obj lookup is inconsistent + if (getRaw(id) != obj) throw new IllegalStateException(String.format("Registry entry for id %d, name %s, doesn't yield the expected %s %s.", id, name, type, obj)); + // name -> obj lookup is inconsistent + if (getRaw(name) != obj) throw new IllegalStateException(String.format("Registry entry for name %s, id %d, doesn't yield the expected %s %s.", name, id, type, obj)); + // name -> id lookup is inconsistent + if (getId(name) != id) throw new IllegalStateException(String.format("Registry entry for name %s doesn't yield the expected id %d.", name, id)); + // id isn't marked as unavailable + if (!availabilityMap.get(id)) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, name %s, marked as empty.", type, obj, id, name)); + // entry is blocked, thus should be empty + if (blockedIds.contains(id)) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, name %s, marked as dangling.", type, obj, id, name)); + + if (obj instanceof ItemBlock) + { + Block block = ((ItemBlock) obj).field_150939_a; + + // verify matching block entry + if (iBlockRegistry.getId(block) != id) + { + throw new IllegalStateException(String.format("Registry entry for ItemBlock %s, id %d, is missing or uses the non-matching id %d.", obj, id, iBlockRegistry.getId(block))); + } + // verify id range + if (id > GameData.MAX_BLOCK_ID) throw new IllegalStateException(String.format("ItemBlock %s uses the id %d outside the block id range", name, id)); + } + } + + } void set(FMLControlledNamespacedRegistry registry) { if (this.superType != registry.superType) throw new IllegalArgumentException("incompatible registry"); @@ -254,12 +296,12 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { public Iterable typeSafeIterable() { - return Iterables.transform(this, new TypeCastFunction()); + Iterable self = this; + return GenericIterableFactory.newCastingIterable(self, superType); } // internal - @SuppressWarnings("unchecked") public void serializeInto(Map idMapping) // for saving { for (I thing : this.typeSafeIterable()) @@ -333,7 +375,6 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { FMLLog.finer("Registry alias: %s -> %s", from, to); } - @SuppressWarnings("unchecked") Map getEntriesNotIn(FMLControlledNamespacedRegistry registry) { Map ret = new HashMap(); @@ -346,7 +387,6 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { return ret; } - @SuppressWarnings("unchecked") void dump() { List ids = new ArrayList(); @@ -378,13 +418,4 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { underlyingIntegerMap.func_148746_a(thing, id); // obj <-> id super.putObject(name, thing); // name <-> obj } - - private class TypeCastFunction implements Function - { - @Override - public I apply(Object o) - { - return superType.cast(o); - } - } } diff --git a/src/main/java/cpw/mods/fml/common/registry/GameData.java b/src/main/java/cpw/mods/fml/common/registry/GameData.java index 9d17d1ea0a..79d92d78de 100644 --- a/src/main/java/cpw/mods/fml/common/registry/GameData.java +++ b/src/main/java/cpw/mods/fml/common/registry/GameData.java @@ -14,7 +14,6 @@ package cpw.mods.fml.common.registry; import java.io.File; import java.io.IOException; -import java.util.Arrays; import java.util.BitSet; import java.util.HashMap; import java.util.HashSet; @@ -43,7 +42,6 @@ import com.google.common.collect.Maps; import com.google.common.collect.Table; import com.google.common.io.Files; -import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.Loader; @@ -56,10 +54,10 @@ import cpw.mods.fml.common.registry.GameRegistry.Type; import cpw.mods.fml.common.registry.GameRegistry.UniqueIdentifier; public class GameData { - private static final int MIN_BLOCK_ID = 0; - private static final int MAX_BLOCK_ID = 4095; - private static final int MIN_ITEM_ID = 4096; - private static final int MAX_ITEM_ID = 31999; + static final int MIN_BLOCK_ID = 0; + static final int MAX_BLOCK_ID = 4095; + static final int MIN_ITEM_ID = 4096; + static final int MAX_ITEM_ID = 31999; private static final GameData mainData = new GameData(); @@ -244,6 +242,7 @@ public class GameData { for (Entry entry : dataList.entrySet()) { String itemName = entry.getKey(); + @SuppressWarnings("unused") String realName = itemName.substring(1); if (itemName.charAt(0) == '\u0001') // is a block @@ -351,7 +350,7 @@ public class GameData { { String mod = it.next(); - if (mod.equals("minecraft") || Loader.instance().isModLoaded(mod)) it.remove(); + if (mod.equals("minecraft") || Loader.isModLoaded(mod)) it.remove(); } if (!modsMissing.isEmpty()) @@ -907,7 +906,6 @@ public class GameData { } } - @SuppressWarnings("unchecked") private void testConsistency() { // test if there's an entry for every set bit in availabilityMap for (int i = availabilityMap.nextSetBit(0); i >= 0; i = availabilityMap.nextSetBit(i+1)) @@ -922,50 +920,8 @@ public class GameData { { boolean isBlock = pass == 0; String type = isBlock ? "block" : "item"; - FMLControlledNamespacedRegistry registry = isBlock ? iBlockRegistry : iItemRegistry; - - // test if the registry is consistent - // test if there's a bit in availabilityMap set for every entry in the registry - // make sure it's not a blocked id - for (Object obj : registry) - { - int id = registry.getId(obj); - String name = registry.getNameForObject(obj); - - // id lookup failed -> obj is not in the obj<->id map - if (id < 0) throw new IllegalStateException(String.format("Registry entry for %s %s, name %s, doesn't yield an id.", type, obj, name)); - // id is too high - if (id > (isBlock ? MAX_BLOCK_ID : MAX_ITEM_ID)) throw new IllegalStateException(String.format("Registry entry for %s %s, name %s uses the too large id %d.", type, obj, name)); - // name lookup failed -> obj is not in the obj<->name map - if (name == null) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, doesn't yield a name.", type, obj, id)); - // empty name - if (name.isEmpty()) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, yields an empty name.", type, obj, id)); - // non-prefixed name - if (name.indexOf(':') == -1) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, has the non-prefixed name %s.", type, obj, id, name)); - // id -> obj lookup is inconsistent - if (registry.getRaw(id) != obj) throw new IllegalStateException(String.format("Registry entry for id %d, name %s, doesn't yield the expected %s %s.", id, name, type, obj)); - // name -> obj lookup is inconsistent - if (registry.getRaw(name) != obj) throw new IllegalStateException(String.format("Registry entry for name %s, id %d, doesn't yield the expected %s %s.", name, id, type, obj)); - // name -> id lookup is inconsistent - if (registry.getId(name) != id) throw new IllegalStateException(String.format("Registry entry for name %s doesn't yield the expected id %d.", name, id)); - // id isn't marked as unavailable - if (!availabilityMap.get(id)) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, name %s, marked as empty.", type, obj, id, name)); - // entry is blocked, thus should be empty - if (blockedIds.contains(id)) throw new IllegalStateException(String.format("Registry entry for %s %s, id %d, name %s, marked as dangling.", type, obj, id, name)); - - if (obj instanceof ItemBlock) - { - Block block = ((ItemBlock) obj).field_150939_a; - - // verify matching block entry - if (iBlockRegistry.getId(block) != id) - { - throw new IllegalStateException(String.format("Registry entry for ItemBlock %s, id %d, is missing or uses the non-matching id %d.", obj, id, iBlockRegistry.getId(block))); - } - // verify id range - if (id > MAX_BLOCK_ID) throw new IllegalStateException(String.format("ItemBlock %s uses the id %d outside the block id range", name, id)); - } - } + FMLControlledNamespacedRegistry registry = isBlock ? iBlockRegistry : iItemRegistry; + registry.validateContent((isBlock ? MAX_BLOCK_ID : MAX_ITEM_ID), type, availabilityMap, blockedIds, iBlockRegistry); } FMLLog.fine("Registry consistency check successful");