@SuppressWarnings("deprecation") private void rewind() { player.getInventory().setContents(previousPlayerInv.getContents()); npc.getInventory().setContents(previousTraderInv.getContents()); player.updateInventory(); npc.getPlayer().updateInventory(); }
@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; }
@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."); }
@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."); }
@Override public void onRightClick(Player player, HumanNPC npc) { if (Permission.canUse(player, npc, getType())) { WizardNPC wizard = npc.getToggleable("wizard"); if (player.getItemInHand().getTypeId() == Constants.wizardInteractItem) { WizardMode mode = wizard.getMode(); switch (mode) { case TELEPORT: if (wizard.getNumberOfLocations() > 0) { WizardManager.buy(player, npc, Operation.WIZARD_TELEPORT); } break; case SPAWN: WizardManager.buy(player, npc, Operation.WIZARD_SPAWNMOB); break; case TIME: WizardManager.buy(player, npc, Operation.WIZARD_CHANGETIME); break; case WEATHER: WizardManager.buy(player, npc, Operation.WIZARD_TOGGLESTORM); break; default: player.sendMessage(ChatColor.RED + "No valid mode selected."); break; } } else if (player.getItemInHand().getTypeId() == Constants.wizardManaRegenItem) { String msg = StringUtils.wrap(npc.getStrippedName() + "'s"); int mana = 0; if (wizard.getMana() + 10 < Constants.maxWizardMana) { mana = wizard.getMana() + 10; msg += " mana has been increased to " + StringUtils.wrap(mana) + "."; } else if (wizard.getMana() + 10 == Constants.maxWizardMana) { mana = Constants.maxWizardMana; msg += " mana has been fully replenished."; } else { msg += " mana cannot be regenerated with that item any further."; return; } InventoryUtils.decreaseItemInHand( player, Material.getMaterial(Constants.wizardManaRegenItem)); player.sendMessage(msg); wizard.setMana(mana); } } else { player.sendMessage(MessageUtils.noPermissionsMessage); } }
/** * Gets run every tick, checks the inventory for changes. * * @param npc * @param player * @param plugin * @param mode */ public TraderTask(HumanNPC npc, Player player, Mode mode) { this.npc = npc; this.player = (CraftPlayer) player; // Create the inventory objects this.previousTraderInv = new CraftInventoryPlayer(new InventoryPlayer(null)); this.previousPlayerInv = new CraftInventoryPlayer(new InventoryPlayer(null)); // clone the items to the newly created inventory objects clonePlayerInventory(npc.getInventory(), this.previousTraderInv); clonePlayerInventory(player.getInventory(), this.previousPlayerInv); this.mode = mode; sendJoinMessage(); }
@Override public void onLeftClick(Player player, HumanNPC npc) { if (Permission.canUse(player, npc, getType())) { if (player.getItemInHand().getTypeId() == Constants.wizardInteractItem) { WizardNPC wizard = npc.getToggleable("wizard"); WizardMode mode = wizard.getMode(); String msg = ChatColor.GREEN + ""; switch (mode) { case TELEPORT: if (wizard.getNumberOfLocations() > 0) { wizard.cycle(npc, WizardMode.TELEPORT); msg += "Location set to " + StringUtils.wrap(wizard.getCurrentLocationName()); } else { msg += ChatColor.RED + npc.getStrippedName() + " has no locations."; } break; case SPAWN: wizard.cycle(npc, WizardMode.SPAWN); msg += "Mob to spawn set to " + StringUtils.wrap(wizard.getMob().name().toLowerCase().replace("_", " ")); break; case TIME: wizard.cycle(npc, WizardMode.TIME); msg += "Time setting set to " + StringUtils.wrap(wizard.getTime()); break; case WEATHER: return; default: msg = ChatColor.RED + "No valid mode selected."; break; } player.sendMessage(msg); } } else { player.sendMessage(MessageUtils.noPermissionsMessage); } }
public void kill() { TraderNPC trader = npc.getToggleable("trader"); trader.setFree(true); if (!said) { sendLeaveMessage(); said = true; } int index = TraderManager.tasks.indexOf(taskID); if (index != -1) { TraderManager.tasks.remove(TraderManager.tasks.indexOf(taskID)); } run(); Bukkit.getServer().getScheduler().cancelTask(taskID); }
private void sendLeaveMessage() { switch (mode) { case INFINITE: case NORMAL: player.sendMessage(ChatColor.GOLD + "-------------------------------"); break; case STOCK: player.sendMessage( ChatColor.GOLD + "Stocking of " + StringUtils.wrap(npc.getStrippedName(), ChatColor.GOLD) + " finished."); break; } }
private void sendJoinMessage() { switch (mode) { case INFINITE: case NORMAL: player.sendMessage(ChatColor.GREEN + "Transaction log"); player.sendMessage(ChatColor.GOLD + "-------------------------------"); break; case STOCK: player.sendMessage( ChatColor.GOLD + "Stocking of " + StringUtils.wrap(npc.getStrippedName(), ChatColor.GOLD) + " started."); break; } }
private Stockable getStockable(ItemStack item, String keyword, boolean selling) { // durability needs to be reset to 0 for tools / weapons / armor short durability = item.getDurability(); if (isTool(item.getTypeId())) { durability = 0; } TraderNPC trader = npc.getToggleable("trader"); if (!trader.isStocked(item.getTypeId(), durability, selling)) { player.sendMessage( StringUtils.wrap(MessageUtils.getItemName(item.getTypeId()), ChatColor.RED) + " isn't being " + keyword + " here."); return null; } return trader.getStockable(item.getTypeId(), durability, selling); }
/** * Sets the next location in the list as active. * * @return */ public void cycle(HumanNPC npc, WizardMode mode) { WizardNPC wizard = npc.getToggleable("wizard"); switch (mode) { case TELEPORT: currentLocation++; if (currentLocation >= numberOfLocations) { currentLocation = 0; } break; case SPAWN: CreatureType type = wizard.getMob(); CreatureType newType = null; switch (type) { case PIG: newType = CreatureType.WOLF; break; case WOLF: newType = CreatureType.COW; break; case COW: newType = CreatureType.CHICKEN; break; case CHICKEN: newType = CreatureType.SHEEP; break; case SHEEP: newType = CreatureType.SQUID; break; case SQUID: newType = CreatureType.PIG_ZOMBIE; break; case PIG_ZOMBIE: newType = CreatureType.GHAST; break; case GHAST: newType = CreatureType.GIANT; break; case GIANT: newType = CreatureType.CREEPER; break; case CREEPER: newType = CreatureType.ZOMBIE; break; case ZOMBIE: newType = CreatureType.SPIDER; break; case SPIDER: newType = CreatureType.SKELETON; break; case SKELETON: newType = CreatureType.PIG; break; } wizard.setMob(newType); break; case TIME: String time = wizard.getTime(); String newTime = ""; if (time.equals("day")) { newTime = "afternoon"; } else if (time.equals("night")) { newTime = "morning"; } else if (time.equals("morning")) { newTime = "day"; } else if (time.equals("afternoon")) { newTime = "night"; } wizard.setTime(newTime); break; } }