Skip to content

Commit fedcc92

Browse files
committed
Port FoxLoader 1.0 ItemMap patch (Fix #42)
1 parent 2de744a commit fedcc92

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

loader/src/main/java/com/fox2code/foxloader/registry/GameRegistry.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,23 @@ public final class GameRegistry {
9595
private static Block FALLBACK_BLOCK = null;
9696
private static Item FALLBACK_ITEM = null;
9797
private static short FALLBACK_ITEM_BLOCK_ID = 0;
98+
private static final ThreadLocal<int[]> blockIntArrayLocal =
99+
ThreadLocal.withInitial(() -> new int[Internal.nextBlockId]);
98100

99101
private GameRegistry() { throw new AssertionError(); }
100102

103+
/**
104+
* @return array to be temporary used in block calculations.
105+
*/
106+
public static int[] getTemporaryBlockIntArray() {
107+
if (GameRegistry.isFrozen()) {
108+
int[] array = blockIntArrayLocal.get();
109+
Arrays.fill(array, 0);
110+
return array;
111+
}
112+
return new int[Internal.nextBlockId];
113+
}
114+
101115
/**
102116
* @param name the registry id of the item.
103117
* @return a registered modded item with the corresponding registry name

patching/src/main/java/com/fox2code/foxloader/patching/game/RegistryPatch.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ final class RegistryPatch extends GamePatch {
3939
private static final int BLOCK_ID_DIFF = PatchConstants.BLOCK_ID_DIFF;
4040
private static final Integer ORIGINAL_BLOCK_LIMIT_OBJ = ORIGINAL_BLOCK_LIMIT;
4141
private static final Integer MODIFIED_BLOCK_LIMIT_OBJ = MODIFIED_BLOCK_LIMIT;
42+
private static final String BitSet = "java/util/BitSet";
4243
private static final String ItemStack = "net/minecraft/common/item/ItemStack";
4344
private static final String Item = "net/minecraft/common/item/Item";
4445
private static final String Items = "net/minecraft/common/item/Items";
@@ -62,6 +63,7 @@ final class RegistryPatch extends GamePatch {
6263
private static final String ItemBlockPlanks = "net/minecraft/common/item/block/ItemBlockPlanks";
6364
private static final String ItemBlockRedCoral = "net/minecraft/common/item/block/ItemBlockRedCoral";
6465
private static final String ItemBlockSlab = "net/minecraft/common/item/block/ItemBlockSlab";
66+
private static final String ItemMap = "net/minecraft/common/item/children/ItemMap";
6567
private static final String ItemRecord = "net/minecraft/common/item/children/ItemRecord";
6668
private static final String Block = "net/minecraft/common/block/Block";
6769
private static final String Blocks = "net/minecraft/common/block/Blocks";
@@ -141,6 +143,10 @@ public ClassNode transform(ClassNode classNode) {
141143
skipGeneric = true;
142144
break;
143145
}
146+
case ItemMap: {
147+
patchItemMap(classNode);
148+
break;
149+
}
144150
case Blocks: {
145151
patchBlocks(classNode);
146152
skipGeneric = true;
@@ -1091,6 +1097,27 @@ private static void patchBlockSlab(ClassNode classNode) {
10911097
classNode.methods.add(initializeItemBlock);
10921098
}
10931099

1100+
// ItemMap fix
1101+
private static void patchItemMap(ClassNode classNode) {
1102+
for (MethodNode methodNode : classNode.methods) {
1103+
InsnList insnList = methodNode.instructions;
1104+
for (AbstractInsnNode insnNode : insnList) {
1105+
if ((insnNode.getOpcode() == NEWARRAY && ((IntInsnNode) insnNode).operand == T_INT) ||
1106+
(insnNode.getOpcode() == INVOKESPECIAL && ((MethodInsnNode) insnNode).owner.equals(BitSet))) {
1107+
AbstractInsnNode previous = insnNode.getPrevious();
1108+
if (previous instanceof IntInsnNode &&
1109+
((IntInsnNode) previous).operand == PatchConstants.ORIGINAL_BLOCK_LIMIT) {
1110+
insnList.insert(insnNode,
1111+
new MethodInsnNode(INVOKESTATIC, GameRegistry,
1112+
"getTemporaryBlockIntArray", "()[I"));
1113+
insnList.remove(previous);
1114+
insnList.remove(insnNode);
1115+
}
1116+
}
1117+
}
1118+
}
1119+
}
1120+
10941121
// Generic
10951122

10961123
private static void patchItemGeneric(ClassNode classNode) {

0 commit comments

Comments
 (0)