Search in sources :

Example 1 with InventoryHolder

use of org.bukkit.inventory.InventoryHolder in project HawkEye by oliverwoodings.

the class ContainerEntry method rebuild.

@Override
public boolean rebuild(Block block) {
    if (!(block instanceof InventoryHolder))
        return false;
    Inventory inv = ((InventoryHolder) block.getState()).getInventory();
    List<HashMap<String, Integer>> ops = InventoryUtil.interpretDifferenceString(data);
    //Handle the additions
    if (ops.size() > 0) {
        for (ItemStack stack : InventoryUtil.uncompressInventory(ops.get(0))) inv.addItem(stack);
    }
    //Handle subtractions
    if (ops.size() > 1) {
        for (ItemStack stack : InventoryUtil.uncompressInventory(ops.get(1))) inv.removeItem(stack);
    }
    return true;
}
Also used : HashMap(java.util.HashMap) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory)

Example 2 with InventoryHolder

use of org.bukkit.inventory.InventoryHolder in project Prism-Bukkit by prism.

the class PrismInventoryMoveItemEvent method onInventoryMoveItem.

/**
     * 
     * @param event
     */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryMoveItem(final InventoryMoveItemEvent event) {
    // Hopper inserted
    if (Prism.getIgnore().event("item-insert") && event.getDestination() != null) {
        // Get container
        final InventoryHolder ih = event.getDestination().getHolder();
        Location containerLoc = null;
        if (ih instanceof BlockState) {
            final BlockState eventChest = (BlockState) ih;
            containerLoc = eventChest.getLocation();
        }
        if (containerLoc == null)
            return;
        if (event.getSource().getType().equals(InventoryType.HOPPER)) {
            RecordingQueue.addToQueue(ActionFactory.createItemStack("item-insert", event.getItem(), event.getItem().getAmount(), 0, null, containerLoc, "hopper"));
        }
    }
    // Hopper removed
    if (Prism.getIgnore().event("item-remove") && event.getSource() != null) {
        // Get container
        final InventoryHolder ih = event.getSource().getHolder();
        Location containerLoc = null;
        if (ih instanceof BlockState) {
            final BlockState eventChest = (BlockState) ih;
            containerLoc = eventChest.getLocation();
        }
        if (containerLoc == null)
            return;
        if (event.getDestination().getType().equals(InventoryType.HOPPER)) {
            RecordingQueue.addToQueue(ActionFactory.createItemStack("item-remove", event.getItem(), event.getItem().getAmount(), 0, null, containerLoc, "hopper"));
        }
    }
}
Also used : BlockState(org.bukkit.block.BlockState) InventoryHolder(org.bukkit.inventory.InventoryHolder) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 3 with InventoryHolder

use of org.bukkit.inventory.InventoryHolder in project Denizen-For-Bukkit by DenizenScript.

