Beispiel #1
0
 @SuppressWarnings("deprecation")
 private void handlePlayerClick(int slot, PlayerInventory playerInv) {
   playerInv.setItem(slot, previousPlayerInv.getItem(slot));
   Stockable stockable = getStockable(playerInv.getItem(slot), "bought", true);
   if (stockable == null) {
     return;
   }
   if (prevPlayerSlot != slot) {
     prevPlayerSlot = slot;
     sendStockableMessage(stockable);
     return;
   }
   prevPlayerSlot = slot;
   prevTraderSlot = -1;
   if (checkMiscellaneous(playerInv, stockable, false)) {
     return;
   }
   ItemStack selling = stockable.getStocking().clone();
   if (mode != Mode.INFINITE) {
     EconomyHandler.pay(new Payment(stockable.getPrice()), npc, -1);
   }
   EconomyHandler.pay(new Payment(selling), player, slot);
   HashMap<Integer, ItemStack> unsold = new HashMap<Integer, ItemStack>();
   if (mode != Mode.INFINITE) {
     unsold = npc.getInventory().addItem(selling);
   }
   if (unsold.size() >= 1) {
     rewind();
     player.sendMessage(
         ChatColor.RED
             + "Not enough room available to add "
             + MessageUtils.getStackString(selling, ChatColor.RED)
             + " to the trader's stock.");
     return;
   }
   if (!stockable.isEconPlugin()) {
     ItemStack temp = stockable.getPrice().getItemStack().clone();
     unsold = player.getInventory().addItem(temp);
     if (unsold.size() >= 1) {
       rewind();
       player.sendMessage(
           ChatColor.RED
               + "Not enough room in your inventory to add "
               + MessageUtils.getStackString(stockable.getPrice().getItemStack(), ChatColor.RED)
               + ".");
       return;
     }
   } else {
     double price = stockable.getPrice().getPrice();
     ServerEconomyInterface.add(player.getName(), price);
   }
   npc.getPlayer().updateInventory();
   player.updateInventory();
   player.sendMessage(ChatColor.GREEN + "Transaction successful.");
 }
Beispiel #2
0
 @SuppressWarnings("deprecation")
 private void handleTraderClick(int slot, PlayerInventory npcInv) {
   npcInv.setItem(slot, previousTraderInv.getItem(slot));
   Stockable stockable = getStockable(npcInv.getItem(slot), "sold", false);
   if (stockable == null) {
     return;
   }
   if (prevTraderSlot != slot) {
     prevTraderSlot = slot;
     sendStockableMessage(stockable);
     return;
   }
   prevTraderSlot = slot;
   prevPlayerSlot = -1;
   if (checkMiscellaneous(npcInv, stockable, true)) {
     return;
   }
   // /trader sell 1:1 1:5
   // stocking is 1 stone
   // price is 5 stone
   ItemStack buying = stockable.getStocking().clone();
   EconomyHandler.pay(new Payment(stockable.getPrice()), player, -1);
   if (mode != Mode.INFINITE) {
     EconomyHandler.pay(new Payment(buying), npc, slot);
   }
   HashMap<Integer, ItemStack> unbought = player.getInventory().addItem(buying);
   if (unbought.size() >= 1) {
     rewind();
     player.sendMessage(
         ChatColor.RED
             + "Not enough room in your inventory to add "
             + MessageUtils.getStackString(buying, ChatColor.RED)
             + ".");
     return;
   }
   if (!stockable.isEconPlugin() && mode != Mode.INFINITE) {
     ItemStack temp = stockable.getPrice().getItemStack().clone();
     unbought = npc.getInventory().addItem(temp);
     if (unbought.size() >= 1) {
       rewind();
       player.sendMessage(
           ChatColor.RED
               + "Not enough room in the npc's inventory to add "
               + MessageUtils.getStackString(stockable.getPrice().getItemStack(), ChatColor.RED)
               + ".");
       return;
     }
   } else {
     double price = stockable.getPrice().getPrice();
     npc.setBalance(npc.getBalance() + price);
   }
   npc.getPlayer().updateInventory();
   player.updateInventory();
   player.sendMessage(ChatColor.GREEN + "Transaction successful.");
 }
Beispiel #3
0
 public void jobOrientation(Player p) {
   p.setGameMode(GameMode.SURVIVAL);
   if (this.equals(TimeProfession.OFFICER)) {
     PlayerInventory pi = p.getInventory();
     ItemStack inv_item = pi.getItem(0);
     String display_name = "";
     if (inv_item != null) {
       display_name = inv_item.getItemMeta().getDisplayName();
       if (display_name == null) display_name = "";
     }
     if (inv_item == null
         || !inv_item.getType().equals(Material.STICK)
         || !display_name.equals("Baton")) {
       ItemStack is = new ItemStack(Material.STICK, 1);
       ItemStack is_backup = null;
       if (inv_item != null) is_backup = inv_item.clone();
       ItemMeta im = is.getItemMeta();
       im.setDisplayName("Baton");
       ArrayList<String> lore = new ArrayList();
       lore.add("Arrests bountied players");
       im.setLore(lore);
       is.setItemMeta(im);
       pi.setItem(0, is);
       if (inv_item != null) pi.addItem(is_backup);
       p.updateInventory();
     }
   }
 }
