Ejemplo n.º 1
0
 private boolean handleServerSideCustomPacket(
     C17PacketCustomPayload msg, ChannelHandlerContext context) {
   if (state == ConnectionState.AWAITING_HANDSHAKE) {
     synchronized (this) { // guard from other threads changing the state on us
       if (state == ConnectionState.AWAITING_HANDSHAKE) {
         state = ConnectionState.HANDSHAKING;
       }
     }
   }
   String channelName = msg.getChannelName();
   if ("FML|HS".equals(channelName)
       || "REGISTER".equals(channelName)
       || "UNREGISTER".equals(channelName)) {
     FMLProxyPacket proxy = new FMLProxyPacket(msg);
     proxy.setDispatcher(this);
     handshakeChannel.writeInbound(proxy);
     for (Object push : handshakeChannel.inboundMessages()) {
       List<FMLProxyPacket> messageResult =
           FMLNetworkHandler.forwardHandshake(
               (FMLMessage.CompleteHandshake) push, this, Side.SERVER);
       for (FMLProxyPacket result : messageResult) {
         result.setTarget(Side.SERVER);
         result.payload().resetReaderIndex();
         context.fireChannelRead(result);
       }
     }
     handshakeChannel.inboundMessages().clear();
     return true;
   } else if (NetworkRegistry.INSTANCE.hasChannel(channelName, Side.SERVER)) {
     FMLProxyPacket proxy = new FMLProxyPacket(msg);
     proxy.setDispatcher(this);
     context.fireChannelRead(proxy);
     return true;
   }
   return false;
 }
  /**
   * @author zml
   *     <p>Purpose: replace the logic used for command blocks to make functional
   * @param ci callback
   * @param packetIn method param
   */
  @Inject(
      method = "processVanilla250Packet",
      at =
          @At(
              value = "INVOKE",
              shift = At.Shift.AFTER,
              target =
                  "net/minecraft/network/PacketThreadUtil.checkThreadAndEnqueue(Lnet/minecraft/network/Packet;"
                      + "Lnet/minecraft/network/INetHandler;Lnet/minecraft/util/IThreadListener;)V"),
      cancellable = true)
  public void processCommandBlock(C17PacketCustomPayload packetIn, CallbackInfo ci) {
    if ("MC|AdvCdm".equals(packetIn.getChannelName())) {
      PacketBuffer packetbuffer;
      try {
        if (!this.serverController.isCommandBlockEnabled()) {
          this.playerEntity.addChatMessage(
              new ChatComponentTranslation("advMode.notEnabled", new Object[0]));
          // Sponge: Check permissions for command block usage TODO: Maybe throw an event instead?
          // } else if (this.playerEntity.canCommandSenderUseCommand(2, "") &&
          // this.playerEntity.capabilities.isCreativeMode) {
        } else {
          packetbuffer = packetIn.getBufferData();

          try {
            byte b0 = packetbuffer.readByte();
            CommandBlockLogic commandblocklogic = null;

            String permissionCheck = null; // Sponge
            if (b0 == 0) {
              TileEntity tileentity =
                  this.playerEntity.worldObj.getTileEntity(
                      new BlockPos(
                          packetbuffer.readInt(), packetbuffer.readInt(), packetbuffer.readInt()));

              if (tileentity instanceof TileEntityCommandBlock) {
                commandblocklogic = ((TileEntityCommandBlock) tileentity).getCommandBlockLogic();
                permissionCheck =
                    "minecraft.commandblock.edit.block."
                        + commandblocklogic.getCommandSenderName(); // Sponge
              }
            } else if (b0 == 1) {
              Entity entity = this.playerEntity.worldObj.getEntityByID(packetbuffer.readInt());

              if (entity instanceof EntityMinecartCommandBlock) {
                commandblocklogic = ((EntityMinecartCommandBlock) entity).getCommandBlockLogic();
                permissionCheck =
                    "minecraft.commandblock.edit.minecart."
                        + commandblocklogic.getCommandSenderName(); // Sponge
              }
              // Sponge begin
            } else {
              throw new IllegalArgumentException("Unknown command block type!");
            }
            Player spongePlayer = ((Player) this.playerEntity);
            if (permissionCheck == null || !spongePlayer.hasPermission(permissionCheck)) {
              spongePlayer.sendMessage(
                  t("You do not have permission to edit this command block!")
                      .builder()
                      .color(TextColors.RED)
                      .build());
              return;
              // Sponge end
            }

            String s1 = packetbuffer.readStringFromBuffer(packetbuffer.readableBytes());
            boolean flag = packetbuffer.readBoolean();

            if (commandblocklogic != null) {
              commandblocklogic.setCommand(s1);
              commandblocklogic.setTrackOutput(flag);

              if (!flag) {
                commandblocklogic.setLastOutput((IChatComponent) null);
              }

              commandblocklogic.func_145756_e();
              this.playerEntity.addChatMessage(
                  new ChatComponentTranslation("advMode.setCommand.success", new Object[] {s1}));
            }
          } catch (Exception exception1) {
            logger.error("Couldn\'t set command block", exception1);
          } finally {
            packetbuffer.release();
          }
          /*} else { // Sponge: Give more accurate no permission message
          this.playerEntity.addChatMessage(new ChatComponentTranslation("advMode.notAllowed", new Object[0]));*/
        }
      } finally {
        ci.cancel();
      }
    }
  }
  // patch method
  @Overwrite
  public void processVanilla250Packet(C17PacketCustomPayload p_147349_1_) {
    PacketBuffer packetbuffer;
    ItemStack itemstack;
    ItemStack itemstack1;

    if ("MC|BEdit".equals(p_147349_1_.func_149559_c())) {
      packetbuffer = new PacketBuffer(Unpooled.wrappedBuffer(p_147349_1_.func_149558_e()));

      try {
        itemstack = packetbuffer.readItemStackFromBuffer();

        if (itemstack == null) {
          return;
        }

        if (!ItemWritableBook.func_150930_a(itemstack.getTagCompound())) {
          throw new IOException("Invalid book tag!");
        }

        itemstack1 = playerEntity.inventory.getCurrentItem();

        if (itemstack1 != null) {
          if (itemstack.getItem() == Items.writable_book
              && itemstack.getItem() == itemstack1.getItem()) {
            itemstack1.setTagInfo("pages", itemstack.getTagCompound().getTagList("pages", 8));
          }

          return;
        }
      } catch (Exception exception4) {
        logger.error("Couldn\'t handle book info", exception4);
        return;
      } finally {
        packetbuffer.release();
      }

      return;
    } else if ("MC|BSign".equals(p_147349_1_.func_149559_c())) {
      packetbuffer = new PacketBuffer(Unpooled.wrappedBuffer(p_147349_1_.func_149558_e()));

      try {
        itemstack = packetbuffer.readItemStackFromBuffer();

        if (itemstack != null) {
          if (!ItemEditableBook.validBookTagContents(itemstack.getTagCompound())) {
            throw new IOException("Invalid book tag!");
          }

          itemstack1 = playerEntity.inventory.getCurrentItem();

          if (itemstack1 == null) {
            return;
          }

          if (itemstack.getItem() == Items.written_book
              && itemstack1.getItem() == Items.writable_book) {
            itemstack1.setTagInfo("author", new NBTTagString(playerEntity.getCommandSenderName()));
            itemstack1.setTagInfo(
                "title", new NBTTagString(itemstack.getTagCompound().getString("title")));
            itemstack1.setTagInfo("pages", itemstack.getTagCompound().getTagList("pages", 8));
            itemstack1.func_150996_a(Items.written_book);
          }

          return;
        }
      } catch (Exception exception3) {
        logger.error("Couldn\'t sign book", exception3);
        return;
      } finally {
        packetbuffer.release();
      }

      return;
    } else {
      DataInputStream datainputstream;
      int i;

      if ("MC|TrSel".equals(p_147349_1_.func_149559_c())) {
        try {
          datainputstream =
              new DataInputStream(new ByteArrayInputStream(p_147349_1_.func_149558_e()));
          i = datainputstream.readInt();
          Container container = playerEntity.openContainer;

          if (container instanceof ContainerMerchant) {
            ((ContainerMerchant) container).setCurrentRecipeIndex(i);
          }
        } catch (Exception exception2) {
          logger.error("Couldn\'t select trade", exception2);
        }
      } else if ("MC|AdvCdm".equals(p_147349_1_.func_149559_c())) {
        if (!serverController.isCommandBlockEnabled()) {
          playerEntity.addChatMessage(
              new ChatComponentTranslation("advMode.notEnabled", new Object[0]));
        } else if (PermissionManager.checkPermission(playerEntity, "mc.cmdblocks")
            && playerEntity.capabilities.isCreativeMode) {
          packetbuffer = new PacketBuffer(Unpooled.wrappedBuffer(p_147349_1_.func_149558_e()));

          try {
            byte b0 = packetbuffer.readByte();
            CommandBlockLogic commandblocklogic = null;

            if (b0 == 0) {
              TileEntity tileentity =
                  playerEntity.worldObj.getTileEntity(
                      packetbuffer.readInt(), packetbuffer.readInt(), packetbuffer.readInt());

              if (tileentity instanceof TileEntityCommandBlock) {
                commandblocklogic = ((TileEntityCommandBlock) tileentity).func_145993_a();
              }
            } else if (b0 == 1) {
              Entity entity = playerEntity.worldObj.getEntityByID(packetbuffer.readInt());

              if (entity instanceof EntityMinecartCommandBlock) {
                commandblocklogic = ((EntityMinecartCommandBlock) entity).func_145822_e();
              }
            }

            String s1 = packetbuffer.readStringFromBuffer(packetbuffer.readableBytes());

            if (commandblocklogic != null) {
              commandblocklogic.func_145752_a(s1);
              commandblocklogic.func_145756_e();
              playerEntity.addChatMessage(
                  new ChatComponentTranslation("advMode.setCommand.success", new Object[] {s1}));
            }
          } catch (Exception exception1) {
            logger.error("Couldn\'t set command block", exception1);
          } finally {
            packetbuffer.release();
          }
        } else {
          playerEntity.addChatMessage(
              new ChatComponentTranslation("advMode.notAllowed", new Object[0]));
        }
      } else if ("MC|Beacon".equals(p_147349_1_.func_149559_c())) {
        if (playerEntity.openContainer instanceof ContainerBeacon) {
          try {
            datainputstream =
                new DataInputStream(new ByteArrayInputStream(p_147349_1_.func_149558_e()));
            i = datainputstream.readInt();
            int j = datainputstream.readInt();
            ContainerBeacon containerbeacon = (ContainerBeacon) playerEntity.openContainer;
            Slot slot = containerbeacon.getSlot(0);

            if (slot.getHasStack()) {
              slot.decrStackSize(1);
              TileEntityBeacon tileentitybeacon = containerbeacon.func_148327_e();
              tileentitybeacon.setPrimaryEffect(i);
              tileentitybeacon.setSecondaryEffect(j);
              tileentitybeacon.markDirty();
            }
          } catch (Exception exception) {
            logger.error("Couldn\'t set beacon", exception);
          }
        }
      } else if ("MC|ItemName".equals(p_147349_1_.func_149559_c())
          && playerEntity.openContainer instanceof ContainerRepair) {
        ContainerRepair containerrepair = (ContainerRepair) playerEntity.openContainer;

        if (p_147349_1_.func_149558_e() != null && p_147349_1_.func_149558_e().length >= 1) {
          String s =
              ChatAllowedCharacters.filerAllowedCharacters(
                  new String(p_147349_1_.func_149558_e(), Charsets.UTF_8));

          if (s.length() <= 30) {
            containerrepair.updateItemName(s);
          }
        } else {
          containerrepair.updateItemName("");
        }
      }
    }
  }