Skip to content

Commit

Permalink
Optimized Core Mod code
Browse files Browse the repository at this point in the history
  • Loading branch information
MorningOriens authored and Protoxy22 committed Aug 5, 2022
1 parent 764e047 commit 58b88b1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 257 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/modularwarfare/InjectMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.modularwarfare;

import com.modularwarfare.client.ClientRenderHooks;
import net.minecraft.client.Minecraft;

public class InjectMethods {

public static boolean isReloading() {
return ClientRenderHooks.getAnimMachine(Minecraft.getMinecraft().player).reloading;
}

}
67 changes: 67 additions & 0 deletions src/main/java/com/modularwarfare/mixin/ClassTransformer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.modularwarfare.mixin;

import net.minecraft.launchwrapper.IClassTransformer;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.*;

import static org.objectweb.asm.Opcodes.*;

public class ClassTransformer implements IClassTransformer {

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {

if (transformedName.equals("net.minecraft.client.Minecraft") || transformedName.equals("bib")) {

ClassReader cr = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);

for (MethodNode method : cn.methods) {

if (!(method.name.equals("processKeyBinds") || method.name.equals("func_184117_aA") || method.name.equals("aE"))) continue;

for (AbstractInsnNode ain : method.instructions.toArray()) {
if (!(ain.getOpcode() == INVOKEVIRTUAL)) continue;

MethodInsnNode min = (MethodInsnNode) ain;

if (min.name.equals("onHotbarSelected") || min.name.equals("func_175260_a") || min.name.equals("a")) {

LabelNode jumpLabel = null;

for (int i = 1;; i++) {

AbstractInsnNode insnNode = method.instructions.get(method.instructions.indexOf(min) + i);

if (insnNode.getOpcode() == GOTO) {
jumpLabel = ((JumpInsnNode) insnNode).label;
}

if(jumpLabel != null && insnNode.getOpcode() == ALOAD) {
InsnList injectInsnList = new InsnList();
injectInsnList.add(new MethodInsnNode(INVOKESTATIC, "com/modularwarfare/InjectMethods", "isReloading", "()Z", false));
injectInsnList.add(new JumpInsnNode(IFNE, jumpLabel));
method.instructions.insertBefore(insnNode, injectInsnList);
break;
}

}

}

}

}

ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return cw.toByteArray();

}

return basicClass;
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/modularwarfare/mixin/MixinCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MixinCore implements IFMLLoadingPlugin {

@Override
public String[] getASMTransformerClass() {
return new String[0];
return new String[] { ClassTransformer.class.getName() };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,25 @@
package com.modularwarfare.mixin.client;

import com.modularwarfare.ModularWarfare;
import com.modularwarfare.client.ClientRenderHooks;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(InventoryPlayer.class)
public abstract class MixinInventoryPlayer {

@Shadow
public int currentItem;

@Shadow
@Final
public NonNullList<ItemStack> mainInventory;

@Shadow
public EntityPlayer player;

@Shadow
public abstract ItemStack getStackInSlot(int index);

/**
* @author
*/
@Overwrite
@SideOnly(Side.CLIENT)
public void changeCurrentItem(int direction) {
@Inject(method = "changeCurrentItem", at = @At("HEAD"), cancellable = true)
public void inject_changeCurrentItem(CallbackInfo ci) {
if(ClientRenderHooks.getAnimMachine(player).reloading){
return;
}
if (direction > 0)
{
direction = 1;
}

if (direction < 0)
{
direction = -1;
}

for (this.currentItem -= direction; this.currentItem < 0; this.currentItem += 9)
{
;
}

while (this.currentItem >= 9)
{
this.currentItem -= 9;
ci.cancel();
}
}


}
212 changes: 0 additions & 212 deletions src/main/java/com/modularwarfare/mixin/client/MixinMinecraft.java

This file was deleted.

0 comments on commit 58b88b1

Please sign in to comment.