Search in sources :

Example 1 with ItemFrame

use of org.bukkit.entity.ItemFrame in project BKCommonLib by bergerhealer.

the class CommonMapController method onEntityRemoved.

@EventHandler(priority = EventPriority.MONITOR)
protected void onEntityRemoved(EntityRemoveEvent event) {
    if (event.getEntityType() == EntityType.ITEM_FRAME) {
        ItemFrame frame = (ItemFrame) event.getEntity();
        ItemFrameInfo info = itemFrames.get(frame);
        if (info != null) {
            info.removed = true;
        }
    }
}
Also used : ItemFrame(org.bukkit.entity.ItemFrame) EventHandler(org.bukkit.event.EventHandler)

Example 2 with ItemFrame

use of org.bukkit.entity.ItemFrame in project Prism-Bukkit by prism.

the class ItemStackAction method placeItems.

/**
     * 
     * @return
     */
protected ChangeResult placeItems(Player player, QueryParameters parameters, boolean is_preview) {
    ChangeResultType result = null;
    if (is_preview) {
        return new ChangeResult(ChangeResultType.PLANNED, null);
    }
    if (plugin.getConfig().getBoolean("prism.appliers.allow-rollback-items-removed-from-container")) {
        final Block block = getWorld().getBlockAt(getLoc());
        Inventory inventory = null;
        // Item drop/pickup from player inventories
        if (getType().getName().equals("item-drop") || getType().getName().equals("item-pickup")) {
            // Is player online?
            final String playerName = getPlayerName();
            final Player onlinePlayer = Bukkit.getServer().getPlayer(playerName);
            if (onlinePlayer != null) {
                inventory = onlinePlayer.getInventory();
            } else {
                // Skip if the player isn't online
                Prism.debug("Skipping inventory process because player is offline");
                return new ChangeResult(ChangeResultType.SKIPPED, null);
            }
        } else {
            if (block.getType().equals(Material.JUKEBOX)) {
                final Jukebox jukebox = (Jukebox) block.getState();
                jukebox.setPlaying(item.getType());
                jukebox.update();
            } else if (block.getState() instanceof InventoryHolder) {
                final InventoryHolder ih = (InventoryHolder) block.getState();
                inventory = ih.getInventory();
            } else {
                Entity[] foundEntities = block.getChunk().getEntities();
                if (foundEntities.length > 0) {
                    for (Entity e : foundEntities) {
                        if (!e.getType().equals(EntityType.ITEM_FRAME))
                            continue;
                        // https://snowy-evening.com/botsko/prism/318/
                        if (!block.getWorld().equals(e.getWorld()))
                            continue;
                        // Let's limit this to only entities within 1 block of the current.
                        Prism.debug(block.getLocation());
                        Prism.debug(e.getLocation());
                        if (block.getLocation().distance(e.getLocation()) < 2) {
                            final ItemFrame frame = (ItemFrame) e;
                            if ((getType().getName().equals("item-remove") && parameters.getProcessType().equals(PrismProcessType.ROLLBACK)) || (getType().getName().equals("item-insert") && parameters.getProcessType().equals(PrismProcessType.RESTORE))) {
                                frame.setItem(item);
                            } else {
                                frame.setItem(null);
                            }
                            result = ChangeResultType.APPLIED;
                        }
                    }
                }
            }
        }
        if (inventory != null) {
            final PrismProcessType pt = parameters.getProcessType();
            final String n = getType().getName();
            // inventory
            if ((pt.equals(PrismProcessType.ROLLBACK) && (n.equals("item-remove") || n.equals("item-drop"))) || (pt.equals(PrismProcessType.RESTORE) && (n.equals("item-insert") || n.equals("item-pickup")))) {
                boolean added = false;
                // We'll attempt to put it back in the same slot
                if (getActionData().slot >= 0) {
                    // https://snowy-evening.com/botsko/prism/450/
                    if (getActionData().slot < inventory.getSize()) {
                        final ItemStack currentSlotItem = inventory.getItem(getActionData().slot);
                        // Make sure nothing's there.
                        if (currentSlotItem == null) {
                            result = ChangeResultType.APPLIED;
                            added = true;
                            inventory.setItem(getActionData().slot, getItem());
                        }
                    }
                }
                // If that failed we'll attempt to put it anywhere
                if (!added) {
                    final HashMap<Integer, ItemStack> leftovers = InventoryUtils.addItemToInventory(inventory, getItem());
                    if (leftovers.size() > 0) {
                        Prism.debug("Skipping adding items because there are leftovers");
                        result = ChangeResultType.SKIPPED;
                    } else {
                        result = ChangeResultType.APPLIED;
                        added = true;
                    }
                }
                // Item was added to the inv, we need to remove the entity
                if (added && (n.equals("item-drop") || n.equals("item-pickup"))) {
                    final Entity[] entities = getLoc().getChunk().getEntities();
                    for (final Entity entity : entities) {
                        if (entity instanceof Item) {
                            final ItemStack stack = ((Item) entity).getItemStack();
                            if (stack.isSimilar(getItem())) {
                                // Remove the event's number of items from
                                // the stack
                                stack.setAmount(stack.getAmount() - getItem().getAmount());
                                if (stack.getAmount() == 0) {
                                    entity.remove();
                                }
                                break;
                            }
                        }
                    }
                }
            }
            // inventory
            if ((pt.equals(PrismProcessType.ROLLBACK) && (n.equals("item-insert") || n.equals("item-pickup"))) || (pt.equals(PrismProcessType.RESTORE) && (n.equals("item-remove") || n.equals("item-drop")))) {
                // does inventory have item?
                boolean removed = false;
                // We'll attempt to take it from the same slot
                if (getActionData().slot >= 0) {
                    if (getActionData().slot > inventory.getContents().length) {
                        inventory.addItem(getItem());
                    } else {
                        final ItemStack currentSlotItem = inventory.getItem(getActionData().slot);
                        // Make sure something's there.
                        if (currentSlotItem != null) {
                            currentSlotItem.setAmount(currentSlotItem.getAmount() - getItem().getAmount());
                            result = ChangeResultType.APPLIED;
                            removed = true;
                            inventory.setItem(getActionData().slot, currentSlotItem);
                        }
                    }
                }
                // If that failed we'll attempt to take it from anywhere
                if (!removed) {
                    final int slot = InventoryUtils.inventoryHasItem(inventory, getItem().getTypeId(), getItem().getDurability());
                    if (slot > -1) {
                        inventory.removeItem(getItem());
                        result = ChangeResultType.APPLIED;
                        removed = true;
                    } else {
                        Prism.debug("Item removal from container skipped because it's not currently inside.");
                        result = ChangeResultType.SKIPPED;
                    }
                }
                // If the item was removed and it's a drop type, re-drop it
                if (removed && (n.equals("item-drop") || n.equals("item-pickup"))) {
                    me.botsko.elixr.ItemUtils.dropItem(getLoc(), getItem());
                }
            }
        }
    }
    return new ChangeResult(result, null);
}
Also used : Jukebox(org.bukkit.block.Jukebox) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ItemFrame(org.bukkit.entity.ItemFrame) ChangeResult(me.botsko.prism.appliers.ChangeResult) ChangeResultType(me.botsko.prism.appliers.ChangeResultType) Item(org.bukkit.entity.Item) PrismProcessType(me.botsko.prism.appliers.PrismProcessType) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory)

