Search in sources :

Example 1 with Item

use of com.loohp.interactionvisualizer.entityholders.Item in project InteractionVisualizer by LOOHP.

the class EnderchestDisplay method onDragEnderChest.

@EventHandler(priority = EventPriority.MONITOR)
public void onDragEnderChest(InventoryDragEvent event) {
    Player player = (Player) event.getWhoClicked();
    if (event.isCancelled()) {
        return;
    }
    if (!playermap.containsKey(player)) {
        return;
    }
    boolean ok = false;
    for (Entry<Integer, ItemStack> entry : event.getNewItems().entrySet()) {
        ItemStack item = event.getView().getItem(entry.getKey());
        if (item == null) {
            ok = true;
            break;
        }
        if (item.getType().equals(Material.AIR)) {
            ok = true;
            break;
        }
        if (!item.getType().equals(entry.getValue().getType())) {
            continue;
        }
        if (item.getAmount() < item.getType().getMaxStackSize()) {
            ok = true;
            break;
        }
    }
    if (!ok) {
        return;
    }
    Inventory topInventory = event.getView().getTopInventory();
    Block block = playermap.get(player);
    Location loc = block.getLocation();
    for (int slot : event.getRawSlots()) {
        if (slot >= 0 && slot < topInventory.getSize()) {
            PacketManager.sendHandMovement(InteractionVisualizerAPI.getPlayers(), player);
            ItemStack itemstack = event.getOldCursor();
            if (itemstack != null) {
                if (itemstack.getType().equals(Material.AIR)) {
                    itemstack = null;
                }
            }
            if (itemstack != null) {
                Item item = new Item(event.getWhoClicked().getEyeLocation().add(0.0, InteractionVisualizer.playerPickupYOffset, 0.0));
                Vector offset = new Vector(0.0, 0.15, 0.0);
                Vector vector = loc.clone().add(0.5, 1, 0.5).toVector().subtract(event.getWhoClicked().getEyeLocation().clone().add(0.0, InteractionVisualizer.playerPickupYOffset, 0.0).toVector()).multiply(0.15).add(offset);
                item.setVelocity(vector);
                PacketManager.sendItemSpawn(InteractionVisualizerAPI.getPlayerModuleList(Modules.ITEMDROP, KEY), item);
                item.setItemStack(itemstack);
                item.setCustomName(System.currentTimeMillis() + "");
                item.setPickupDelay(32767);
                item.setGravity(true);
                PacketManager.updateItem(item);
                if (!link.containsKey(player)) {
                    link.put(player, new ArrayList<Item>());
                }
                List<Item> list = link.get(player);
                list.add(item);
                Bukkit.getScheduler().runTaskLater(InteractionVisualizer.plugin, () -> {
                    item.teleport(loc.clone().add(0.5, 1, 0.5));
                    item.setVelocity(new Vector(0.0, 0.0, 0.0));
                    item.setGravity(false);
                    PacketManager.updateItem(item);
                }, 8);
                Bukkit.getScheduler().runTaskLater(InteractionVisualizer.plugin, () -> {
                    PacketManager.removeItem(InteractionVisualizerAPI.getPlayers(), item);
                    list.remove(item);
                }, 20);
            }
            break;
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Item(com.loohp.interactionvisualizer.entityholders.Item) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) Inventory(org.bukkit.inventory.Inventory) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 2 with Item

use of com.loohp.interactionvisualizer.entityholders.Item in project InteractionVisualizer by LOOHP.

the class EnderchestDisplay method onUseEnderChest.

@EventHandler(priority = EventPriority.MONITOR)
public void onUseEnderChest(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked();
    if (event.isCancelled()) {
        return;
    }
    if (!playermap.containsKey(player)) {
        return;
    }
    if (event.getClick().equals(ClickType.MIDDLE) && !event.getWhoClicked().getGameMode().equals(GameMode.CREATIVE)) {
        return;
    }
    Inventory topInventory = event.getView().getTopInventory();
    Block block = playermap.get(player);
    Location loc = block.getLocation();
    boolean isIn = true;
    boolean isMove = false;
    ItemStack itemstack = null;
    if (event.getRawSlot() >= 0 && event.getRawSlot() < topInventory.getSize()) {
        itemstack = event.getCurrentItem();
        if (itemstack != null) {
            if (itemstack.getType().equals(Material.AIR)) {
                itemstack = null;
            } else {
                isIn = false;
                isMove = true;
            }
        }
        if (itemstack == null) {
            itemstack = event.getCursor();
            if (itemstack != null) {
                if (itemstack.getType().equals(Material.AIR)) {
                    itemstack = null;
                } else {
                    isMove = true;
                }
            }
        } else {
            if (event.getCursor() != null) {
                if (event.getCursor().getType().equals(itemstack.getType())) {
                    isIn = true;
                }
            }
        }
        if (itemstack == null) {
            if (event.getAction().equals(InventoryAction.HOTBAR_MOVE_AND_READD) || event.getAction().equals(InventoryAction.HOTBAR_SWAP)) {
                itemstack = event.getWhoClicked().getInventory().getItem(event.getHotbarButton());
                if (itemstack != null) {
                    if (itemstack.getType().equals(Material.AIR)) {
                        itemstack = null;
                    } else {
                        isMove = true;
                    }
                }
            }
        }
    }
    if (itemstack == null) {
        if (event.getAction().equals(InventoryAction.MOVE_TO_OTHER_INVENTORY)) {
            itemstack = event.getCurrentItem();
            if (itemstack != null) {
                if (itemstack.getType().equals(Material.AIR)) {
                    itemstack = null;
                } else {
                    isMove = true;
                }
            }
        }
    }
    if (event.isShiftClick() && event.getView().getItem(event.getRawSlot()) != null) {
        if (isIn) {
            if (!InventoryUtils.stillHaveSpace(event.getView().getTopInventory(), event.getView().getItem(event.getRawSlot()).getType())) {
                return;
            }
        } else {
            if (!InventoryUtils.stillHaveSpace(event.getWhoClicked().getInventory(), event.getView().getItem(event.getRawSlot()).getType())) {
                return;
            }
        }
    }
    if (event.getCursor() != null) {
        if (!event.getCursor().getType().equals(Material.AIR)) {
            if (event.getCurrentItem() != null) {
                if (!event.getCurrentItem().getType().equals(Material.AIR)) {
                    if (event.getCurrentItem().getType().equals(event.getCursor().getType())) {
                        if (event.getCurrentItem().getAmount() >= event.getCurrentItem().getType().getMaxStackSize()) {
                            return;
                        }
                    }
                }
            }
        }
    }
    if (isMove) {
        PacketManager.sendHandMovement(InteractionVisualizerAPI.getPlayers(), player);
        if (itemstack != null) {
            Item item = new Item(loc.clone().add(0.5, 1, 0.5));
            Vector offset = new Vector(0.0, 0.15, 0.0);
            Vector vector = loc.clone().add(0.5, 1, 0.5).toVector().subtract(event.getWhoClicked().getEyeLocation().clone().add(0.0, -0.5, 0.0).add(0.0, InteractionVisualizer.playerPickupYOffset, 0.0).toVector()).multiply(-0.15).add(offset);
            item.setVelocity(vector);
            if (isIn) {
                item.teleport(event.getWhoClicked().getEyeLocation().add(0.0, InteractionVisualizer.playerPickupYOffset, 0.0));
                vector = loc.clone().add(0.5, 1, 0.5).toVector().subtract(event.getWhoClicked().getEyeLocation().clone().add(0.0, InteractionVisualizer.playerPickupYOffset, 0.0).toVector()).multiply(0.15).add(offset);
                item.setVelocity(vector);
            }
            PacketManager.sendItemSpawn(InteractionVisualizerAPI.getPlayerModuleList(Modules.ITEMDROP, KEY), item);
            item.setItemStack(itemstack);
            item.setPickupDelay(32767);
            item.setGravity(true);
            PacketManager.updateItem(item);
            if (!link.containsKey(player)) {
                link.put(player, new ArrayList<Item>());
            }
            List<Item> list = link.get(player);
            list.add(item);
            boolean finalIsIn = isIn;
            Bukkit.getScheduler().runTaskLater(InteractionVisualizer.plugin, () -> {
                if (finalIsIn) {
                    item.teleport(loc.clone().add(0.5, 1, 0.5));
                } else {
                    item.teleport(event.getWhoClicked().getEyeLocation().add(0.0, -0.5, 0.0).add(0.0, InteractionVisualizer.playerPickupYOffset, 0.0));
                }
                item.setVelocity(new Vector(0.0, 0.0, 0.0));
                item.setGravity(false);
                PacketManager.updateItem(item);
            }, 8);
            Bukkit.getScheduler().runTaskLater(InteractionVisualizer.plugin, () -> {
                PacketManager.removeItem(InteractionVisualizerAPI.getPlayers(), item);
                list.remove(item);
            }, 20);
        }
    }
}
Also used : Item(com.loohp.interactionvisualizer.entityholders.Item) Player(org.bukkit.entity.Player) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) Inventory(org.bukkit.inventory.Inventory) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 3 with Item

use of com.loohp.interactionvisualizer.entityholders.Item in project InteractionVisualizer by LOOHP.

the class EnderchestDisplay method onCloseEnderChest.

@EventHandler
public void onCloseEnderChest(InventoryCloseEvent event) {
    Player player = (Player) event.getPlayer();
    if (!playermap.containsKey(player)) {
        return;
    }
    List<Item> list = link.get(player);
    if (list != null) {
        for (Item item : list) {
            PacketManager.removeItem(InteractionVisualizerAPI.getPlayers(), item);
        }
        link.remove(player);
    }
    playermap.remove(player);
}
Also used : Item(com.loohp.interactionvisualizer.entityholders.Item) Player(org.bukkit.entity.Player) EventHandler(org.bukkit.event.EventHandler)

Example 4 with Item

use of com.loohp.interactionvisualizer.entityholders.Item in project InteractionVisualizer by LOOHP.

the class FurnaceDisplay method onBreakFurnace.

@EventHandler(priority = EventPriority.MONITOR)
public void onBreakFurnace(TileEntityRemovedEvent event) {
    Block block = event.getBlock();
    if (!furnaceMap.containsKey(block)) {
        return;
    }
    Map<String, Object> map = furnaceMap.get(block);
    if (map.get("Item") instanceof Item) {
        Item item = (Item) map.get("Item");
        PacketManager.removeItem(InteractionVisualizerAPI.getPlayers(), item);
    }
    if (map.get("Stand") instanceof ArmorStand) {
        ArmorStand stand = (ArmorStand) map.get("Stand");
        PacketManager.removeArmorStand(InteractionVisualizerAPI.getPlayers(), stand);
    }
    furnaceMap.remove(block);
}
Also used : Item(com.loohp.interactionvisualizer.entityholders.Item) ArmorStand(com.loohp.interactionvisualizer.entityholders.ArmorStand) Block(org.bukkit.block.Block) EventHandler(org.bukkit.event.EventHandler)

Example 5 with Item

use of com.loohp.interactionvisualizer.entityholders.Item in project InteractionVisualizer by LOOHP.

the class GrindstoneDisplay method onCloseGrindstone.

@EventHandler
public void onCloseGrindstone(InventoryCloseEvent event) {
    if (event.getView().getTopInventory() == null) {
        return;
    }
    try {
        if (event.getView().getTopInventory().getLocation() == null) {
            return;
        }
    } catch (Exception | AbstractMethodError e) {
        return;
    }
    if (event.getView().getTopInventory().getLocation().getBlock() == null) {
        return;
    }
    Block block = event.getView().getTopInventory().getLocation().getBlock();
    if (!openedGrindstone.containsKey(block)) {
        return;
    }
    Map<String, Object> map = openedGrindstone.get(block);
    if (!map.get("Player").equals(event.getPlayer())) {
        return;
    }
    for (int i = 0; i <= 2; i++) {
        if (!(map.get(String.valueOf(i)) instanceof String)) {
            Object entity = map.get(String.valueOf(i));
            if (entity instanceof Item) {
                PacketManager.removeItem(InteractionVisualizerAPI.getPlayers(), (Item) entity);
            } else if (entity instanceof ArmorStand) {
                PacketManager.removeArmorStand(InteractionVisualizerAPI.getPlayers(), (ArmorStand) entity);
            }
        }
    }
    openedGrindstone.remove(block);
}
Also used : Item(com.loohp.interactionvisualizer.entityholders.Item) ArmorStand(com.loohp.interactionvisualizer.entityholders.ArmorStand) Block(org.bukkit.block.Block) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Item (com.loohp.interactionvisualizer.entityholders.Item)59 Player (org.bukkit.entity.Player)45 EventHandler (org.bukkit.event.EventHandler)45 Block (org.bukkit.block.Block)43 Vector (org.bukkit.util.Vector)35 Location (org.bukkit.Location)32 ItemStack (org.bukkit.inventory.ItemStack)31 ArmorStand (com.loohp.interactionvisualizer.entityholders.ArmorStand)23 Inventory (org.bukkit.inventory.Inventory)22 HashMap (java.util.HashMap)7 InventoryView (org.bukkit.inventory.InventoryView)5 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)5 DynamicVisualizerEntity (com.loohp.interactionvisualizer.entityholders.DynamicVisualizerEntity)4 ItemFrame (com.loohp.interactionvisualizer.entityholders.ItemFrame)4 VisualizerEntity (com.loohp.interactionvisualizer.entityholders.VisualizerEntity)4 Collection (java.util.Collection)4 Chest (org.bukkit.block.Chest)4 DoubleChest (org.bukkit.block.DoubleChest)4 InventoryHolder (org.bukkit.inventory.InventoryHolder)4 HashSet (java.util.HashSet)3