Skip to content

Commit

Permalink
Upstream fix: Fix ItemStackHolder setting, simplify both object holders
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmuscaria committed Mar 19, 2024
1 parent 83e601a commit ab8c31e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
42 changes: 26 additions & 16 deletions patches/cpw/mods/fml/common/registry/ItemStackHolderRef.java.patch
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
--- ../src-base/minecraft/cpw/mods/fml/common/registry/ItemStackHolderRef.java
+++ ../src-work/minecraft/cpw/mods/fml/common/registry/ItemStackHolderRef.java
@@ -1,5 +1,7 @@
@@ -1,19 +1,15 @@
package cpw.mods.fml.common.registry;

+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -40,20 +42,26 @@
private static Object reflectionFactory;
private static Method newFieldAccessor;
private static Method fieldAccessorSet;
+ private static MethodHandle fieldSetter; // Crucible - lwjgl3ify field
private static void makeWritable(Field f)
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-
-import net.minecraft.item.ItemStack;
-
-import org.apache.logging.log4j.Level;
-
import com.google.common.base.Throwables;
-
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.registry.GameRegistry.ItemStackHolder;
+import net.minecraft.item.ItemStack;
+import org.apache.logging.log4j.Level;

+import java.lang.reflect.Field;
+import java.lang.reflect.Method;

+
/**
* Internal class used in tracking {@link ItemStackHolder} references
*
@@ -44,16 +40,19 @@
{
try
{
Expand All @@ -39,13 +51,11 @@
+// }
+// modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
+ f.setAccessible(true);
+ fieldSetter = MethodHandles.lookup()
+ .unreflectSetter(f);
+ // Crucible end
} catch (Exception e)
{
throw Throwables.propagate(e);
@@ -62,23 +70,53 @@
@@ -62,23 +61,53 @@

public void apply()
{
Expand Down Expand Up @@ -94,7 +104,7 @@
- Object fieldAccessor = newFieldAccessor.invoke(reflectionFactory, field, false);
- fieldAccessorSet.invoke(fieldAccessor, null, is);
+ try {
+ fieldSetter.invoke(is);
+ field.set(null, is);
+ } catch (Throwable e) {
+ FMLLog.getLogger()
+ .log(
Expand Down
61 changes: 34 additions & 27 deletions patches/cpw/mods/fml/common/registry/ObjectHolderRef.java.patch
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
--- ../src-base/minecraft/cpw/mods/fml/common/registry/ObjectHolderRef.java
+++ ../src-work/minecraft/cpw/mods/fml/common/registry/ObjectHolderRef.java
@@ -1,5 +1,7 @@
@@ -1,17 +1,18 @@
package cpw.mods.fml.common.registry;

+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -74,16 +76,19 @@
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import org.apache.logging.log4j.Level;
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;
+import net.minecraft.util.RegistryNamespaced;
+import org.apache.logging.log4j.Level;

+import java.lang.reflect.Field;
+import java.lang.reflect.Method;

+
/**
* Internal class used in tracking {@link ObjectHolder} references
*
@@ -74,16 +75,19 @@
{
try
{
Expand Down Expand Up @@ -38,12 +53,11 @@
} catch (Exception e)
{
throw Throwables.propagate(e);
@@ -94,39 +99,80 @@
@@ -94,39 +98,72 @@
{
return isBlock || isItem;
}
+
+ private MethodHandle fieldSetter = null; // Crucible - needed by lwjgl3ify
public void apply()
{
+ // Crucible start - implement lwjgl3ify patch directly
Expand Down Expand Up @@ -79,23 +93,16 @@
+// {
+// FMLLog.log(Level.WARN, e, "Unable to set %s with value %s (%s)", this.field, thing, this.injectedObject);
+// }
+ if (fieldSetter == null) {
+ try {
+ fieldSetter = MethodHandles.lookup()
+ .unreflectSetter(this.field);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
Object thing;
- if (isBlock)
- {
- thing = GameData.getBlockRegistry().getObject(injectedObject);
- if (thing == Blocks.air)
- {
+ RegistryNamespaced registry;
+ if (isBlock) {
+ thing = GameData.getBlockRegistry()
+ .getObject(injectedObject);
+ registry = GameData.getBlockRegistry();
+ thing = registry.getObject(injectedObject);
+ if (thing == Blocks.air) {
thing = null;
}
Expand All @@ -107,8 +114,8 @@
- else
- {
+ } else if (isItem) {
+ thing = GameData.getItemRegistry()
+ .getObject(injectedObject);
+ registry = GameData.getItemRegistry();
+ thing = registry.getObject(injectedObject);
+ } else {
thing = null;
}
Expand All @@ -118,11 +125,11 @@
- FMLLog.getLogger().log(Level.DEBUG, "Unable to lookup {} for {}. This means the object wasn't registered. It's likely just mod options.", injectedObject, field);
+ if (thing == null) {
+ FMLLog.getLogger()
+ .log(
+ Level.DEBUG,
+ "Unable to lookup {} for {}. This means the object wasn't registered. It's likely just mod options.",
+ injectedObject,
+ field);
+ .log(
+ Level.DEBUG,
+ "Unable to lookup {} for {}. This means the object wasn't registered. It's likely just mod options.",
+ injectedObject,
+ field);
return;
}
- try
Expand All @@ -133,7 +140,7 @@
- catch (Exception e)
- {
+ try {
+ fieldSetter.invoke(thing);
+ field.set(null, thing);
+ FMLLog.finer("Set field " + field.toString() + " to " + thing);
+ } catch (Throwable e) {
FMLLog.log(Level.WARN, e, "Unable to set %s with value %s (%s)", this.field, thing, this.injectedObject);
Expand Down

0 comments on commit ab8c31e

Please sign in to comment.