Package org.spout.api.inventory

Examples of org.spout.api.inventory.Slot


public final class PlayerBlockPlacementHandler extends MessageHandler<PlayerBlockPlacementMessage> {
  private void refreshClient(Player player, Block clickedBlock, BlockFace clickedFace, RepositionManager rm) {
    // refresh the client just in case it assumed something
    player.getNetwork().getSession().send(new BlockChangeMessage(clickedBlock, rm));
    player.getNetwork().getSession().send(new BlockChangeMessage(clickedBlock.translate(clickedFace), rm));
    Slot held = PlayerUtil.getHeldSlot(player);
    if (held != null) {
      held.set(held.get());
    }
  }
View Full Code Here


    RepositionManager rm = player.getNetwork().getRepositionManager();
    RepositionManager rmInverse = rm.getInverse();
    message = message.convert(rmInverse);

    World world = player.getWorld();
    Slot currentSlot = PlayerUtil.getHeldSlot(player);
    if (currentSlot == null) {
      return;
    }
    ItemStack holding = currentSlot.get();
    Material holdingMat = holding == null ? null : holding.getMaterial();

    /*
     * The notch client's packet sending is weird. Here's how it works: If the client is clicking a block not in range, sends a packet with x=-1,y=255,z=-1 If the client is clicking a block in
     * range with an item in hand (id > 255) Sends both the normal block placement packet and a (-1,255,-1) one If the client is placing a block in range with a block in hand, only one normal
     * packet is sent That is how it usually happens. Sometimes it doesn't happen like that. Therefore, a hacky workaround.
     */
    final BlockFace clickedFace = message.getDirection();
    Hunger hunger = player.add(Hunger.class);
    if ((holdingMat instanceof Food && hunger.getHunger() != VanillaData.HUNGER.getDefaultValue()) || holdingMat instanceof Sword || (holdingMat instanceof PotionItem && !((PotionItem) holdingMat).isSplash())) {
      player.get(Living.class).setEatingBlocking(true);
      hunger.setEating(true, currentSlot);
      return;
    }
    if (clickedFace == BlockFace.THIS) {
      // Right clicked air with an item.
      PlayerInteractBlockEvent event = Spout.getEventManager().callEvent(new PlayerInteractBlockEvent(player, null, null, clickedFace, Action.RIGHT_CLICK));

      // May have been changed by the event
      holding = currentSlot.get();
      holdingMat = holding == null ? null : holding.getMaterial();

      if (holdingMat != null) {
        holdingMat.onInteract(player, Action.RIGHT_CLICK);
      }
    } else {
      // TODO: Validate the x/y/z coordinates of the message to check if it is in range of the player
      // This is an anti-hack requirement (else hackers can load far-away chunks and crash the server)

      // Get clicked block and validated face against it was placed
      final Block clickedBlock = world.getBlock(message.getX(), message.getY(), message.getZ());
      final BlockMaterial clickedMaterial = clickedBlock.getMaterial();

      // Perform interaction event
      PlayerInteractBlockEvent interactEvent = Spout.getEventManager().callEvent(new PlayerInteractBlockEvent(player, clickedBlock, clickedBlock.getPosition(), clickedFace, Action.RIGHT_CLICK));

      // May have been changed by the event
      holding = currentSlot.get();
      holdingMat = holding == null ? null : holding.getMaterial();

      // check if the interaction was cancelled by the event
      if (interactEvent.isCancelled()) {
        refreshClient(player, clickedBlock, clickedFace, rm);
        return;
      }
      if (holdingMat != null) {
        holdingMat.onInteract(player, clickedBlock, Action.RIGHT_CLICK, clickedFace);
      }
      clickedMaterial.onInteract(player, clickedBlock, Action.RIGHT_CLICK, clickedFace);

      // If the holding material can be placed, place it
      if (holdingMat instanceof Placeable) {
        Cause<?> cause = new PlayerClickBlockCause(player, clickedBlock);
        short placedData = holding.getData(); // TODO: shouldn't the sub-material deal with this?
        Placeable toPlace = (Placeable) holdingMat;

        final Block placedBlock;
        final BlockFace placedAgainst;
        final boolean placedIsClicked;
        // For snow, tall grass, and the like, place at the clicked block
        final BlockFace clickedAgainst;
        if (!clickedBlock.getMaterial().isPlacementObstacle() && BlockFaces.NESW.contains(clickedFace)) {
          clickedAgainst = BlockFace.BOTTOM;
        } else {
          clickedAgainst = clickedFace.getOpposite();
        }
        if (toPlace.canPlace(clickedBlock, placedData, clickedAgainst, message.getFace(), true, cause)) {
          placedBlock = clickedBlock;
          placedAgainst = clickedAgainst;
          placedIsClicked = true;
        } else {
          placedBlock = clickedBlock.translate(clickedFace);
          placedAgainst = clickedFace.getOpposite();
          placedIsClicked = false;
          if (!toPlace.canPlace(placedBlock, placedData, placedAgainst, message.getFace(), false, cause)) {
            refreshClient(player, clickedBlock, clickedFace, rm);
            return;
          }
        }

        // is the player not solid-colliding with the block?
        if (toPlace instanceof BlockMaterial) {
          BlockMaterial mat = (BlockMaterial) toPlace;
          if (mat.getShape() != null) {
            // TODO: Implement collision models to make this work
            // CollisionModel playerModel = player.getEntity().getCollision();
            // Vector3 offset = playerModel.resolve(mat.getCollisionModel());
            // Vector3 dist = player.getEntity().getPosition().subtract(target.getPosition());
            // if (dist.getX() < offset.getX() || dist.getY() < offset.getY() || dist.getZ() < offset.getZ()) {
            // undoPlacement(player, clickedBlock, alterBlock);
            // return;
            // }

            // For now: simple distance checking
            Point tpos = placedBlock.getPosition();
            if (player.getPhysics().getPosition().distance(tpos) < 0.6) {
              refreshClient(player, clickedBlock, clickedFace, rm);
              return;
            }
            EntityHead head = player.get(EntityHead.class);
            if (head != null && head.getPosition().distance(tpos) < 0.6) {
              refreshClient(player, clickedBlock, clickedFace, rm);
              return;
            }
          }
        }

        // Check if the player can place the block.
        Collection<Protection> protections = VanillaPlugin.getInstance().getEngine().getServiceManager().getRegistration(ProtectionService.class).getProvider().getAllProtections(placedBlock.getPosition());
        for (Protection p : protections) {
          if (p.contains(placedBlock.getPosition()) && !VanillaConfiguration.OPS.isOp(player.getName())) {
            refreshClient(player, clickedBlock, clickedFace, rm);
            player.sendMessage(ChatStyle.DARK_RED + "This area is a protected spawn point!");
            return;
          }
        }
        cause = new PlayerPlacementCause(player, (Material) toPlace, placedBlock);

        // Perform actual placement
        toPlace.onPlacement(placedBlock, placedData, placedAgainst, message.getFace(), placedIsClicked, cause);

        // Play sound
        BlockMaterial material = placedBlock.getMaterial();
        SoundEffect sound;
        if (material instanceof VanillaBlockMaterial) {
          sound = ((VanillaBlockMaterial) material).getStepSound();
        } else {
          sound = SoundEffects.STEP_STONE;
        }
        sound.playGlobal(placedBlock.getPosition(), 0.8f, 0.8f);

        // Remove block from inventory
        if (!PlayerUtil.isCostSuppressed(player) && holdingMat == (currentSlot.get() != null ? currentSlot.get().getMaterial() : null)) {
          currentSlot.addAmount(-1);
        }
      }

      refreshClient(player, clickedBlock, clickedFace, rm);
    }
View Full Code Here

