Browse Source

hook for modifying crafting

Kajetan Johannes Hammerle 3 years ago
parent
commit
be98497ef5

+ 1 - 0
src/main/java/me/kcm/BlockHarvest.java

@@ -9,6 +9,7 @@ import net.minecraft.tileentity.TileEntity;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.world.server.ServerWorld;
 
+@FunctionalInterface
 public interface BlockHarvest {
     public List<ItemStack> onBlockHarvest(BlockState state, ServerWorld w, BlockPos pos, @Nullable TileEntity tileEnt, @Nullable Entity ent, @Nullable ItemStack stack);
 }

+ 11 - 0
src/main/java/me/kcm/Craft.java

@@ -0,0 +1,11 @@
+package me.kcm;
+
+import net.minecraft.entity.player.PlayerEntity;
+import net.minecraft.inventory.CraftResultInventory;
+import net.minecraft.inventory.CraftingInventory;
+import net.minecraft.world.World;
+
+@FunctionalInterface
+public interface Craft {
+    public void onCraft(int id, World w, PlayerEntity p, CraftingInventory cInv, CraftResultInventory slot);
+}

+ 13 - 0
src/main/java/me/kcm/Hooks.java

@@ -6,16 +6,21 @@ import java.util.function.Consumer;
 import javax.annotation.Nullable;
 import net.minecraft.block.BlockState;
 import net.minecraft.entity.Entity;
+import net.minecraft.entity.player.PlayerEntity;
+import net.minecraft.inventory.CraftResultInventory;
+import net.minecraft.inventory.CraftingInventory;
 import net.minecraft.item.ItemStack;
 import net.minecraft.server.dedicated.DedicatedPlayerList;
 import net.minecraft.server.dedicated.DedicatedServer;
 import net.minecraft.tileentity.TileEntity;
 import net.minecraft.util.math.BlockPos;
+import net.minecraft.world.World;
 import net.minecraft.world.server.ServerWorld;
 
 public class Hooks {
     private static Consumer<DedicatedServer> playerListFunction = null;
     private static BlockHarvest blockHarvest = (state, w, pos, tileEnt, ent, stack) -> Collections.EMPTY_LIST;
+    private static Craft craft = (id, w, p, cInv, slot) -> {};
 
     public static void setPlayerListFunction(Consumer<DedicatedServer> c) {
         playerListFunction = c;
@@ -37,6 +42,10 @@ public class Hooks {
     public static void setBlockHarvest(BlockHarvest c) {
         blockHarvest = c;
     }
+    
+    public static void setCraft(Craft c) {
+        craft = c;
+    }
 
     public static List<ItemStack> getDropsA(BlockState state, ServerWorld w, BlockPos pos, @Nullable TileEntity tileEnt) {
         return blockHarvest.onBlockHarvest(state, w, pos, tileEnt, null, null);
@@ -45,4 +54,8 @@ public class Hooks {
     public static List<ItemStack> getDropsB(BlockState state, ServerWorld w, BlockPos pos, @Nullable TileEntity tileEnt, @Nullable Entity ent, ItemStack stack) {
         return blockHarvest.onBlockHarvest(state, w, pos, tileEnt, ent, stack);
     }
+    
+    public static void onCraft(int id, World w, PlayerEntity p, CraftingInventory cInv, CraftResultInventory slot) {
+        craft.onCraft(id, w, p, cInv, slot);
+    }
 }

+ 2 - 1
src/main/resources/META-INF/coremods.json

@@ -1,4 +1,5 @@
 {
     "Dedicated Server Transformer": "class_transformer/playerlist.js",
-    "Block Transformer": "class_transformer/block.js"
+    "Block Transformer": "class_transformer/block.js",
+    "Crafting Transformer": "class_transformer/craft.js"
 }

+ 47 - 0
src/main/resources/class_transformer/craft.js

@@ -0,0 +1,47 @@
+var transformerName = "Dedicated Server Transformer";
+var ASMAPI = Java.type('net.minecraftforge.coremod.api.ASMAPI');
+var Opcodes = Java.type('org.objectweb.asm.Opcodes');
+var VarInsnNode = Java.type("org.objectweb.asm.tree.VarInsnNode");
+var MethodInsnNode = Java.type("org.objectweb.asm.tree.MethodInsnNode");
+var LineNumberNode = Java.type("org.objectweb.asm.tree.LineNumberNode");
+var FieldInsnNode = Java.type("org.objectweb.asm.tree.FieldInsnNode");
+var InsnNode = Java.type("org.objectweb.asm.tree.InsnNode");
+
+function initializeCoreMod() {
+    return {
+        transformerName: {
+            'target': {
+                'type': 'CLASS',
+                'name': 'net.minecraft.inventory.container.WorkbenchContainer'
+            },
+            'transformer': function (classNode) {
+                var methods = classNode.methods;
+
+                var targetMethodName = ASMAPI.mapMethod("func_217066_a"); // no name set yet
+                for (var i in methods) {
+                    var method = methods[i];
+                    if (method.name.equals(targetMethodName)) {
+                        transform(method);
+                        break;
+                    }
+                }
+                return classNode;
+            }
+        }
+    };
+}
+
+function transform(method) {
+    var instrList = method.instructions;
+    instrList.clear();
+    instrList.add(new VarInsnNode(Opcodes.ILOAD, 0));
+    instrList.add(new VarInsnNode(Opcodes.ALOAD, 1));
+    instrList.add(new VarInsnNode(Opcodes.ALOAD, 2));
+    instrList.add(new VarInsnNode(Opcodes.ALOAD, 3));
+    instrList.add(new VarInsnNode(Opcodes.ALOAD, 4));
+    instrList.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "me/kcm/Hooks",
+            "onCraft",
+            "(ILnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/inventory/CraftingInventory;Lnet/minecraft/inventory/CraftResultInventory;)V",
+            false));
+    instrList.add(new InsnNode(Opcodes.RETURN));
+}