Beispiel #4
0
 public static void deduct(ArrayList<ItemStack> reqItems, Player player) {
   PlayerInventory inven = player.getInventory();
   for (int i = 1; i < reqItems.size(); i++) {
     ItemStack currItem = new ItemStack(reqItems.get(i).getTypeId(), reqItems.get(i).getAmount());
     int neededAmount = reqItems.get(i).getAmount();
     int smallestSlot = AlchemySupport.findSmallest(currItem, player);
     while (neededAmount > 0) {
       smallestSlot = AlchemySupport.findSmallest(currItem, player);
       ItemStack smallestItem = inven.getItem(smallestSlot);
       if (neededAmount < smallestItem.getAmount()) {
         // got enough in smallest stack deal and done
         ItemStack newSize =
             new ItemStack(currItem.getType(), smallestItem.getAmount() - neededAmount);
         inven.setItem(smallestSlot, newSize);
         neededAmount = 0;
       } else {
         // need to remove from more than one stack, deal and continue
         neededAmount -= smallestItem.getAmount();
         inven.clear(smallestSlot);
       }
     }
   }
   ItemStack reward = new ItemStack(reqItems.get(0).getTypeId(), reqItems.get(0).getAmount());
   player.sendMessage("§3You have made " + reward.getAmount() + " " + reward.getType());
   inven.addItem(reward);
 }
  public boolean addEnchantmentsToPlayerInventorySlot(
      String playerName, int slot, List<Object> enchantments) {
    try {
      Player p = getPlayerExact(playerName);
      PlayerInventory inv = p.getInventory();
      ItemStack it;

      if (slot == inv.getHeldItemSlot()) it = inv.getHelmet();
      else if (slot == 102) it = inv.getChestplate();
      else if (slot == 101) it = inv.getLeggings();
      else if (slot == 100) it = inv.getBoots();
      else it = inv.getItem(slot);

      for (int i = 0; i < enchantments.size(); i++) {
        JSONObject o = (JSONObject) enchantments.get(i);
        it.addEnchantment(
            Enchantment.getById(Integer.valueOf(o.get("enchantment").toString())),
            Integer.valueOf(o.get("level").toString()));
      }

      p.saveData();

      return true;
    } catch (NullPointerException e) {
      return false;
    }
  }
  public GraveInventory(PlayerInventory inventory) {
    this.items = new ItemStack[inventory.getSize()];

    for (int i = 0; i < inventory.getSize(); i++) {
      items[i] = inventory.getItem(i);
    }
  }
Beispiel #7
0
  /**
   * Converts specified inventory into inventory string and stores it. Previous inventory string
   * will be deleted.
   *
   * @param playerinv inventory that will be stored
   */
  public void storeInventory(PlayerInventory playerinv) {
    String inv = "";
    for (int i = 0; i < 40; i++) {
      ItemStack item = playerinv.getItem(i);
      if (item == null || item.getType() == Material.AIR) continue;

      String enchantString = "";
      for (Entry<Enchantment, Integer> e : item.getEnchantments().entrySet())
        enchantString +=
            String.valueOf(e.getKey().getId()) + ":" + String.valueOf(e.getValue()) + "*";
      if (enchantString.length() > 1)
        enchantString = enchantString.substring(0, enchantString.length() - 1);

      inv +=
          String.valueOf(item.getTypeId())
              + ","
              + String.valueOf(item.getAmount())
              + ","
              + String.valueOf(item.getDurability())
              + ","
              + enchantString
              + ";";
    }
    inventory = inv;
    InputOutput.UpdatePrisoner(this);
  }
Beispiel #8
0
  public static int countItemsInInventory(PlayerInventory inventory, ItemStack item) {
    int totalAmount = 0;
    boolean isDurable = Items.itemByStack(item).isDurable();

    for (Integer i : inventory.all(item.getType()).keySet()) {
      ItemStack thisStack = inventory.getItem(i);
      if (!isDurable && thisStack.getDurability() != item.getDurability()) {
        continue;
      }
      totalAmount += thisStack.getAmount();
    }
    return totalAmount;
  }
Beispiel #9
0
 @EventHandler
 public void onPlayerJoin(PlayerJoinEvent e) {
   Player player = e.getPlayer();
   PlayerInventory in = player.getInventory();
   String locale = Locale.getPlayerLocale(player);
   for (int i = 0; i < in.getSize(); i++) {
     ItemStack item = in.getItem(i);
     if (ItemManager.toRPGItem(item) != null) RPGItem.updateItem(item, locale);
   }
   for (ItemStack item : player.getInventory().getArmorContents()) {
     if (ItemManager.toRPGItem(item) != null) RPGItem.updateItem(item, locale);
   }
 }
