Esempio n. 1
0
  @Override
  public Hostage getHostage(Entity entity) {
    Hostage h;
    try {
      h = (Hostage) ((CraftEntity) entity).getHandle();
    } catch (ClassCastException ex) {
      // Caused by baby villager or a non-Hostage Villager.
      plugin
          .debug()
          .log(
              "onHostageInteract() ClassCastException: most likely "
                  + "caused by a baby villager or a Villager that is not a Hostage.");

      Villager v = (Villager) entity;
      World world = ((CraftWorld) v.getWorld()).getHandle();

      CraftHostage hostage = new CraftHostage(world, v.getProfession().getId());
      hostage.setLocation(v.getLocation());

      world.removeEntity(((CraftEntity) entity).getHandle());
      world.addEntity(hostage);

      hostage.setHealth((float) v.getHealth());
      hostage.setProfessionType(v.getProfession());
      hostage.setCustomName(v.getCustomName());

      return hostage;
    }
    return h;
  }
  // returns false, if the player wasn't able to hire this villager
  @SuppressWarnings("deprecation")
  // because of player.updateInventory()
  private boolean handleHireOtherVillager(Player player, Villager villager) {
    // hire him if holding his hiring item
    ItemStack inHand = player.getItemInHand();
    if (Settings.isHireItem(inHand)) {
      Inventory inventory = player.getInventory();
      // check if the player has enough of those hiring items
      int costs = Settings.hireOtherVillagersCosts;
      if (costs > 0) {
        if (Utils.hasInventoryItemsAtLeast(
            inventory, Settings.hireItem, (short) Settings.hireItemData, costs)) {
          Log.debug("  Villager hiring: the player has the needed amount of hiring items");
          int inHandAmount = inHand.getAmount();
          int remaining = inHandAmount - costs;
          Log.debug(
              "  Villager hiring: in hand="
                  + inHandAmount
                  + " costs="
                  + costs
                  + " remaining="
                  + remaining);
          if (remaining > 0) {
            inHand.setAmount(remaining);
          } else { // remaining <= 0
            player.setItemInHand(null); // remove item in hand
            if (remaining < 0) {
              // remove remaining costs from inventory
              Utils.removeItemsFromInventory(
                  inventory, Settings.hireItem, (short) Settings.hireItemData, -remaining);
            }
          }
        } else {
          Utils.sendMessage(player, Settings.msgCantHire);
          return false;
        }
      }

      // give player the creation item
      ItemStack creationItem = Settings.createCreationItem();
      Map<Integer, ItemStack> remaining = inventory.addItem(creationItem);
      if (!remaining.isEmpty()) {
        villager.getWorld().dropItem(villager.getLocation(), creationItem);
      }

      // remove the entity:
      villager.remove();

      // update client's inventory
      player.updateInventory();

      Utils.sendMessage(player, Settings.msgHired);
      return true;
    } else {
      Utils.sendMessage(
          player,
          Settings.msgVillagerForHire,
          "{costs}",
          String.valueOf(Settings.hireOtherVillagersCosts),
          "{hire-item}",
          Settings.hireItem.toString());
    }
    return false;
  }