Search in sources :

Example 1 with Jukebox

use of org.bukkit.block.Jukebox in project modules-extra by CubeEngine.

the class ActionBlock method setBlock.

protected boolean setBlock(BlockSection blockData, Location loc, LogAttachment attachment, boolean force, boolean preview, boolean rollback) {
    Block block = loc.getBlock();
    BlockState state = loc.getBlock().getState();
    state.setType(blockData.material);
    if (blockData.material == IRON_DOOR_BLOCK || blockData.material == WOODEN_DOOR) {
        // TODO correct?
        byte data = (byte) (blockData.data & ~8);
        state.setRawData(data);
    } else {
        state.setRawData(blockData.data);
    }
    if (!force && (// TODO correct?
    state.getData() instanceof Attachable || BlockUtil.isDetachableFromBelow(blockData.material))) {
        return false;
    }
    if (state.getData() instanceof Bed) {
        Bed bed = (Bed) state.getData();
        Block headBed = block.getRelative(bed.getFacing());
        BlockState headState = headBed.getState();
        headState.setType(AIR);
        if (preview) {
            attachment.addToPreview(headState);
        } else {
            headState.update(true, false);
        }
    } else if (state.getType() == WOOD_DOOR || state.getType() == IRON_DOOR_BLOCK) {
        Block topDoor = block.getRelative(UP);
        if (topDoor.getType() == state.getType()) {
            BlockState topState = topDoor.getState();
            topState.setType(AIR);
            if (preview) {
                attachment.addToPreview(topState);
            } else {
                topState.update(true, false);
            }
        }
    }
    if (preview) {
        attachment.addToPreview(state);
    } else {
        state.update(true, false);
    }
    if (rollback) {
        if (this instanceof SignBreak || this instanceof PlayerSignBreak) {
            String[] lines = this instanceof SignBreak ? ((SignBreak) this).oldLines : ((PlayerSignBreak) this).oldLines;
            if (preview) {
                attachment.addToPreview(state.getLocation(), lines);
            } else {
                Sign sign = (Sign) state.getBlock().getState();
                int i = 0;
                for (String line : lines) {
                    sign.setLine(i++, line);
                }
                sign.update();
            }
        } else if (blockData.is(BED_BLOCK)) {
            Bed bed = (Bed) state.getData();
            BlockState headBed = block.getRelative(bed.getFacing()).getState();
            headBed.setType(BED_BLOCK);
            Bed bedhead = (Bed) headBed.getData();
            bedhead.setHeadOfBed(true);
            bedhead.setFacingDirection(bed.getFacing());
            if (preview) {
                attachment.addToPreview(headBed);
            } else {
                headBed.update(true);
            }
        } else if (blockData.is(WOOD_DOOR, IRON_DOOR_BLOCK)) {
            byte data = (byte) (((blockData.data & 8) == 8) ? 9 : 8);
            BlockState topDoor = block.getRelative(UP).getState();
            topDoor.setType(state.getType());
            topDoor.setRawData(data);
            if (preview) {
                attachment.addToPreview(topDoor);
            } else {
                topDoor.update(true);
            }
        } else if (!preview) {
            if (this instanceof PlayerNoteBlockBreak) {
                NoteBlock noteblock = (NoteBlock) state.getBlock().getState();
                noteblock.setNote(((PlayerNoteBlockBreak) this).note);
                noteblock.update();
            } else if (this instanceof PlayerJukeboxBreak) {
                Jukebox jukebox = (Jukebox) state.getBlock().getState();
                jukebox.setPlaying(((PlayerJukeboxBreak) this).disc);
                jukebox.update();
            } else if (this instanceof PlayerContainerBreak) {
                InventoryHolder inventoryHolder = (InventoryHolder) state.getBlock().getState();
                inventoryHolder.getInventory().setContents(((PlayerContainerBreak) this).contents);
                ((BlockState) inventoryHolder).update();
            }
        }
    }
    return true;
}
Also used : Bed(org.bukkit.material.Bed) Jukebox(org.bukkit.block.Jukebox) PlayerSignBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerSignBreak) NoteBlock(org.bukkit.block.NoteBlock) PlayerContainerBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerContainerBreak) PlayerNoteBlockBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerNoteBlockBreak) BlockState(org.bukkit.block.BlockState) PlayerJukeboxBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerJukeboxBreak) Block(org.bukkit.block.Block) NoteBlock(org.bukkit.block.NoteBlock) Sign(org.bukkit.block.Sign) PlayerSignBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerSignBreak) Attachable(org.bukkit.material.Attachable) InventoryHolder(org.bukkit.inventory.InventoryHolder)

Example 2 with Jukebox

use of org.bukkit.block.Jukebox in project Glowstone by GlowstoneMC.

the class BlockJukebox method blockInteract.

