From 4512f8e5e316ddaf6a4fe35470f1f88dcdddae1a Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 26 May 2014 10:58:13 -0400 Subject: [PATCH] Warn when the objectholder finds nothing in the registry. Helps debug mismatched names. Also, actually make the scoping thing work with objectholder Also handle Blocks.air as a special case. It should never be a valid block in the block registry. Nothing can ever replace the default block registry block. It is disabled for replacement. Air is properly skipped --- .../FMLControlledNamespacedRegistry.java | 7 +++- .../fml/common/registry/ObjectHolderRef.java | 37 +++++++++++++++++-- .../common/registry/ObjectHolderRegistry.java | 22 ++++++++++- 3 files changed, 60 insertions(+), 6 deletions(-) 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 a53d9250ff..6375e64d13 100644 --- a/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java +++ b/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java @@ -330,7 +330,7 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { if (name.isEmpty()) throw new IllegalArgumentException(String.format("Can't use an empty name for the registry, object %s.", thing)); if (name.indexOf(':') == -1) throw new IllegalArgumentException(String.format("Can't add the name (%s) without a prefix, object %s", name, thing)); if (thing == null) throw new NullPointerException(String.format("Can't add null-object to the registry, name %s.", name)); - if (name.equals(optionalDefaultName)) + if (name.equals(optionalDefaultName) && this.optionalDefaultObject == null) { this.optionalDefaultObject = thing; } @@ -420,4 +420,9 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { underlyingIntegerMap.func_148746_a(thing, id); // obj <-> id super.putObject(name, thing); // name <-> obj } + + public I getDefaultValue() + { + return optionalDefaultObject; + } } diff --git a/src/main/java/cpw/mods/fml/common/registry/ObjectHolderRef.java b/src/main/java/cpw/mods/fml/common/registry/ObjectHolderRef.java index 65e2082c71..c072d3a525 100644 --- a/src/main/java/cpw/mods/fml/common/registry/ObjectHolderRef.java +++ b/src/main/java/cpw/mods/fml/common/registry/ObjectHolderRef.java @@ -8,6 +8,7 @@ import com.google.common.base.Throwables; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.registry.GameRegistry.ObjectHolder; import net.minecraft.block.Block; +import net.minecraft.init.Blocks; import net.minecraft.item.Item; @@ -34,8 +35,20 @@ class ObjectHolderRef { try { Object existing = field.get(null); - this.injectedObject = isBlock ? GameData.getBlockRegistry().getNameForObject(existing) : - isItem ? GameData.getItemRegistry().getNameForObject(existing) : null; + // nothing is ever allowed to replace AIR + if (existing == null || existing == GameData.getBlockRegistry().getDefaultValue()) + { + this.injectedObject = null; + this.field = null; + this.isBlock = false; + this.isItem = false; + return; + } + else + { + this.injectedObject = isBlock ? GameData.getBlockRegistry().getNameForObject(existing) : + isItem ? GameData.getItemRegistry().getNameForObject(existing) : null; + } } catch (Exception e) { throw Throwables.propagate(e); @@ -85,9 +98,27 @@ class ObjectHolderRef { { Object thing; if (isBlock) + { thing = GameData.getBlockRegistry().getObject(injectedObject); - else + if (thing == Blocks.air) + { + thing = null; + } + } + else if (isItem) + { thing = GameData.getItemRegistry().getObject(injectedObject); + } + else + { + thing = null; + } + + if (thing == null) + { + FMLLog.warning("Unable to lookup %s for %s. Is there something wrong with the registry?", injectedObject, field); + return; + } try { Object fieldAccessor = newFieldAccessor.invoke(reflectionFactory, field, false); diff --git a/src/main/java/cpw/mods/fml/common/registry/ObjectHolderRegistry.java b/src/main/java/cpw/mods/fml/common/registry/ObjectHolderRegistry.java index d4dbb0c7f6..b1173e25e4 100644 --- a/src/main/java/cpw/mods/fml/common/registry/ObjectHolderRegistry.java +++ b/src/main/java/cpw/mods/fml/common/registry/ObjectHolderRegistry.java @@ -34,7 +34,22 @@ public enum ObjectHolderRegistry { String annotationTarget = data.getObjectName(); String value = (String) data.getAnnotationInfo().get("value"); boolean isClass = className.equals(annotationTarget); - scanTarget(classModIds, classCache, className, annotationTarget, value, isClass, false); + if (isClass) + { + scanTarget(classModIds, classCache, className, annotationTarget, value, isClass, false); + } + } + // double pass - get all the class level annotations first, then the field level annotations + for (ASMData data : allObjectHolders) + { + String className = data.getClassName(); + String annotationTarget = data.getObjectName(); + String value = (String) data.getAnnotationInfo().get("value"); + boolean isClass = className.equals(annotationTarget); + if (!isClass) + { + scanTarget(classModIds, classCache, className, annotationTarget, value, isClass, false); + } } scanTarget(classModIds, classCache, "net.minecraft.init.Blocks", null, "minecraft", true, true); scanTarget(classModIds, classCache, "net.minecraft.init.Items", null, "minecraft", true, true); @@ -107,7 +122,10 @@ public enum ObjectHolderRegistry { private void addHolderReference(ObjectHolderRef ref) { - objectHolders.add(ref); + if (ref.isValid()) + { + objectHolders.add(ref); + } } public void applyObjectHolders()