    super.onInteract(entity, block, type, clickedface);
    if (type == Action.RIGHT_CLICK) {
      if (block.isMaterial(VanillaMaterials.DIRT, VanillaMaterials.GRASS)) {
        block.setMaterial(VanillaMaterials.FARMLAND);

        Slot held = PlayerUtil.getHeldSlot(entity);
        if (held != null && !PlayerUtil.isCostSuppressed(entity)) {
          held.addData(1);
        }
      }
    }
  }
View Full Code Here

        // Default fire creation
        Block target = block.translate(clickedface);
        // Default fire placement
        if (VanillaMaterials.FIRE.canCreate(target, (short) 0, cause)) {
          VanillaMaterials.FIRE.onCreate(target, (short) 0, cause);
          Slot held = PlayerUtil.getHeldSlot(entity);
          if (held != null && !PlayerUtil.isCostSuppressed(entity)) {
            held.addData(1);
          }
        }

        // Handle the creation of portals
        Point pos = target.translate(BlockFace.BOTTOM).getPosition();
View Full Code Here

        if (block.getMaterial() instanceof Water && VanillaMaterials.WATER.isSource(block)) {
          break;
        }
      }

      Slot slot = PlayerUtil.getHeldSlot(entity);
      if (slot != null) {
        ItemStack fullbottle = new ItemStack(PotionItem.WATER_BOTTLE, 1);
        entity.get(PlayerInventory.class).getQuickbar().add(fullbottle);
        if (!fullbottle.isEmpty()) {
          entity.get(PlayerInventory.class).getMain().add(fullbottle);
        }
        if (fullbottle.isEmpty() && !PlayerUtil.isCostSuppressed(entity)) {
          slot.addAmount(-1);
        }
      }
    }
  }
