mrkirby153

Untitled

Sep 10th, 2015
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1.     private void transformEntityPlayerSP(ClassNode entityPlayerSPClass, boolean obfuscated) {
  2.         final String ADD_CHAT_COMPONENT_MESSAGE = (obfuscated) ? "b" : "addChatComponentMessage";
  3.         final String ADD_CHAT_COMPONENT_DESCRIPTOR = (obfuscated) ? "(Lfj;)V" : "(Lnet/minecraft/util/IChatComponent;)V";
  4.         // Find target node
  5.         MethodNode targetMethod = findTargetNode(entityPlayerSPClass, ADD_CHAT_COMPONENT_MESSAGE, ADD_CHAT_COMPONENT_DESCRIPTOR);
  6.         if(targetMethod == null)
  7.             return;
  8.         AbstractInsnNode targetNode = null;
  9.         for(AbstractInsnNode instruction : targetMethod.instructions.toArray()){
  10.             if(instruction.getOpcode() == ALOAD){
  11.                 if(((VarInsnNode) instruction).var == 0 && instruction.getNext().getOpcode() == GETFIELD){
  12.                     targetNode = instruction;
  13.                     break;
  14.                 }
  15.             }
  16.         }
  17.         if(targetNode == null)
  18.             return;
  19.         /*
  20.         Replace:
  21.         this.mc.ingameGui.getChatGUI().printChatMessage(p_146105_1_);
  22.         WITH:
  23.         if(!ChatHandler.hasHandledChat(p_146105_1_){
  24.             this.mc.ingameGui.getChatGUI().printChatMessage();
  25.          }
  26.          BYTECODE:
  27.          REPLACE:
  28.             mv.visitVarInsn(ALOAD, 0);
  29.             mv.visitFieldInsn(GETFIELD, "net/minecraft/client/entity/EntityPlayerSP", "mc", "Lnet/minecraft/client/Minecraft;");
  30.             v.visitFieldInsn(GETFIELD, "net/minecraft/client/Minecraft", "ingameGUI", "Lnet/minecraft/client/gui/GuiIngame;");
  31.             mv.visitMethodInsn(INVOKEVIRTUAL, "net/minecraft/client/gui/GuiIngame", "getChatGUI", "()Lnet/minecraft/client/gui/GuiNewChat;", false);
  32.             mv.visitVarInsn(ALOAD, 1);
  33.             mv.visitMethodInsn(INVOKEVIRTUAL, "net/minecraft/client/gui/GuiNewChat", "printChatMessage", "(Lnet/minecraft/util/IChatComponent;)V", false);
  34.          WIT
  35.             ALOAD 1
  36.             INVOKESTATIC me/mrkirby153/AntiChatSpam/regex/ChatHandler.hasHandledChat (Lnet/minecraft/util/IChatComponent;)Z
  37.             IFEQ newLabelNode
  38.             newLableNode
  39.             RETURN
  40.             mv.visitVarInsn(ALOAD, 0);
  41.             mv.visitFieldInsn(GETFIELD, "net/minecraft/client/entity/EntityPlayerSP", "mc", "Lnet/minecraft/client/Minecraft;");
  42.             v.visitFieldInsn(GETFIELD, "net/minecraft/client/Minecraft", "ingameGUI", "Lnet/minecraft/client/gui/GuiIngame;");
  43.             mv.visitMethodInsn(INVOKEVIRTUAL, "net/minecraft/client/gui/GuiIngame", "getChatGUI", "()Lnet/minecraft/client/gui/GuiNewChat;", false);
  44.             mv.visitVarInsn(ALOAD, 1);
  45.             mv.visitMethodInsn(INVOKEVIRTUAL, "net/minecraft/client/gui/GuiNewChat", "printChatMessage", "(Lnet/minecraft/util/IChatComponent;)V", false);
  46.             LABEL newLabelNode
  47.          */
  48.  
  49.         AbstractInsnNode insertPoint = targetNode.getPrevious();
  50.         LabelNode returnLabel = new LabelNode();
  51.         InsnList toInsert = new InsnList();
  52.         Type chatHandlerType = Type.getType(ChatHandler.class);
  53.         String handlerDescriptor;
  54.         String thingsDescriptor;
  55.         try {
  56.             handlerDescriptor = Type.getMethodDescriptor(ChatHandler.class.getMethod("hasHandledChat", IChatComponent.class));
  57.             thingsDescriptor = Type.getMethodDescriptor(ChatHandler.class.getMethod("doThings"));
  58.         } catch (Exception e){
  59.             logger.catching(e);
  60.             return;
  61.         }
  62.         toInsert.add(new VarInsnNode(ALOAD, 1));
  63.         toInsert.add(new MethodInsnNode(INVOKESTATIC, chatHandlerType.getInternalName(), "hasHandledChat", handlerDescriptor, false));
  64.         toInsert.add(new JumpInsnNode(IFNE, returnLabel));
  65.         toInsert.add(returnLabel);
  66.         toInsert.add(new InsnNode(RETURN));
  67.         targetMethod.instructions.insert(insertPoint, toInsert);
  68.  
  69. //        AbstractInsnNode invokeVirtualPrint = targetNode;
  70. //        for(int i = 0; i < 5; i++){
  71. //            invokeVirtualPrint = invokeVirtualPrint.getNext();
  72. //        }
  73. //        LabelNode newLabelNode = new LabelNode();
  74. //        InsnList toInsert = new InsnList();
  75. //        toInsert.add(new VarInsnNode(ALOAD, 1));
  76. //        final String HAS_HANDLED_CHAT_DESCRIPTOR = (obfuscated)? "(Lfj;)V" : "(Lnet/minecraft/util/IChatComponent;)V";
  77. //        toInsert.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(ChatHandler.class), "hasHandledChat", HAS_HANDLED_CHAT_DESCRIPTOR, false));
  78. //        toInsert.add(new JumpInsnNode(IFNE, newLabelNode));
  79. //
  80. //        targetMethod.instructions.insertBefore(targetNode, toInsert);
  81. //        targetMethod.instructions.insert(invokeVirtualPrint, newLabelNode);
  82. //
  83.         for(AbstractInsnNode n : targetMethod.instructions.toArray()){
  84.             System.out.println(n.getOpcode());
  85.         }
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment