Search in sources :

Example 1 with MerchantInventory

use of org.bukkit.inventory.MerchantInventory in project scripted-quests by TeamMonumenta.

the class NpcTradeManager method onSuccessfulTrade.

/* This is a successful trade - clicking with an empty cursor on a valid result item */
private void onSuccessfulTrade(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked();
    MerchantInventory merchInv = (MerchantInventory) event.getInventory();
    PlayerTradeContext context = mOpenTrades.get(player.getUniqueId());
    /*
		 * Have to manually compute which slot this was because merchInv.getSelectedIndex returns the wrong index
		 * if the player leaves the trade on the first slot but puts in materials for one of the other trades
		 */
    MerchantRecipe recipe = merchInv.getSelectedRecipe();
    List<MerchantRecipe> recipes = merchInv.getMerchant().getRecipes();
    int selectedIndex = recipes.indexOf(recipe);
    if (selectedIndex < 0) {
        player.sendMessage(ChatColor.YELLOW + "BUG! Somehow the recipe you selected couldn't be found. Please report this, and include which villager and what you were trading for");
    } else {
        NpcTrade trade = context.getSlotProperties().get(selectedIndex);
        if (trade != null) {
            trade.doActions(new QuestContext(Plugin.getInstance(), player, context.getVillager()));
        }
    }
}
Also used : MerchantRecipe(org.bukkit.inventory.MerchantRecipe) Player(org.bukkit.entity.Player) MerchantInventory(org.bukkit.inventory.MerchantInventory) QuestContext(com.playmonumenta.scriptedquests.quests.QuestContext) NpcTrade(com.playmonumenta.scriptedquests.trades.NpcTrade)

Example 2 with MerchantInventory

use of org.bukkit.inventory.MerchantInventory in project scripted-quests by TeamMonumenta.

the class NpcTradeManager method inventoryClickEvent.

@EventHandler(priority = EventPriority.HIGHEST)
public void inventoryClickEvent(InventoryClickEvent event) {
    if (event.isCancelled() || event.getResult().equals(Event.Result.DENY) || !event.getInventory().getType().equals(InventoryType.MERCHANT) || !(event.getWhoClicked() instanceof Player)) {
        /* Already cancelled, or not a merchant inventory, or not a click by a player */
        return;
    }
    Player player = (Player) event.getWhoClicked();
    MerchantInventory merchInv = (MerchantInventory) event.getInventory();
    PlayerTradeContext context = mOpenTrades.get(player.getUniqueId());
    int hotbarButton = event.getHotbarButton();
    if (context == null || !merchInv.getMerchant().equals(context.getMerchant())) {
        player.sendMessage(ChatColor.RED + "DENIED: You should not have been able to view this interface. If this is a bug, please report it, and try trading with the villager again.");
        event.setCancelled(true);
        event.setResult(Event.Result.DENY);
        Bukkit.getScheduler().runTask(Plugin.getInstance(), () -> player.closeInventory());
        return;
    }
    if (event.getSlot() != 2) {
        /*
			 * The player is interacting with a valid merchant inventory but not yet clicking on the resulting item slot
			 * Ignore the click
			 */
        return;
    }
    ItemStack clickedItem = event.getInventory().getItem(event.getSlot());
    if (hotbarButton != -1) {
        ItemStack hotbarItem = player.getInventory().getItem(hotbarButton);
        if ((hotbarItem == null || hotbarItem.getType().isAir()) && clickedItem != null && !clickedItem.getType().isAir()) {
            onSuccessfulTrade(event);
        }
    } else {
        // If they use the swap hands key on the trade item and their offhand is not empty, do not trigger a successful trade
        if (event.getClick().equals(ClickType.SWAP_OFFHAND) && player.getInventory().getItemInOffHand() != null && !player.getInventory().getItemInOffHand().getType().isAir()) {
            return;
        }
        if ((event.getCursor() == null || event.getCursor().getType().isAir()) && clickedItem != null && !clickedItem.getType().isAir()) {
            onSuccessfulTrade(event);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) MerchantInventory(org.bukkit.inventory.MerchantInventory) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 3 with MerchantInventory

use of org.bukkit.inventory.MerchantInventory in project CoreProtect by PlayPro.

the class CraftItemListener method playerCraftItem.

protected static void playerCraftItem(InventoryClickEvent event, boolean isTrade) {
    if (event.getResult() == Result.DENY || event.getSlotType() != SlotType.RESULT) {
        return;
    }
    Player player = (Player) event.getWhoClicked();
    if (event.getClick() == ClickType.NUMBER_KEY && player.getInventory().getItem(event.getHotbarButton()) != null) {
        return;
    }
    if ((event.getClick() == ClickType.DROP || event.getClick() == ClickType.CONTROL_DROP) && event.getCursor().getType() != Material.AIR) {
        return;
    }
    Inventory bottomInventory = event.getView().getBottomInventory();
    if (bottomInventory.getType() != InventoryType.PLAYER) {
        return;
    }
    Recipe recipe = null;
    if (!isTrade && event instanceof CraftItemEvent) {
        recipe = ((CraftItemEvent) event).getRecipe();
    } else if (isTrade && event.getInventory() instanceof MerchantInventory) {
        recipe = ((MerchantInventory) event.getInventory()).getSelectedRecipe();
    }
    if (recipe == null) {
        return;
    }
    ItemStack addItem = recipe.getResult().clone();
    int amount = addItem.getAmount();
    if (amount == 0) {
        return;
    }
    int amountMultiplier = 1;
    if (event.getClick() == ClickType.SHIFT_LEFT || event.getClick() == ClickType.SHIFT_RIGHT) {
        int newMultiplier = Integer.MIN_VALUE;
        for (ItemStack item : event.getInventory().getContents()) {
            if (item != null && item.getType() != Material.AIR && !item.equals(recipe.getResult())) {
                if (isTrade) {
                    int merchantAmount = newMultiplier;
                    MerchantRecipe merchantRecipe = (MerchantRecipe) recipe;
                    for (ItemStack ingredient : merchantRecipe.getIngredients()) {
                        if (ingredient.isSimilar(item)) {
                            ItemStack adjusted = BukkitAdapter.ADAPTER.adjustIngredient(merchantRecipe, ingredient);
                            if (adjusted == null) {
                                return;
                            }
                            merchantAmount = item.getAmount() / adjusted.getAmount();
                            break;
                        }
                    }
                    int merchantUsesLeft = merchantRecipe.getMaxUses() - merchantRecipe.getUses();
                    if (merchantUsesLeft < merchantAmount) {
                        merchantAmount = merchantUsesLeft;
                    }
                    if (newMultiplier == Integer.MIN_VALUE || merchantAmount < newMultiplier) {
                        newMultiplier = merchantAmount;
                    }
                } else if (newMultiplier == Integer.MIN_VALUE || item.getAmount() < newMultiplier) {
                    newMultiplier = item.getAmount();
                }
            }
        }
        amountMultiplier = (newMultiplier == Integer.MIN_VALUE ? 1 : newMultiplier);
        int addAmount = amount * amountMultiplier;
        Inventory virtualInventory = Bukkit.createInventory(null, 36);
        virtualInventory.setStorageContents(bottomInventory.getStorageContents());
        addItem.setAmount(1);
        int addSuccess = 0;
        for (int i = 0; i < addAmount; i++) {
            if (!virtualInventory.addItem(addItem).isEmpty()) {
                break;
            }
            addSuccess++;
        }
        if (addSuccess < addAmount) {
            addAmount = (int) (Math.ceil(addSuccess / (double) amount) * amount);
            amountMultiplier = addAmount / amount;
        }
        virtualInventory.clear();
        addItem.setAmount(addAmount);
    }
    List<ItemStack> oldItems = new ArrayList<>();
    if (recipe instanceof ShapelessRecipe) {
        oldItems.addAll(((ShapelessRecipe) recipe).getIngredientList());
    } else if (recipe instanceof ShapedRecipe) {
        oldItems.addAll(((ShapedRecipe) recipe).getIngredientMap().values());
    } else if (recipe instanceof MerchantRecipe) {
        oldItems.addAll(((MerchantRecipe) recipe).getIngredients());
    }
    if (addItem.getAmount() > 0) {
        for (ItemStack oldItem : oldItems) {
            if (oldItem == null || oldItem.getType() == Material.AIR) {
                continue;
            }
            ItemStack removedItem = isTrade ? BukkitAdapter.ADAPTER.adjustIngredient((MerchantRecipe) recipe, oldItem) : oldItem.clone();
            if (removedItem == null) {
                return;
            }
            removedItem.setAmount(removedItem.getAmount() * amountMultiplier);
            logCraftedItem(event.getInventory().getLocation(), player.getName(), removedItem, isTrade ? ItemLogger.ITEM_SELL : ItemLogger.ITEM_DESTROY);
        }
        logCraftedItem(event.getInventory().getLocation(), player.getName(), addItem, isTrade ? ItemLogger.ITEM_BUY : ItemLogger.ITEM_CREATE);
    }
}
Also used : MerchantRecipe(org.bukkit.inventory.MerchantRecipe) Player(org.bukkit.entity.Player) MerchantInventory(org.bukkit.inventory.MerchantInventory) ShapelessRecipe(org.bukkit.inventory.ShapelessRecipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) MerchantRecipe(org.bukkit.inventory.MerchantRecipe) Recipe(org.bukkit.inventory.Recipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) CraftItemEvent(org.bukkit.event.inventory.CraftItemEvent) ArrayList(java.util.ArrayList) ShapelessRecipe(org.bukkit.inventory.ShapelessRecipe) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) MerchantInventory(org.bukkit.inventory.MerchantInventory)

Example 4 with MerchantInventory

use of org.bukkit.inventory.MerchantInventory in project InteractionVisualizer by LOOHP.

the class VillagerDisplay method onVillageTrade.

@EventHandler(priority = EventPriority.MONITOR)
public void onVillageTrade(InventoryClickEvent event) {
    if (event.isCancelled()) {
        return;
    }
    if (VanishUtils.isVanished((Player) event.getWhoClicked())) {
        return;
    }
    if (!(event.getView().getTopInventory() instanceof MerchantInventory)) {
        return;
    }
    if (event.getRawSlot() != 2) {
        return;
    }
    if (event.getCurrentItem() == null) {
        return;
    }
    if (event.getCurrentItem().getType().equals(Material.AIR)) {
        return;
    }
    if (event.getCursor() != null) {
        if (!event.getCursor().getType().equals(Material.AIR)) {
            if (event.getCursor().getAmount() >= event.getCursor().getType().getMaxStackSize()) {
                return;
            }
        }
    }
    if (event.isShiftClick()) {
        if (!InventoryUtils.stillHaveSpace(event.getWhoClicked().getInventory(), event.getView().getItem(2).getType())) {
            return;
        }
    }
    ItemStack item0 = null;
    if (event.getView().getItem(0) != null) {
        if (!event.getView().getItem(0).getType().equals(Material.AIR)) {
            item0 = event.getView().getItem(0).clone();
        }
    }
    ItemStack item1 = null;
    if (event.getView().getItem(1) != null) {
        if (!event.getView().getItem(1).getType().equals(Material.AIR)) {
            item1 = event.getView().getItem(1).clone();
        }
    }
    ItemStack item2 = event.getCurrentItem().clone();
    Player player = (Player) event.getWhoClicked();
    MerchantInventory tradeinv = (MerchantInventory) event.getView().getTopInventory();
    if (InteractionVisualizer.version.isNewerOrEqualTo(MCVersion.V1_14)) {
        if (!(tradeinv.getHolder() instanceof org.bukkit.entity.AbstractVillager)) {
            return;
        }
    } else {
        if (!(tradeinv.getHolder() instanceof Villager)) {
            return;
        }
    }
    NPC villager = (NPC) tradeinv.getHolder();
    Vector lift = new Vector(0.0, 0.20, 0.0);
    if (item0 != null) {
        Item in = new Item(player.getEyeLocation());
        Vector vector = villager.getEyeLocation().add(0.0, -0.5, 0.0).toVector().subtract(player.getEyeLocation().toVector()).multiply(0.12).add(lift);
        in.setItemStack(item0);
        in.setGravity(true);
        in.setVelocity(vector);
        PacketManager.sendItemSpawn(InteractionVisualizerAPI.getPlayerModuleList(Modules.ITEMDROP, KEY), in);
        PacketManager.updateItem(in);
        Bukkit.getScheduler().runTaskLater(InteractionVisualizer.plugin, () -> PacketManager.removeItem(InteractionVisualizerAPI.getPlayers(), in), 14);
    }
    ItemStack item1final = item1;
    Bukkit.getScheduler().runTaskLater(InteractionVisualizer.plugin, () -> {
        if (item1final != null) {
            Item in = new Item(player.getEyeLocation());
            Vector vector = villager.getEyeLocation().add(0.0, -0.5, 0.0).toVector().subtract(player.getEyeLocation().toVector()).multiply(0.12).add(lift);
            in.setItemStack(item1final);
            in.setGravity(true);
            in.setVelocity(vector);
            PacketManager.sendItemSpawn(InteractionVisualizerAPI.getPlayerModuleList(Modules.ITEMDROP, KEY), in);
            PacketManager.updateItem(in);
            Bukkit.getScheduler().runTaskLater(InteractionVisualizer.plugin, () -> PacketManager.removeItem(InteractionVisualizerAPI.getPlayers(), in), 14);
        }
    }, 8);
    Bukkit.getScheduler().runTaskLater(InteractionVisualizer.plugin, () -> {
        Item out = new Item(villager.getEyeLocation());
        Vector vector = player.getEyeLocation().add(0.0, -0.5, 0.0).toVector().subtract(villager.getEyeLocation().toVector()).multiply(0.10).add(lift);
        out.setItemStack(item2);
        out.setGravity(true);
        out.setVelocity(vector);
        PacketManager.sendItemSpawn(InteractionVisualizerAPI.getPlayerModuleList(Modules.ITEMDROP, KEY), out);
        PacketManager.updateItem(out);
        Bukkit.getScheduler().runTaskLater(InteractionVisualizer.plugin, () -> PacketManager.removeItem(InteractionVisualizerAPI.getPlayers(), out), 12);
    }, 40);
}
Also used : NPC(org.bukkit.entity.NPC) Item(com.loohp.interactionvisualizer.entityholders.Item) Player(org.bukkit.entity.Player) MerchantInventory(org.bukkit.inventory.MerchantInventory) Villager(org.bukkit.entity.Villager) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Player (org.bukkit.entity.Player)4 MerchantInventory (org.bukkit.inventory.MerchantInventory)4 ItemStack (org.bukkit.inventory.ItemStack)3 EventHandler (org.bukkit.event.EventHandler)2 MerchantRecipe (org.bukkit.inventory.MerchantRecipe)2 Item (com.loohp.interactionvisualizer.entityholders.Item)1 QuestContext (com.playmonumenta.scriptedquests.quests.QuestContext)1 NpcTrade (com.playmonumenta.scriptedquests.trades.NpcTrade)1 ArrayList (java.util.ArrayList)1 NPC (org.bukkit.entity.NPC)1 Villager (org.bukkit.entity.Villager)1 CraftItemEvent (org.bukkit.event.inventory.CraftItemEvent)1 Inventory (org.bukkit.inventory.Inventory)1 Recipe (org.bukkit.inventory.Recipe)1 ShapedRecipe (org.bukkit.inventory.ShapedRecipe)1 ShapelessRecipe (org.bukkit.inventory.ShapelessRecipe)1 Vector (org.bukkit.util.Vector)1