@Override
public boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc) {
    Jukebox jukebox = (Jukebox) block.getState();
    if (jukebox.isPlaying()) {
        jukebox.eject();
        jukebox.update();
        return true;
    }
    ItemStack handItem = player.getItemInHand();
    if (handItem != null && handItem.getType().isRecord()) {
        jukebox.setPlaying(handItem.getType());
        jukebox.update();
        if (player.getGameMode() != GameMode.CREATIVE) {
            handItem.setAmount(handItem.getAmount() - 1);
            player.setItemInHand(handItem);
        }
        return true;
    }
    return false;
}
Also used : GlowJukebox(net.glowstone.block.entity.state.GlowJukebox) Jukebox(org.bukkit.block.Jukebox) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with Jukebox

use of org.bukkit.block.Jukebox in project Prism-Bukkit by prism.

the class PrismPlayerEvents method recordDiscInsert.

/**
 * recordDiscInsert.
 *
 * @param block  Block
 * @param player Player
 */
private void recordDiscInsert(Block block, Player player) {
    ItemStack hand = player.getInventory().getItemInMainHand();
    // They have to be holding a record
    if (!hand.getType().isRecord()) {
        return;
    }
    final Jukebox jukebox = (Jukebox) block.getState();
    // Do we have a disc inside? This will pop it out
    if (!jukebox.getPlaying().equals(Material.AIR)) {
        // Record currently playing disc
        final ItemStack i = new ItemStack(jukebox.getPlaying(), 1);
        RecordingQueue.addToQueue(ActionFactory.createItemStack("item-remove", i, i.getAmount(), 0, null, block.getLocation(), player));
    } else {
        // Record the insert
        RecordingQueue.addToQueue(ActionFactory.createItemStack("item-insert", hand, 1, 0, null, block.getLocation(), player));
    }
}
Also used : Jukebox(org.bukkit.block.Jukebox) ItemStack(org.bukkit.inventory.ItemStack)

Example 4 with Jukebox

use of org.bukkit.block.Jukebox 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 5 with Jukebox

use of org.bukkit.block.Jukebox in project Prism-Bukkit by prism.

the class PrismBlockEvents method logItemRemoveFromDestroyedContainer.

/**
     * If this is a container we need to trigger item removal for everything in
     * it. It's important we record this *after* the block break so the log
     * shows what really happened.
     * 
     * @param player_name
     * @param block
     */
public void logItemRemoveFromDestroyedContainer(String player_name, Block block) {
    if (block.getType().equals(Material.JUKEBOX)) {
        final Jukebox jukebox = (Jukebox) block.getState();
        final Material playing = jukebox.getPlaying();
        if (playing == null || playing.equals(Material.AIR))
            return;
        final ItemStack i = new ItemStack(jukebox.getPlaying(), 1);
        RecordingQueue.addToQueue(ActionFactory.createItemStack("item-remove", i, i.getAmount(), 0, null, block.getLocation(), player_name));
        return;
    }
    if (block.getState() instanceof InventoryHolder) {
        final InventoryHolder container = (InventoryHolder) block.getState();
        int slot = 0;
        for (final ItemStack i : container.getInventory().getContents()) {
            // even though only half of the chest breaks.
            if ((block.getType().equals(Material.CHEST) || block.getType().equals(Material.TRAPPED_CHEST)) && slot > 26)
                break;
            // record item
            if (i != null) {
                RecordingQueue.addToQueue(ActionFactory.createItemStack("item-remove", i, i.getAmount(), slot, null, block.getLocation(), player_name));
            }
            slot++;
        }
    }
}
Also used : Jukebox(org.bukkit.block.Jukebox) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder)

Aggregations

Jukebox (org.bukkit.block.Jukebox)9 InventoryHolder (org.bukkit.inventory.InventoryHolder)7 ItemStack (org.bukkit.inventory.ItemStack)6 Block (org.bukkit.block.Block)5 BlockState (org.bukkit.block.BlockState)3 NoteBlock (org.bukkit.block.NoteBlock)3 Material (org.bukkit.Material)2 BlockFace (org.bukkit.block.BlockFace)2 Sign (org.bukkit.block.Sign)2 Entity (org.bukkit.entity.Entity)2 Item (org.bukkit.entity.Item)2 ItemFrame (org.bukkit.entity.ItemFrame)2 Player (org.bukkit.entity.Player)2 EventHandler (org.bukkit.event.EventHandler)2 Inventory (org.bukkit.inventory.Inventory)2 ChangeResultType (me.botsko.prism.api.ChangeResultType)1 PrismProcessType (me.botsko.prism.api.actions.PrismProcessType)1 ChangeResult (me.botsko.prism.appliers.ChangeResult)1 ChangeResultImpl (me.botsko.prism.appliers.ChangeResultImpl)1 ChangeResultType (me.botsko.prism.appliers.ChangeResultType)1