the class CopyBlockCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    dLocation copy_location = (dLocation) scriptEntry.getObject("location");
    dLocation destination = (dLocation) scriptEntry.getObject("destination");
    dCuboid copy_cuboid = (dCuboid) scriptEntry.getObject("cuboid");
    Element remove_original = (Element) scriptEntry.getObject("remove");
    dB.report(scriptEntry, getName(), (copy_location != null ? copy_location.debug() : "") + (copy_cuboid != null ? copy_cuboid.debug() : "") + destination.debug() + remove_original.debug());
    List<Location> locations = new ArrayList<Location>();
    if (copy_location != null) {
        locations.add(copy_location);
    } else if (copy_cuboid != null) {
        // TODO: make this work?...
        locations.addAll(copy_cuboid.getBlockLocations());
    }
    for (Location loc : locations) {
        Block source = loc.getBlock();
        BlockState sourceState = source.getState();
        Block update = destination.getBlock();
        update.setTypeIdAndData(source.getTypeId(), source.getData(), false);
        BlockState updateState = update.getState();
        // of InventoryHolder
        if (sourceState instanceof InventoryHolder) {
            ((InventoryHolder) updateState).getInventory().setContents(((InventoryHolder) sourceState).getInventory().getContents());
        } else if (sourceState instanceof Sign) {
            int n = 0;
            for (String line : ((Sign) sourceState).getLines()) {
                ((Sign) updateState).setLine(n, line);
                n++;
            }
        } else if (sourceState instanceof NoteBlock) {
            ((NoteBlock) updateState).setNote(((NoteBlock) sourceState).getNote());
        } else if (sourceState instanceof Skull) {
            ((Skull) updateState).setSkullType(((Skull) sourceState).getSkullType());
            ((Skull) updateState).setOwner(((Skull) sourceState).getOwner());
            ((Skull) updateState).setRotation(((Skull) sourceState).getRotation());
        } else if (sourceState instanceof Jukebox) {
            ((Jukebox) updateState).setPlaying(((Jukebox) sourceState).getPlaying());
        } else if (sourceState instanceof Banner) {
            ((Banner) updateState).setBaseColor(((Banner) sourceState).getBaseColor());
            ((Banner) updateState).setPatterns(((Banner) sourceState).getPatterns());
        } else if (sourceState instanceof CommandBlock) {
            ((CommandBlock) updateState).setName(((CommandBlock) sourceState).getName());
            ((CommandBlock) updateState).setCommand(((CommandBlock) sourceState).getCommand());
        } else if (sourceState instanceof CreatureSpawner) {
            ((CreatureSpawner) updateState).setCreatureTypeByName(((CreatureSpawner) sourceState).getCreatureTypeName());
            ((CreatureSpawner) updateState).setDelay(((CreatureSpawner) sourceState).getDelay());
        }
        updateState.update();
        if (remove_original.asBoolean()) {
            loc.getBlock().setType(Material.AIR);
        }
    }
}
Also used : Element(net.aufdemrand.denizencore.objects.Element) ArrayList(java.util.ArrayList) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) net.aufdemrand.denizen.objects.dCuboid(net.aufdemrand.denizen.objects.dCuboid) InventoryHolder(org.bukkit.inventory.InventoryHolder) Location(org.bukkit.Location) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation)

Example 4 with InventoryHolder

use of org.bukkit.inventory.InventoryHolder in project Towny by ElgarL.

the class ProtectionRegenTask method replaceProtections.

public void replaceProtections() {
    try {
        Block block = state.getBlock();
        if (state.getData() instanceof Door) {
            Door door = (Door) state.getData();
            Block topHalf;
            Block bottomHalf;
            if (door.isTopHalf()) {
                topHalf = block;
                bottomHalf = block.getRelative(BlockFace.DOWN);
            } else {
                bottomHalf = block;
                topHalf = block.getRelative(BlockFace.UP);
            }
            door.setTopHalf(true);
            topHalf.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
            door.setTopHalf(false);
            bottomHalf.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
        } else if (state instanceof Sign) {
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
            Sign sign = (Sign) block.getState();
            int i = 0;
            for (String line : ((Sign) state).getLines()) sign.setLine(i++, line);
            sign.update(true);
        } else if (state instanceof CreatureSpawner) {
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
            ((CreatureSpawner) block.getState()).setSpawnedType(((CreatureSpawner) state).getSpawnedType());
        } else if (state instanceof InventoryHolder) {
            block.setTypeId(state.getTypeId(), false);
            // Container to receive the inventory
            Inventory container = ((InventoryHolder) block.getState()).getInventory();
            container.setContents(contents.toArray(new ItemStack[0]));
            block.setData(state.getData().getData(), false);
        } else if (state.getData() instanceof PistonExtensionMaterial) {
            PistonExtensionMaterial extension = (PistonExtensionMaterial) state.getData();
            Block piston = block.getRelative(extension.getAttachedFace());
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
            if (altState != null) {
                piston.setTypeIdAndData(altState.getTypeId(), altState.getData().getData(), false);
            }
        } else if (state.getData() instanceof Attachable) {
            Block attachedBlock = block.getRelative(((Attachable) state.getData()).getAttachedFace());
            if (attachedBlock.getTypeId() == 0) {
                attachedBlock.setTypeId(placeholder.getId(), false);
                TownyRegenAPI.addPlaceholder(attachedBlock);
            }
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
        } else {
            if (NeedsPlaceholder.contains(state.getType())) {
                Block blockBelow = block.getRelative(BlockFace.DOWN);
                if (blockBelow.getTypeId() == 0) {
                    if (state.getType().equals(Material.CROPS)) {
                        blockBelow.setTypeId(Material.SOIL.getId(), true);
                    } else {
                        blockBelow.setTypeId(placeholder.getId(), true);
                    }
                    TownyRegenAPI.addPlaceholder(blockBelow);
                }
            }
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), !NeedsPlaceholder.contains(state.getType()));
        }
        TownyRegenAPI.removePlaceholder(block);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : PistonExtensionMaterial(org.bukkit.material.PistonExtensionMaterial) Block(org.bukkit.block.Block) Sign(org.bukkit.block.Sign) Attachable(org.bukkit.material.Attachable) InventoryHolder(org.bukkit.inventory.InventoryHolder) Door(org.bukkit.material.Door) Inventory(org.bukkit.inventory.Inventory) CreatureSpawner(org.bukkit.block.CreatureSpawner)

