Skip to content

Commit c3f7e5c

Browse files
committed
Add proper creative tab API.
1 parent c5073a1 commit c3f7e5c

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public static void freeze() {
311311
for (int i = INITIAL_TRANSLATED_BLOCK_ID; i < MAXIMUM_ITEM_ID; i++) {
312312
Item item = Items.ITEMS_LIST[i];
313313
CreativeTab creativeTab;
314-
if (item != null && (creativeTab = item.getRegisterFLTab()) != null) {
314+
if (item != null && (creativeTab = item.getCreativeTab()) != null) {
315315
creativeTab.getGlobalItemList(); // Force build tab to avoid it being empty.
316316
creativeTab.add(item);
317317
}

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

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public ClassNode transform(ClassNode classNode) {
274274
}
275275

276276
// Helpers
277-
private static void injectGetRegisterFLTab(ClassNode classNode, String creativeTab) {
277+
private static void injectGetRegisterFLTab(ClassNode classNode, String creativeTab, boolean withField) {
278278
MethodNode getRegisterFLTab = new MethodNode(ASM_API,
279279
ACC_PUBLIC, "getRegisterFLTab", "()L" + CreativeTab + ";", null, null);
280280
getRegisterFLTab.instructions.add(new FieldInsnNode(
@@ -283,6 +283,45 @@ private static void injectGetRegisterFLTab(ClassNode classNode, String creativeT
283283
classNode.methods.add(getRegisterFLTab);
284284
}
285285

286+
private static void injectGetCreativeTab(ClassNode classNode) {
287+
FieldNode creativeTab = new FieldNode(ACC_PRIVATE,
288+
"creativeTab", "L" + CreativeTab + ";", null, null);
289+
classNode.fields.add(creativeTab);
290+
MethodNode getCreativeTab = new MethodNode(ASM_API,
291+
ACC_PUBLIC, "getCreativeTab", "()L" + CreativeTab + ";", null, null);
292+
getCreativeTab.instructions.add(new VarInsnNode(ALOAD, 0));
293+
getCreativeTab.instructions.add(new FieldInsnNode(GETFIELD,
294+
classNode.name, creativeTab.name, creativeTab.desc));
295+
getCreativeTab.instructions.add(new VarInsnNode(ASTORE, 1));
296+
getCreativeTab.instructions.add(new VarInsnNode(ALOAD, 1));
297+
LabelNode nullCreativeTabOhNo = new LabelNode();
298+
getCreativeTab.instructions.add(new JumpInsnNode(IFNULL, nullCreativeTabOhNo));
299+
getCreativeTab.instructions.add(new VarInsnNode(ALOAD, 1));
300+
getCreativeTab.instructions.add(new InsnNode(ARETURN));
301+
getCreativeTab.instructions.add(nullCreativeTabOhNo);
302+
getCreativeTab.instructions.add(new VarInsnNode(ALOAD, 0));
303+
getCreativeTab.instructions.add(new MethodInsnNode(INVOKEVIRTUAL,
304+
classNode.name, "getRegisterFLTab", "()L" + CreativeTab + ";"));
305+
getCreativeTab.instructions.add(new InsnNode(ARETURN));
306+
TransformerUtils.setThisParameterName(classNode, getCreativeTab);
307+
getCreativeTab.localVariables.add(new LocalVariableNode(
308+
"creativeTab", "L" + CreativeTab + ";", null,
309+
TransformerUtils.getBeginingLabelNode(getCreativeTab),
310+
TransformerUtils.getEndingLabelNode(getCreativeTab), 1));
311+
classNode.methods.add(getCreativeTab);
312+
MethodNode setCreativeTab = new MethodNode(ASM_API,
313+
ACC_PUBLIC, "setCreativeTab", "(L" + CreativeTab + ";)L" + classNode.name + ";", null, null);
314+
setCreativeTab.instructions.add(new VarInsnNode(ALOAD, 0));
315+
setCreativeTab.instructions.add(new VarInsnNode(ALOAD, 1));
316+
setCreativeTab.instructions.add(new FieldInsnNode(PUTFIELD,
317+
classNode.name, creativeTab.name, creativeTab.desc));
318+
setCreativeTab.instructions.add(new VarInsnNode(ALOAD, 0));
319+
setCreativeTab.instructions.add(new InsnNode(ARETURN));
320+
TransformerUtils.setThisParameterName(classNode, setCreativeTab);
321+
TransformerUtils.setParameterName(setCreativeTab, 1, "creativeTab");
322+
classNode.methods.add(setCreativeTab);
323+
}
324+
286325
// Data loss prevention
287326
private static void patchChunkBlockMap(ClassNode classNode) {
288327
classNode.fields.clear();
@@ -651,7 +690,8 @@ private static void patchItem(ClassNode classNode) {
651690
getRegisteringModInsns.add(new InsnNode(ARETURN));
652691
classNode.methods.add(getRegisteringMod);
653692
// getRegisterFLTab
654-
injectGetRegisterFLTab(classNode, "MISCELLANEOUS");
693+
injectGetRegisterFLTab(classNode, "MISCELLANEOUS", true);
694+
injectGetCreativeTab(classNode);
655695
}
656696

657697
private static void patchItemBlock(ClassNode classNode) {
@@ -691,7 +731,40 @@ private static void patchItemBlock(ClassNode classNode) {
691731
getRegisterFLTab.instructions.add(new MethodInsnNode(
692732
INVOKEVIRTUAL, Block, "getRegisterFLTab", "()L" + CreativeTab + ";", false));
693733
getRegisterFLTab.instructions.add(new InsnNode(ARETURN));
734+
TransformerUtils.setThisParameterName(classNode, getRegisterFLTab);
694735
classNode.methods.add(getRegisterFLTab);
736+
// getCreativeTab
737+
MethodNode getCreativeTab = new MethodNode(ASM_API,
738+
ACC_PUBLIC, "getCreativeTab", "()L" + CreativeTab + ";", null, null);
739+
getCreativeTab.instructions.add(new FieldInsnNode(
740+
GETSTATIC, Blocks, "BLOCKS_LIST", "[L" + Block + ";"));
741+
getCreativeTab.instructions.add(new VarInsnNode(ALOAD, 0));
742+
getCreativeTab.instructions.add(new FieldInsnNode(
743+
GETFIELD, ItemBlock, "blockID", "I"));
744+
getCreativeTab.instructions.add(new InsnNode(AALOAD));
745+
getCreativeTab.instructions.add(new MethodInsnNode(
746+
INVOKEVIRTUAL, Block, "getCreativeTab", "()L" + CreativeTab + ";", false));
747+
getCreativeTab.instructions.add(new InsnNode(ARETURN));
748+
TransformerUtils.setThisParameterName(classNode, getCreativeTab);
749+
classNode.methods.add(getCreativeTab);
750+
// setCreativeTab
751+
MethodNode setCreativeTab = new MethodNode(ASM_API,
752+
ACC_PUBLIC, "setCreativeTab", "(L" + CreativeTab + ";)L" + Item + ";", null, null);
753+
setCreativeTab.instructions.add(new FieldInsnNode(
754+
GETSTATIC, Blocks, "BLOCKS_LIST", "[L" + Block + ";"));
755+
setCreativeTab.instructions.add(new VarInsnNode(ALOAD, 0));
756+
setCreativeTab.instructions.add(new FieldInsnNode(
757+
GETFIELD, ItemBlock, "blockID", "I"));
758+
setCreativeTab.instructions.add(new InsnNode(AALOAD));
759+
setCreativeTab.instructions.add(new VarInsnNode(ALOAD, 1));
760+
setCreativeTab.instructions.add(new MethodInsnNode(
761+
INVOKEVIRTUAL, Block, "setCreativeTab", "(L" + CreativeTab + ";)L" + Block + ";", false));
762+
setCreativeTab.instructions.add(new InsnNode(POP));
763+
setCreativeTab.instructions.add(new VarInsnNode(ALOAD, 0));
764+
setCreativeTab.instructions.add(new InsnNode(ARETURN));
765+
TransformerUtils.setThisParameterName(classNode, setCreativeTab);
766+
TransformerUtils.setParameterName(setCreativeTab, 1, "creativeTab");
767+
classNode.methods.add(setCreativeTab);
695768
}
696769

697770
private static void patchBlock(ClassNode classNode) {
@@ -875,7 +948,8 @@ private static void patchBlock(ClassNode classNode) {
875948
TransformerUtils.insertToBeginningOfCode(harvestBlock, prependHarvestBlock);
876949
harvestBlock.instructions.insert(addStat, skipStatIncrease);
877950
// getRegisterFLTab
878-
injectGetRegisterFLTab(classNode, "MISCELLANEOUS");
951+
injectGetRegisterFLTab(classNode, "MISCELLANEOUS", true);
952+
injectGetCreativeTab(classNode);
879953
}
880954

881955
private static void patchBlocks(ClassNode classNode) {

0 commit comments

Comments
 (0)