Example 3 with ItemFrame

use of org.bukkit.entity.ItemFrame in project BKCommonLib by bergerhealer.

the class CommonMapController method onEntityRightClick.

@EventHandler(priority = EventPriority.LOWEST)
protected void onEntityRightClick(PlayerInteractEntityEvent event) {
    if (!(event.getRightClicked() instanceof ItemFrame)) {
        return;
    }
    ItemFrame itemFrame = (ItemFrame) event.getRightClicked();
    if (lastClickOffset != null) {
        Vector pos = lastClickOffset;
        lastClickOffset = null;
        BlockFace attachedFace = itemFrame.getAttachedFace();
        double dx, dy;
        if (FaceUtil.isAlongZ(attachedFace)) {
            dx = pos.getX() + 0.5;
            dy = 1.0 - (pos.getY() + 0.5);
            if (attachedFace == BlockFace.SOUTH) {
                dx = 1.0 - dx;
            }
        } else {
            dx = pos.getZ() + 0.5;
            dy = 1.0 - (pos.getY() + 0.5);
            if (attachedFace == BlockFace.WEST) {
                dx = 1.0 - dx;
            }
        }
        event.setCancelled(dispatchClickAction(event.getPlayer(), itemFrame, dx, dy, MapAction.RIGHT_CLICK));
    } else {
        event.setCancelled(dispatchClickActionApprox(event.getPlayer(), itemFrame, MapAction.RIGHT_CLICK));
    }
}
Also used : BlockFace(org.bukkit.block.BlockFace) ItemFrame(org.bukkit.entity.ItemFrame) Vector(org.bukkit.util.Vector) EventHandler(org.bukkit.event.EventHandler)

