Skip to content

Commit

Permalink
refactor(coremod): refactor coremods
Browse files Browse the repository at this point in the history
Co-authored-by: lainio24 <[email protected]>
  • Loading branch information
WakelessSloth56 and lainio24 committed Feb 15, 2024
1 parent 9f89e84 commit 3c5e247
Show file tree
Hide file tree
Showing 6 changed files with 458 additions and 341 deletions.
55 changes: 21 additions & 34 deletions src/main/resources/coremods/cross_bow_item.shoot_projectile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
function initializeCoreMod() {
var ASMAPI = Java.type('net.neoforged.coremod.api.ASMAPI');
var Opcodes = Java.type('org.objectweb.asm.Opcodes');
var InsnList = Java.type('org.objectweb.asm.tree.InsnList');
var VarInsnNode = Java.type('org.objectweb.asm.tree.VarInsnNode');
var MethodInsnNode = Java.type('org.objectweb.asm.tree.MethodInsnNode');
Java.type('net.neoforged.coremod.api.ASMAPI').loadFile('coremods/util/utils.js');

return {
'CrossbowItem#shootProjectile': {
Expand All @@ -14,36 +10,27 @@ function initializeCoreMod() {
methodDesc: '(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/InteractionHand;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/item/ItemStack;FZFFF)V'
},
transformer: function (methodNode) {
var toInject = new InsnList();
{
toInject.add(new VarInsnNode(Opcodes.ALOAD, 1));
toInject.add(new VarInsnNode(Opcodes.ALOAD, 3));
toInject.add(new VarInsnNode(Opcodes.ALOAD, 11));
toInject.add(
new MethodInsnNode(
Opcodes.INVOKESTATIC,
'org/auioc/mcmod/arnicalib/mod/coremod/AHCoreModHandler',
'preCrossbowRelease',
'(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/entity/projectile/Projectile;)V',
false
)
);
}
var insns = methodNode.instructions;

var at = methodNode.instructions.get(
methodNode.instructions.indexOf(
ASMAPI.findFirstMethodCall(
methodNode,
ASMAPI.MethodType.VIRTUAL,
'net/minecraft/world/level/Level',
'addFreshEntity',
'(Lnet/minecraft/world/entity/Entity;)Z'
)
) - 2
);
methodNode.instructions.insertBefore(at, toInject);
var injects = [
aLoad(1),
aLoad(3),
aLoad(11),
invokeStatic(
'org/auioc/mcmod/arnicalib/mod/coremod/AHCoreModHandler', 'preCrossbowRelease',
'(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/entity/projectile/Projectile;)V'
)
];

// print(ASMAPI.methodNodeToString(methodNode));
var at = findNodeByR(insns,
isInvoke(
'net/minecraft/world/level/Level', 'addFreshEntity',
'(Lnet/minecraft/world/entity/Entity;)Z'
),
-2);
insns.insertBefore(at, toInsnList(injects));

// printMethodNode(methodNode);
return methodNode;
}
}
Expand All @@ -68,7 +55,7 @@ function initializeCoreMod() {
//_...
Projectile projectile;
//_...
+ org.auioc.mcmod.arnicalib.mod.coremod.AHCoreModHandler.preCrossbowRelease(pShooter, pCrossbowStack, projectile);
+ AHCoreModHandler.preCrossbowRelease(pShooter, pCrossbowStack, projectile);
pLevel.addFreshEntity(projectile);
//_...
}
Expand Down
112 changes: 40 additions & 72 deletions src/main/resources/coremods/fishing_rod_item.use.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
function initializeCoreMod() {
var ASMAPI = Java.type('net.neoforged.coremod.api.ASMAPI');
var Opcodes = Java.type('org.objectweb.asm.Opcodes');
var InsnList = Java.type('org.objectweb.asm.tree.InsnList');
var VarInsnNode = Java.type('org.objectweb.asm.tree.VarInsnNode');
var MethodInsnNode = Java.type('org.objectweb.asm.tree.MethodInsnNode');
var LabelNode = Java.type('org.objectweb.asm.tree.LabelNode');
var LocalVariableNode = Java.type('org.objectweb.asm.tree.LocalVariableNode');

Java.type('net.neoforged.coremod.api.ASMAPI').loadFile('coremods/util/utils.js');

return {
'FishingRodItem#use': {
Expand All @@ -16,76 +12,48 @@ function initializeCoreMod() {
methodDesc: '(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/InteractionHand;)Lnet/minecraft/world/InteractionResultHolder;'
},
transformer: function (methodNode) {
var toInject = new InsnList();
{
var fn_start = new LabelNode();
var fn_end = new LabelNode();

methodNode.localVariables.add(
new LocalVariableNode(
'event',
'Lorg/auioc/mcmod/arnicalib/game/event/server/FishingRodCastEvent;',
null,
fn_start,
fn_end,
7
)
);
var insns = methodNode.instructions;

toInject.add(fn_start);
toInject.add(new VarInsnNode(Opcodes.ALOAD, 2));
toInject.add(new VarInsnNode(Opcodes.ALOAD, 4));
toInject.add(new VarInsnNode(Opcodes.ILOAD, 5));
toInject.add(new VarInsnNode(Opcodes.ILOAD, 6));
toInject.add(
new MethodInsnNode(
Opcodes.INVOKESTATIC,
'org/auioc/mcmod/arnicalib/mod/coremod/AHCoreModHandler',
'preFishingRodCast',
'(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/item/ItemStack;II)Lorg/auioc/mcmod/arnicalib/game/event/server/FishingRodCastEvent;',
false
)
);
toInject.add(new VarInsnNode(Opcodes.ASTORE, 7));
toInject.add(new VarInsnNode(Opcodes.ALOAD, 7));
toInject.add(
new MethodInsnNode(
Opcodes.INVOKEVIRTUAL,
'org/auioc/mcmod/arnicalib/game/event/server/FishingRodCastEvent',
'getSpeedBonus',
'()I',
false
)
);
toInject.add(new VarInsnNode(Opcodes.ISTORE, 5));
toInject.add(new VarInsnNode(Opcodes.ALOAD, 7));
toInject.add(
new MethodInsnNode(
Opcodes.INVOKEVIRTUAL,
'org/auioc/mcmod/arnicalib/game/event/server/FishingRodCastEvent',
'getLuckBonus',
'()I',
false
)
);
toInject.add(new VarInsnNode(Opcodes.ISTORE, 6));
toInject.add(fn_end);
}
var startLabel = new LabelNode();
var endLabel = new LabelNode();

var at = methodNode.instructions.get(
methodNode.instructions.indexOf(
ASMAPI.findFirstInstructionBefore(
methodNode,
Opcodes.NEW,
0
)
) - 1
addLocalVariable(methodNode,
'event', 'Lorg/auioc/mcmod/arnicalib/game/event/server/FishingRodCastEvent;', null,
startLabel, endLabel, 7
);
methodNode.instructions.insertBefore(at, toInject);

methodNode.visitMaxs(14, 8);
var injects = [
startLabel,
aLoad(2),
aLoad(4),
iLoad(5),
iLoad(6),
invokeStatic(
'org/auioc/mcmod/arnicalib/mod/coremod/AHCoreModHandler', 'preFishingRodCast',
'(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/item/ItemStack;II)Lorg/auioc/mcmod/arnicalib/game/event/server/FishingRodCastEvent;'
),
aStore(7),
aLoad(7),
invokeVirtual(
'org/auioc/mcmod/arnicalib/game/event/server/FishingRodCastEvent', 'getSpeedBonus',
'()I'
),
iStore(5),
aLoad(7),
invokeVirtual(
'org/auioc/mcmod/arnicalib/game/event/server/FishingRodCastEvent', 'getLuckBonus',
'()I'
),
iStore(6),
endLabel
];

var at = findNodeByR(insns, isNewObject('net/minecraft/world/entity/projectile/FishingHook'), -1);
insns.insertBefore(at, toInsnList(injects));

setMaxLocals(methodNode, 8);

// print(ASMAPI.methodNodeToString(methodNode));
// printMethodNode(methodNode);
return methodNode;
}
}
Expand Down Expand Up @@ -114,7 +82,7 @@ function initializeCoreMod() {
if (!pLevel.isClientSide) {
int k = EnchantmentHelper.getFishingSpeedBonus(itemstack);
int j = EnchantmentHelper.getFishingLuckBonus(itemstack);
+ var event = org.auioc.mcmod.arnicalib.mod.coremod.AHCoreModHandler.preFishingRodCast(pPlayer, itemstack, k, j);
+ var event = AHCoreModHandler.preFishingRodCast(pPlayer, itemstack, k, j);
+ k = event.getSpeedBonus();
+ j = event.getLuckBonus();
pLevel.addFreshEntity(new FishingHook(pPlayer, pLevel, j, k));
Expand Down
58 changes: 25 additions & 33 deletions src/main/resources/coremods/item_stack.hurt.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
function initializeCoreMod() {
var ASMAPI = Java.type('net.neoforged.coremod.api.ASMAPI');
var Opcodes = Java.type('org.objectweb.asm.Opcodes');
var InsnList = Java.type('org.objectweb.asm.tree.InsnList');
var VarInsnNode = Java.type('org.objectweb.asm.tree.VarInsnNode');
var MethodInsnNode = Java.type('org.objectweb.asm.tree.MethodInsnNode');
Java.type('net.neoforged.coremod.api.ASMAPI').loadFile('coremods/util/utils.js');

return {
'ItemStack#hurt': {
Expand All @@ -14,30 +10,26 @@ function initializeCoreMod() {
methodDesc: '(ILnet/minecraft/util/RandomSource;Lnet/minecraft/server/level/ServerPlayer;)Z'
},
transformer: function (methodNode) {
var toInject = new InsnList();
{
toInject.add(new VarInsnNode(Opcodes.ALOAD, 0));
toInject.add(new VarInsnNode(Opcodes.ILOAD, 1));
toInject.add(new VarInsnNode(Opcodes.ALOAD, 2));
toInject.add(new VarInsnNode(Opcodes.ALOAD, 3));
toInject.add(
new MethodInsnNode(
Opcodes.INVOKESTATIC,
'org/auioc/mcmod/arnicalib/mod/coremod/AHCoreModHandler',
'onItemHurt',
'(Lnet/minecraft/world/item/ItemStack;ILnet/minecraft/util/RandomSource;Lnet/minecraft/server/level/ServerPlayer;)I',
false
)
);
toInject.add(new VarInsnNode(Opcodes.ISTORE, 1));
}
var insns = methodNode.instructions;

var at = ASMAPI.findFirstInstructionAfter(methodNode, Opcodes.ILOAD, 0);
methodNode.instructions.insertBefore(at, toInject);
var injects = [
aLoad(0),
iLoad(1),
aLoad(2),
aLoad(3),
invokeStatic(
'org/auioc/mcmod/arnicalib/mod/coremod/AHCoreModHandler', 'onItemHurt',
'(Lnet/minecraft/world/item/ItemStack;ILnet/minecraft/util/RandomSource;Lnet/minecraft/server/level/ServerPlayer;)I'
),
iStore(1)

methodNode.visitMaxs(6, 7);
];
var at = findNodeBy(insns, isILoad(1), 0);
insns.insertBefore(at, toInsnList(injects));

// print(ASMAPI.methodNodeToString(methodNode));
setMaxLocals(methodNode, 7);

// printMethodNode(methodNode);
return methodNode;
}
}
Expand All @@ -60,7 +52,7 @@ function initializeCoreMod() {
if (!this.isDamageableItem()) {
return false;
} else {
+ pAmount = org.auioc.mcmod.arnicalib.mod.coremod.AHCoreModHandler.onItemHurt(this, pAmount, pRandom, pUser);
+ pAmount = AHCoreModHandler.onItemHurt(this, pAmount, pRandom, pUser);
if (pAmount > 0) {
//_ ...
}
Expand All @@ -71,12 +63,12 @@ function initializeCoreMod() {
//_ ...
L1
LINENUMBER 406 L1
ALOAD 0
ILOAD 1
ALOAD 2
ALOAD 3
INVOKESTATIC org/auioc/mcmod/arnicalib/mod/coremod/AHCoreModHandler.onItemHurt (Lnet/minecraft/world/item/ItemStack;ILnet/minecraft/util/RandomSource;Lnet/minecraft/server/level/ServerPlayer;)I
ISTORE 1
+ ALOAD 0
+ ILOAD 1
+ ALOAD 2
+ ALOAD 3
+ INVOKESTATIC org/auioc/mcmod/arnicalib/mod/coremod/AHCoreModHandler.onItemHurt (Lnet/minecraft/world/item/ItemStack;ILnet/minecraft/util/RandomSource;Lnet/minecraft/server/level/ServerPlayer;)I
+ ISTORE 1
ILOAD 1
IFLE L3
L4
Expand Down
Loading

0 comments on commit 3c5e247

Please sign in to comment.