@Override
  public IMessage onMessage(SecurityModeMessage message, MessageContext context) {
    EntityPlayer player = PacketHandler.getPlayer(context);

    PacketHandler.handlePacket(
        new Thread() {
          @Override
          public void run() {
            if (message.packetType == SecurityPacketType.BLOCK) {
              TileEntity tileEntity = message.coord4D.getTileEntity(player.worldObj);

              if (tileEntity instanceof ISecurityTile) {
                String owner = ((ISecurityTile) tileEntity).getSecurity().getOwner();

                if (owner != null && player.getName().equals(owner)) {
                  ((ISecurityTile) tileEntity).getSecurity().setMode(message.value);
                }
              }
            } else {
              ItemStack stack = player.getHeldItem(message.currentHand);

              if (stack.getItem() instanceof ISecurityItem) {
                ((ISecurityItem) stack.getItem()).setSecurity(stack, message.value);
              }
            }
          }
        },
        player);

    return null;
  }
  @Override
  public boolean placeBlockAt(
      ItemStack stack,
      EntityPlayer player,
      World world,
      int x,
      int y,
      int z,
      int side,
      float hitX,
      float hitY,
      float hitZ,
      int metadata) {
    boolean place =
        super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata);

    if (place) {
      TileEntityEnergyCube tileEntity = (TileEntityEnergyCube) world.getBlockTileEntity(x, y, z);
      tileEntity.tier = ((IEnergyCube) stack.getItem()).getEnergyCubeTier(stack);
      tileEntity.electricityStored = getEnergy(stack);

      ((ISustainedInventory) tileEntity).setInventory(getInventory(stack));

      if (!world.isRemote) {
        PacketHandler.sendPacket(
            Transmission.ALL_CLIENTS,
            new PacketTileEntity()
                .setParams(Object3D.get(tileEntity), tileEntity.getNetworkedData(new ArrayList())));
      }
    }

    return place;
  }
  @Override
  public void handlePacketData(ByteBuf dataStream) {
    super.handlePacketData(dataStream);

    authenticated = dataStream.readBoolean();
    locked = dataStream.readBoolean();
    password = PacketHandler.readString(dataStream);
  }
  @Override
  public IMessage onMessage(DigitalMinerGuiMessage message, MessageContext context) {
    EntityPlayer player = PacketHandler.getPlayer(context);

    if (!player.worldObj.isRemote) {
      World worldServer =
          FMLCommonHandler.instance()
              .getMinecraftServerInstance()
              .worldServerForDimension(message.coord4D.dimensionId);

      if (worldServer != null
          && message.coord4D.getTileEntity(worldServer) instanceof TileEntityDigitalMiner) {
        DigitalMinerGuiMessage.openServerGui(
            message.packetType,
            message.guiType,
            worldServer,
            (EntityPlayerMP) player,
            message.coord4D,
            message.index);
      }
    } else {
      if (message.coord4D.getTileEntity(player.worldObj) instanceof TileEntityDigitalMiner) {
        try {
          if (message.packetType == MinerGuiPacket.CLIENT) {
            FMLCommonHandler.instance()
                .showGuiScreen(
                    DigitalMinerGuiMessage.getGui(
                        message.packetType,
                        message.guiType,
                        player,
                        player.worldObj,
                        message.coord4D.xCoord,
                        message.coord4D.yCoord,
                        message.coord4D.zCoord,
                        -1));
          } else if (message.packetType == MinerGuiPacket.CLIENT_INDEX) {
            FMLCommonHandler.instance()
                .showGuiScreen(
                    DigitalMinerGuiMessage.getGui(
                        message.packetType,
                        message.guiType,
                        player,
                        player.worldObj,
                        message.coord4D.xCoord,
                        message.coord4D.yCoord,
                        message.coord4D.zCoord,
                        message.index));
          }

          player.openContainer.windowId = message.windowId;
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

    return null;
  }
  @Override
  public IMessage onMessage(DropperUseMessage message, MessageContext context) {
    TileEntity tileEntity =
        message.coord4D.getTileEntity(PacketHandler.getPlayer(context).worldObj);

    if (tileEntity instanceof ITankManager) {
      try {
        Object tank = ((ITankManager) tileEntity).getTanks()[message.tankId];

        if (tank != null) {
          DropperHandler.useDropper(PacketHandler.getPlayer(context), tank, message.mouseButton);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    return null;
  }
    @Override
    public void fromBytes(ByteBuf buffer) {
      packetType = PortableTeleporterPacketType.values()[buffer.readInt()];

      if (packetType == PortableTeleporterPacketType.DATA_REQUEST) {
        if (buffer.readBoolean()) {
          frequency =
              new Frequency(PacketHandler.readString(buffer), null).setPublic(buffer.readBoolean());
        }
      } else if (packetType == PortableTeleporterPacketType.DATA_RESPONSE) {
        if (buffer.readBoolean()) {
          frequency =
              new Frequency(PacketHandler.readString(buffer), null).setPublic(buffer.readBoolean());
        }

        status = buffer.readByte();

        int amount = buffer.readInt();

        for (int i = 0; i < amount; i++) {
          publicCache.add(new Frequency(buffer));
        }

        amount = buffer.readInt();

        for (int i = 0; i < amount; i++) {
          privateCache.add(new Frequency(buffer));
        }
      } else if (packetType == PortableTeleporterPacketType.SET_FREQ) {
        frequency =
            new Frequency(PacketHandler.readString(buffer), null).setPublic(buffer.readBoolean());
      } else if (packetType == PortableTeleporterPacketType.DEL_FREQ) {
        frequency =
            new Frequency(PacketHandler.readString(buffer), null).setPublic(buffer.readBoolean());
      } else if (packetType == PortableTeleporterPacketType.TELEPORT) {
        frequency =
            new Frequency(PacketHandler.readString(buffer), null).setPublic(buffer.readBoolean());
      }
    }
  @Override
  public void handlePacketData(ByteBuf dataStream) {
    super.handlePacketData(dataStream);

    if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
      FluidTankTier prevTier = tier;

      tier = FluidTankTier.values()[dataStream.readInt()];
      fluidTank.setCapacity(tier.storage);

      clientActive = dataStream.readBoolean();
      valve = dataStream.readInt();
      editMode = ContainerEditMode.values()[dataStream.readInt()];

      if (valve > 0) {
        valveFluid = FluidRegistry.getFluid(PacketHandler.readString(dataStream));
      } else {
        valveFluid = null;
      }

      if (dataStream.readInt() == 1) {
        fluidTank.setFluid(
            new FluidStack(
                FluidRegistry.getFluid(PacketHandler.readString(dataStream)),
                dataStream.readInt()));
      } else {
        fluidTank.setFluid(null);
      }

      if (prevTier != tier || (updateDelay == 0 && clientActive != isActive)) {
        updateDelay = general.UPDATE_DELAY;
        isActive = clientActive;
        MekanismUtils.updateBlock(worldObj, getPos());
      }
    }
  }
    @Override
    public void toBytes(ByteBuf buffer) {
      buffer.writeInt(packetType.ordinal());

      if (packetType == PortableTeleporterPacketType.DATA_REQUEST) {
        if (frequency != null) {
          buffer.writeBoolean(true);
          PacketHandler.writeString(buffer, frequency.name);
          buffer.writeBoolean(frequency.publicFreq);
        } else {
          buffer.writeBoolean(false);
        }
      } else if (packetType == PortableTeleporterPacketType.DATA_RESPONSE) {
        if (frequency != null) {
          buffer.writeBoolean(true);
          PacketHandler.writeString(buffer, frequency.name);
          buffer.writeBoolean(frequency.publicFreq);
        } else {
          buffer.writeBoolean(false);
        }

        buffer.writeByte(status);

        ArrayList data = new ArrayList();
        data.add(publicCache.size());

        for (Frequency freq : publicCache) {
          freq.write(data);
        }

        data.add(privateCache.size());

        for (Frequency freq : privateCache) {
          freq.write(data);
        }

        PacketHandler.encode(data.toArray(), buffer);
      } else if (packetType == PortableTeleporterPacketType.SET_FREQ) {
        PacketHandler.writeString(buffer, frequency.name);
        buffer.writeBoolean(frequency.publicFreq);
      } else if (packetType == PortableTeleporterPacketType.DEL_FREQ) {
        PacketHandler.writeString(buffer, frequency.name);
        buffer.writeBoolean(frequency.publicFreq);
      } else if (packetType == PortableTeleporterPacketType.TELEPORT) {
        PacketHandler.writeString(buffer, frequency.name);
        buffer.writeBoolean(frequency.publicFreq);
      }
    }
  @Override
  public void read(ByteArrayDataInput dataStream, EntityPlayer player, World world)
      throws Exception {
    ItemStack itemstack = player.getCurrentEquippedItem();

    if (itemstack != null && itemstack.getItem() instanceof ItemPortableTeleporter) {
      ItemPortableTeleporter item = (ItemPortableTeleporter) itemstack.getItem();

      if (item.getStatus(itemstack) == 1) {
        Object3D coords =
            MekanismUtils.getClosestCoords(
                new Teleporter.Code(
                    item.getDigit(itemstack, 0),
                    item.getDigit(itemstack, 1),
                    item.getDigit(itemstack, 2),
                    item.getDigit(itemstack, 3)),
                player);

        item.setEnergy(
            itemstack, item.getEnergy(itemstack) - item.calculateEnergyCost(player, coords));

        if (world.provider.dimensionId != coords.dimensionId) {
          ((EntityPlayerMP) player).travelToDimension(coords.dimensionId);
        }

        ((EntityPlayerMP) player)
            .playerNetServerHandler.setPlayerLocation(
                coords.xCoord + 0.5,
                coords.yCoord + 1,
                coords.zCoord + 0.5,
                player.rotationYaw,
                player.rotationPitch);

        world.playSoundAtEntity(player, "mob.endermen.portal", 1.0F, 1.0F);
        PacketHandler.sendPacket(
            Transmission.CLIENTS_RANGE, new PacketPortalFX().setParams(coords), coords, 40D);
      }
    }
  }
  @Override
  public void onUpdate() {
    ChargeUtils.discharge(2, this);

    if (inventory[0] != null) {
      if (fluidTank.getFluid() != null
          && fluidTank.getFluid().amount >= FluidContainerRegistry.BUCKET_VOLUME) {
        if (FluidContainerRegistry.isEmptyContainer(inventory[0])) {
          ItemStack tempStack =
              FluidContainerRegistry.fillFluidContainer(fluidTank.getFluid(), inventory[0]);

          if (tempStack != null) {
            if (inventory[1] == null) {
              fluidTank.drain(FluidContainerRegistry.BUCKET_VOLUME, true);

              inventory[1] = tempStack;
              inventory[0].stackSize--;

              if (inventory[0].stackSize <= 0) {
                inventory[0] = null;
              }
            } else if (tempStack.isItemEqual(inventory[1])
                && tempStack.getMaxStackSize() > inventory[1].stackSize) {
              fluidTank.drain(FluidContainerRegistry.BUCKET_VOLUME, true);

              inventory[1].stackSize++;
              inventory[0].stackSize--;

              if (inventory[0].stackSize <= 0) {
                inventory[0] = null;
              }
            }
          }
        }
      }
    }

    if (!worldObj.isRemote && worldObj.getWorldTime() % 20 == 0) {
      if (getEnergy() >= 100
          && (fluidTank.getFluid() == null
              || fluidTank.getFluid().amount + FluidContainerRegistry.BUCKET_VOLUME <= 10000)) {
        if (suck(true)) {
          PacketHandler.sendPacket(
              Transmission.CLIENTS_RANGE,
              new PacketTileEntity()
                  .setParams(Object3D.get(this), getNetworkedData(new ArrayList())),
              Object3D.get(this),
              50D);
        }

        clean(true);
      }
    }

    super.onUpdate();

    if (fluidTank.getFluid() != null) {
      for (ForgeDirection orientation : ForgeDirection.VALID_DIRECTIONS) {
        TileEntity tileEntity = Object3D.get(this).getFromSide(orientation).getTileEntity(worldObj);

        if (tileEntity instanceof IFluidHandler) {
          FluidStack toDrain =
              new FluidStack(fluidTank.getFluid(), Math.min(100, fluidTank.getFluidAmount()));
          fluidTank.drain(
              ((IFluidHandler) tileEntity).fill(orientation.getOpposite(), toDrain, true), true);

          if (fluidTank.getFluid() == null || fluidTank.getFluid().amount <= 0) {
            break;
          }
        }
      }
    }
  }
  @Override
  public IMessage onMessage(PortableTeleporterMessage message, MessageContext context) {
    EntityPlayer player = PacketHandler.getPlayer(context);
    ItemStack itemstack = player.getCurrentEquippedItem();
    World world = player.worldObj;

    if (itemstack != null && itemstack.getItem() instanceof ItemPortableTeleporter) {
      ItemPortableTeleporter item = (ItemPortableTeleporter) itemstack.getItem();

      switch (message.packetType) {
        case DATA_REQUEST:
          sendDataResponse(message.frequency, world, player, item, itemstack);
          break;
        case DATA_RESPONSE:
          Mekanism.proxy.handleTeleporterUpdate(message);
          break;
        case SET_FREQ:
          FrequencyManager manager1 =
              getManager(
                  message.frequency.isPublic() ? null : player.getCommandSenderName(), world);
          Frequency toUse = null;

          for (Frequency freq : manager1.getFrequencies()) {
            if (freq.name.equals(message.frequency.name)) {
              toUse = freq;
              break;
            }
          }

          if (toUse == null) {
            toUse =
                new Frequency(message.frequency.name, player.getCommandSenderName())
                    .setPublic(message.frequency.isPublic());
            manager1.addFrequency(toUse);
          }

          sendDataResponse(toUse, world, player, item, itemstack);

          break;
        case DEL_FREQ:
          FrequencyManager manager =
              getManager(
                  message.frequency.isPublic() ? null : player.getCommandSenderName(), world);
          manager.remove(message.frequency.name, player.getCommandSenderName());

          break;
        case TELEPORT:
          FrequencyManager manager2 =
              getManager(
                  message.frequency.isPublic() ? null : player.getCommandSenderName(), world);
          Frequency found = null;

          for (Frequency freq : manager2.getFrequencies()) {
            if (message.frequency.name.equals(freq.name)) {
              found = freq;
              break;
            }
          }

          if (found == null) {
            break;
          }

          Coord4D coords = found.getClosestCoords(new Coord4D(player));

          World teleWorld =
              FMLCommonHandler.instance()
                  .getMinecraftServerInstance()
                  .worldServerForDimension(coords.dimensionId);
          TileEntityTeleporter teleporter = (TileEntityTeleporter) coords.getTileEntity(teleWorld);

          if (teleporter != null) {
            try {
              teleporter.didTeleport.add(player);
              teleporter.teleDelay = 5;

              item.setEnergy(
                  itemstack, item.getEnergy(itemstack) - item.calculateEnergyCost(player, coords));

              if (player instanceof EntityPlayerMP) {
                MekanismUtils.setPrivateValue(
                    ((EntityPlayerMP) player).playerNetServerHandler,
                    0,
                    NetHandlerPlayServer.class,
                    ObfuscatedNames.NetHandlerPlayServer_floatingTickCount);
              }

              player.closeScreen();

              Mekanism.packetHandler.sendToAllAround(
                  new PortalFXMessage(new Coord4D(player)), coords.getTargetPoint(40D));
              TileEntityTeleporter.teleportPlayerTo((EntityPlayerMP) player, coords, teleporter);

              world.playSoundAtEntity(player, "mob.endermen.portal", 1.0F, 1.0F);
              Mekanism.packetHandler.sendToReceivers(
                  new PortalFXMessage(coords), new Range4D(coords));
            } catch (Exception e) {
            }
          }

          break;
      }
    }

    return null;
  }
示例#12
0
 protected void read(ByteBuf dataStream) {
   name = PacketHandler.readString(dataStream);
   owner = PacketHandler.readString(dataStream);
   publicFreq = dataStream.readBoolean();
 }