Example 4 with ItemFrame

use of org.bukkit.entity.ItemFrame in project BKCommonLib by bergerhealer.

the class CommonMapController method onEntityAdded.

@EventHandler(priority = EventPriority.MONITOR)
protected void onEntityAdded(EntityAddEvent event) {
    if (event.getEntityType() == EntityType.ITEM_FRAME) {
        ItemFrame frame = (ItemFrame) event.getEntity();
        itemFrames.put(frame, new ItemFrameInfo(frame));
    }
}
Also used : ItemFrame(org.bukkit.entity.ItemFrame) EventHandler(org.bukkit.event.EventHandler)

Example 5 with ItemFrame

use of org.bukkit.entity.ItemFrame in project BKCommonLib by bergerhealer.

the class CommonMapController method dispatchClickActionFromBlock.

private boolean dispatchClickActionFromBlock(Player player, Block clickedBlock, BlockFace clickedFace, MapAction action) {
    double x = clickedBlock.getX() + 0.5 + (double) clickedFace.getModX() * 0.5;
    double y = clickedBlock.getY() + 0.5 + (double) clickedFace.getModY() * 0.5;
    double z = clickedBlock.getZ() + 0.5 + (double) clickedFace.getModZ() * 0.5;
    for (Entity e : WorldUtil.getEntities(clickedBlock.getWorld(), null, x - 0.01, y - 0.01, z - 0.01, x + 0.01, y + 0.01, z + 0.01)) {
        if (e instanceof ItemFrame) {
            return dispatchClickActionApprox(player, (ItemFrame) e, action);
        }
    }
    return false;
}
Also used : Entity(org.bukkit.entity.Entity) ItemFrame(org.bukkit.entity.ItemFrame)

Aggregations

ItemFrame (org.bukkit.entity.ItemFrame)5 EventHandler (org.bukkit.event.EventHandler)3 Entity (org.bukkit.entity.Entity)2 ChangeResult (me.botsko.prism.appliers.ChangeResult)1 ChangeResultType (me.botsko.prism.appliers.ChangeResultType)1 PrismProcessType (me.botsko.prism.appliers.PrismProcessType)1 Block (org.bukkit.block.Block)1 BlockFace (org.bukkit.block.BlockFace)1 Jukebox (org.bukkit.block.Jukebox)1 Item (org.bukkit.entity.Item)1 Player (org.bukkit.entity.Player)1 Inventory (org.bukkit.inventory.Inventory)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1 ItemStack (org.bukkit.inventory.ItemStack)1 Vector (org.bukkit.util.Vector)1