View Full Code Here

   */
  public void handleSelectionRemove(Entity entity) {
    if (PlayerUtil.isCostSuppressed(entity)) {
      return;
    }
    Slot slot = PlayerUtil.getHeldSlot(entity);
    if (slot == null || slot.get() == null || !slot.get().isMaterial(this)) {
      return;
    }
    slot.addAmount(-1);
  }
View Full Code Here

    if (type == Action.RIGHT_CLICK) {
      if (block.isMaterial(VanillaMaterials.END_PORTAL_FRAME)) {
        // Default ender eye placement
        if (!VanillaMaterials.END_PORTAL_FRAME.hasEyeOfTheEnder(block)) {
          VanillaMaterials.END_PORTAL_FRAME.setEyeOfTheEnder(block, true);
          Slot held = PlayerUtil.getHeldSlot(entity);
          if (held != null && !PlayerUtil.isCostSuppressed(entity)) {
            held.addAmount(-1);
          }
        }
      }
    }
  }
View Full Code Here

    BlockFace clickedFace = message.getFace();
    Human human = player.get(Human.class);
    if (human == null) {
      return;
    }
    Slot currentSlot = PlayerUtil.getHeldSlot(player);
    if (currentSlot == null) {
      return;
    }
    ItemStack heldItem = currentSlot.get();

    // Don't block protections if dropping an item, silly Notch...
    if (state != PlayerDiggingMessage.STATE_DROP_ITEM && state != PlayerDiggingMessage.STATE_SHOOT_ARROW_EAT_FOOD) {
      Collection<Protection> protections = player.getEngine().getServiceManager().getRegistration(ProtectionService.class).getProvider().getAllProtections(point);
      for (Protection p : protections) {
        if (p.contains(point) && !human.isOp()) {
          player.getNetwork().getSession().send(new BlockChangeMessage(x, y, z, minecraftID, block.getBlockData() & 0xF, rm));
          player.sendMessage(ChatStyle.DARK_RED + "This area is a protected spawn point!");
          return;
        }
      }
    }

    if (state == PlayerDiggingMessage.STATE_DROP_ITEM && x == 0 && y == 0 && z == 0) {
      human.dropItem();
      return;
    }

    boolean isInteractable = true;
    if (blockMaterial == VanillaMaterials.AIR) {
      isInteractable = false;
    }
    // TODO VanillaPlayerInteractBlockEvent and add in Results to it (to
    // more indepthly take away durability).
    switch (state) {
      case PlayerDiggingMessage.STATE_START_DIGGING:
        if (!isProtected(player, rm, x, y, z, block, minecraftID)) {
          final PlayerInteractBlockEvent event = new PlayerInteractBlockEvent(player, block, point, clickedFace, Action.LEFT_CLICK);
          if (player.getEngine().getEventManager().callEvent(event).isCancelled()) {
            if (human.isCreative() || blockMaterial.getHardness() == 0.0f) {
              session.send(new BlockChangeMessage(block, player.getNetwork().getRepositionManager()));
              Sign sign = block.get(Sign.class);
              if (sign != null) {
                session.send(new SignMessage(block.getX(), block.getY(), block.getZ(), sign.getText(), player.getNetwork().getRepositionManager()));
              }
            }
          } else {
            // Perform interactions
            if (heldItem == null) {
              // interacting using fist
            } else if (!isInteractable) {
              // interacting with nothing using item
              heldItem.getMaterial().onInteract(player, Action.LEFT_CLICK);
            } else {
              // interacting with block using item
              heldItem.getMaterial().onInteract(player, block, Action.LEFT_CLICK, clickedFace);
            }

            if (isInteractable) {
              Block neigh = block.translate(clickedFace);
              boolean fire = neigh.getMaterial().equals(VanillaMaterials.FIRE);
              if (fire) {
                // put out fire
                if (VanillaMaterials.FIRE.onDestroy(neigh, new PlayerBreakCause(player, neigh))) {
                  GeneralEffects.RANDOM_FIZZ.playGlobal(block.getPosition());
                }
              } else if (human.isSurvival() && blockMaterial.getHardness() != 0.0f) {
                ItemStack currentItem = PlayerUtil.getHeldSlot(player).get();
                if (currentItem != null) {
                  player.get(Digging.class).startDigging(new Point(w, x, y, z), currentItem.getMaterial());
                } else {
                  player.get(Digging.class).startDigging(new Point(w, x, y, z), VanillaMaterials.AIR);
                }
              } else {
                // insta-break
                if (breakBlock(blockMaterial, block, human, session)) {
                  GeneralEffects.BREAKBLOCK.playGlobal(block.getPosition(), blockMaterial, player);
                }
              }
            }
          }
        }
        break;
      case PlayerDiggingMessage.STATE_CANCEL_DIGGING:
        if (!isProtected(player, rm, x, y, z, block, minecraftID)) {
          player.get(Digging.class).stopDigging(new Point(w, x, y, z), false);
        }
        break;
      case PlayerDiggingMessage.STATE_DONE_DIGGING:
        if (!isProtected(player, rm, x, y, z, block, minecraftID)) {
          Digging diggingComponent = player.get(Digging.class);

          if (!diggingComponent.stopDigging(new Point(w, x, y, z), true) || !isInteractable) {
            if (!diggingComponent.isDigging()) {
              session.send(new BlockChangeMessage(block, player.getNetwork().getRepositionManager()));
              Sign sign = block.get(Sign.class);
              if (sign != null) {
                session.send(new SignMessage(block.getX(), block.getY(), block.getZ(), sign.getText(), player.getNetwork().getRepositionManager()));
              }
            }
            return;
          }

          if (player.getData().get(VanillaData.GAMEMODE).equals(GameMode.SURVIVAL)) {
            long diggingTicks = diggingComponent.getDiggingTicks();
            int damageDone;
            int totalDamage;

            if (heldItem == null) {
              damageDone = ((int) diggingTicks);
            } else {
              damageDone = ((int) diggingTicks * ((VanillaMaterial) heldItem.getMaterial()).getDamage());
            }
            // TODO: Take into account EFFICIENCY enchantment
            // TODO: Digging is slower while under water, on ladders,
            // etc. AQUA_AFFINITY enchantment speeds up underwater
            // digging

            totalDamage = ((int) blockMaterial.getHardness() - damageDone);
            if (totalDamage <= 40) { // Yes, this is a very high
              // allowance - this is because
              // this is only over a single
              // block, and this will spike
              // due to varying latency.
              if (breakBlock(blockMaterial, block, human, session)) {
                GeneralEffects.BREAKBLOCK.playGlobal(block.getPosition(), blockMaterial, player);

                if (heldItem != null && heldItem.getMaterial() instanceof Tool) {
                  Tool tool = (Tool) heldItem.getMaterial();
                  short damage = tool.getDurabilityPenalty(heldItem);
                  if (currentSlot.get().getData() + damage >= tool.getMaxDurability()) {
                    currentSlot.set(null);
                  } else {
                    currentSlot.addData(damage);
                  }
                }
              }
            }
          }
View Full Code Here

    final Vector3f axesAngles = entity.getPhysics().getRotation().getAxesAngleDeg();
    int r = VanillaByteBufUtils.protocolifyYaw(axesAngles.getY());
    int p = VanillaByteBufUtils.protocolifyPitch(axesAngles.getX());

    int item = 0;
    Slot hand = PlayerUtil.getHeldSlot(entity);
    if (hand != null && hand.get() != null) {
      item = hand.get().getMaterial().getId();
    }

    // Spawn message
    messages.add(new PlayerSpawnMessage(id, human.getName(), x, y, z, r, p, item, getSpawnParameters(entity)));
View Full Code Here

  }

  @Override
  public void onInteractBy(Entity entity, Block block, Action type, BlockFace clickedFace) {
    super.onInteractBy(entity, block, type, clickedFace);
    Slot inv = PlayerUtil.getHeldSlot(entity);

    if (inv != null && inv.get() != null && type.equals(Action.RIGHT_CLICK) && clickedFace.equals(BlockFace.TOP) && block.translate(BlockFace.TOP).isMaterial(VanillaMaterials.AIR)) {
      if (inv.get().isMaterial(VanillaMaterials.CARROT)) {
        block.translate(clickedFace).setMaterial(VanillaMaterials.CARROT_CROP);
        if (!(PlayerUtil.isCostSuppressed(entity))) {
          inv.addAmount(-1);
        }
      }

      if (inv.get().isMaterial(VanillaMaterials.POTATO)) {
        block.translate(clickedFace).setMaterial(VanillaMaterials.POTATO_CROP);
        if (!(PlayerUtil.isCostSuppressed(entity))) {
          inv.addAmount(-1);
        }
      }
    }
  }
View Full Code Here

TOP

Related Classes of org.spout.api.inventory.Slot

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.