Example 5 with InventoryHolder

use of org.bukkit.inventory.InventoryHolder in project Denizen-For-Bukkit by DenizenScript.

the class PlayerDragsInInvScriptEvent method onPlayerDragsInInv.

@EventHandler
public void onPlayerDragsInInv(InventoryDragEvent event) {
    if (dEntity.isCitizensNPC(event.getWhoClicked())) {
        return;
    }
    entity = dEntity.getPlayerFrom(event.getWhoClicked());
    inventory = event.getInventory();
    dInv = dInventory.mirrorBukkitInventory(inventory);
    item = new dItem(event.getOldCursor());
    slots = new dList();
    for (Integer slot : event.getInventorySlots()) {
        slots.add(slot.toString());
    }
    raw_slots = new dList();
    for (Integer raw_slot : event.getRawSlots()) {
        raw_slots.add(raw_slot.toString());
    }
    cancelled = event.isCancelled();
    manual_cancelled = false;
    this.event = event;
    fire();
    if (cancelled && manual_cancelled) {
        final InventoryHolder holder = inventory.getHolder();
        new BukkitRunnable() {

            @Override
            public void run() {
                entity.getPlayerEntity().updateInventory();
                if (holder != null && holder instanceof Player) {
                    ((Player) holder).updateInventory();
                }
            }
        }.runTaskLater(DenizenAPI.getCurrentInstance(), 1);
    }
    event.setCancelled(cancelled);
}
Also used : net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) InventoryHolder(org.bukkit.inventory.InventoryHolder) EventHandler(org.bukkit.event.EventHandler)

Aggregations

InventoryHolder (org.bukkit.inventory.InventoryHolder)23 Player (org.bukkit.entity.Player)9 ItemStack (org.bukkit.inventory.ItemStack)9 Inventory (org.bukkit.inventory.Inventory)8 EventHandler (org.bukkit.event.EventHandler)7 Location (org.bukkit.Location)5 Sign (org.bukkit.block.Sign)4 HashMap (java.util.HashMap)3 Block (org.bukkit.block.Block)3 BlockState (org.bukkit.block.BlockState)3 Jukebox (org.bukkit.block.Jukebox)3 BlockInventoryHolder (com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder)2 BlockMobSpawner (com.palmergames.bukkit.towny.regen.block.BlockMobSpawner)2 BlockObject (com.palmergames.bukkit.towny.regen.block.BlockObject)2 BlockSign (com.palmergames.bukkit.towny.regen.block.BlockSign)2 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 Element (net.aufdemrand.denizencore.objects.Element)2 Chunk (org.bukkit.Chunk)2 Material (org.bukkit.Material)2