Beispiel #10
0
  @Override
  public synchronized void run() {
    if (stop) {
      return;
    }
    if ((npc == null || player == null || checkContainer(player.getHandle()) || !player.isOnline())
        && !said) {
      kill();
      return;
    }
    if (mode == Mode.STOCK) {
      return;
    }
    stop = true;
    int count = 0;
    boolean found = false;
    net.minecraft.server.ItemStack inhand = player.getHandle().inventory.j();
    for (ItemStack i : npc.getInventory().getContents()) {
      if (!previousTraderInv.getItem(count).equals(i) && inhand == null) {
        rewind();
        break;
      }
      if (!previousTraderInv.getItem(count).equals(i)
          && previousTraderInv.getItem(count).getTypeId() == inhand.id) {
        found = true;
        handleTraderClick(count, npc.getInventory());
        break;
      }
      count += 1;
    }

    count = 0;
    if (!found) {
      for (ItemStack i : player.getInventory().getContents()) {
        if (!previousPlayerInv.getItem(count).equals(i) && inhand == null) {
          rewind();
          break;
        }
        if (!previousPlayerInv.getItem(count).equals(i)
            && previousPlayerInv.getItem(count).getTypeId() == inhand.id) {
          handlePlayerClick(count, player.getInventory());
          break;
        }
        count += 1;
      }
    }

    clonePlayerInventory(npc.getInventory(), this.previousTraderInv);
    clonePlayerInventory(player.getInventory(), this.previousPlayerInv);

    // Set the itemstack in the player's cursor to null.
    player.getHandle().inventory.b((net.minecraft.server.ItemStack) null);
    // Get rid of the picture on the cursor.
    Packet103SetSlot packet = new Packet103SetSlot(-1, -1, null);
    player.getHandle().netServerHandler.sendPacket(packet);
    stop = false;
  }
 public static void emptyWaterBottle(Player player) {
   PlayerInventory inventory = player.getInventory();
   int index = inventory.first(new ItemStack(Material.POTION));
   if (index != -1) {
     ItemStack item = inventory.getItem(index);
     if (item.getAmount() == 1) {
       inventory.setItem(index, new ItemStack(Material.GLASS_BOTTLE));
     } else {
       item.setAmount(item.getAmount() - 1);
       inventory.setItem(index, item);
       HashMap<Integer, ItemStack> leftover =
           inventory.addItem(new ItemStack(Material.GLASS_BOTTLE));
       for (int left : leftover.keySet()) {
         player.getWorld().dropItemNaturally(player.getLocation(), leftover.get(left));
       }
     }
   }
 }
  private void fillBottle() {
    PlayerInventory inventory = player.getInventory();
    if (inventory.contains(Material.GLASS_BOTTLE)) {
      int index = inventory.first(Material.GLASS_BOTTLE);
      ItemStack item = inventory.getItem(index);
      if (item.getAmount() == 1) {
        inventory.setItem(index, new ItemStack(Material.POTION));
      } else {
        item.setAmount(item.getAmount() - 1);
        inventory.setItem(index, item);
        HashMap<Integer, ItemStack> leftover = inventory.addItem(new ItemStack(Material.POTION));
        for (int left : leftover.keySet()) {
          player.getWorld().dropItemNaturally(player.getLocation(), leftover.get(left));
        }
      }
    }

    remove();
  }
Beispiel #13
0
  public boolean addEnchantmentToPlayerInventorySlot(
      String playerName, int slot, int enchantmentID, int level) {
    try {
      Player p = getPlayerExact(playerName);
      PlayerInventory inv = p.getInventory();
      ItemStack it;

      if (slot == 103) it = inv.getHelmet();
      else if (slot == 102) it = inv.getChestplate();
      else if (slot == 101) it = inv.getLeggings();
      else if (slot == 100) it = inv.getBoots();
      else it = inv.getItem(slot);

      it.addEnchantment(Enchantment.getById(enchantmentID), level);

      p.saveData();

      return true;
    } catch (NullPointerException e) {
      return false;
    }
  }
Beispiel #14
0
  public boolean removeEnchantmentsFromPlayerInventorySlot(
      String playerName, int slot, List<Object> enchantments) {
    try {
      Player p = getPlayerExact(playerName);
      PlayerInventory inv = p.getInventory();
      ItemStack it;

      if (slot == inv.getHeldItemSlot()) it = inv.getHelmet();
      else if (slot == 102) it = inv.getChestplate();
      else if (slot == 101) it = inv.getLeggings();
      else if (slot == 100) it = inv.getBoots();
      else it = inv.getItem(slot);

      for (Object o : enchantments) {
        it.removeEnchantment(Enchantment.getById(Integer.valueOf(o.toString())));
      }

      p.saveData();

      return true;
    } catch (NullPointerException e) {
      return false;
    }
  }