Search in sources :

Example 21 with InventoryHolder

use of org.bukkit.inventory.InventoryHolder in project Minigames by AddstarMC.

the class RecorderData method restoreBlocks.

public void restoreBlocks(final MinigamePlayer modifier) {
    // When rolling back a single player's changes dont change the overall games state
    if (modifier == null) {
        minigame.setState(MinigameState.REGENERATING);
    }
    Iterator<BlockData> it = blockdata.values().iterator();
    final List<BlockData> resBlocks = Lists.newArrayList();
    final List<BlockData> addBlocks = Lists.newArrayList();
    while (it.hasNext()) {
        BlockData data = it.next();
        if (modifier == null || modifier.equals(data.getModifier())) {
            it.remove();
            // Clear inventories
            if (data.getLocation().getBlock().getState() instanceof InventoryHolder) {
                InventoryHolder block = (InventoryHolder) data.getLocation().getBlock().getState();
                block.getInventory().clear();
            }
            if (physBlocks.contains(data.getBlockState().getType()) || data.getItems() != null) {
                addBlocks.add(data);
            } else {
                resBlocks.add(data);
            }
        }
    }
    Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {

        @Override
        public void run() {
            Collections.sort(resBlocks, new Comparator<BlockData>() {

                @Override
                public int compare(BlockData o1, BlockData o2) {
                    int comp = Integer.valueOf(o1.getBlockState().getChunk().getX()).compareTo(o2.getBlockState().getChunk().getX());
                    if (comp != 0)
                        return comp;
                    comp = Integer.valueOf(o1.getBlockState().getChunk().getZ()).compareTo(o2.getBlockState().getChunk().getZ());
                    if (comp != 0)
                        return comp;
                    return Integer.valueOf(o1.getBlockState().getY()).compareTo(o2.getBlockState().getY());
                }
            });
            Collections.sort(addBlocks, new Comparator<BlockData>() {

                @Override
                public int compare(BlockData o1, BlockData o2) {
                    int comp = Integer.valueOf(o1.getBlockState().getChunk().getX()).compareTo(o2.getBlockState().getChunk().getX());
                    if (comp != 0)
                        return comp;
                    comp = Integer.valueOf(o1.getBlockState().getChunk().getZ()).compareTo(o2.getBlockState().getChunk().getZ());
                    if (comp != 0)
                        return comp;
                    return Integer.valueOf(o1.getBlockState().getY()).compareTo(o2.getBlockState().getY());
                }
            });
            new RollbackScheduler(resBlocks, addBlocks, minigame, modifier);
        }
    });
}
Also used : InventoryHolder(org.bukkit.inventory.InventoryHolder) Comparator(java.util.Comparator)

Example 22 with InventoryHolder

use of org.bukkit.inventory.InventoryHolder in project Glowstone by GlowstoneMC.

the class GlowMinecart method entityInteract.

@Override
public boolean entityInteract(GlowPlayer player, InteractEntityMessage message) {
    if (message.getAction() == InteractEntityMessage.Action.ATTACK.ordinal()) {
        // todo: damage points
        if (this instanceof InventoryHolder) {
            InventoryHolder inv = (InventoryHolder) this;
            if (inv.getInventory() != null) {
                for (ItemStack drop : inv.getInventory().getContents()) {
                    if (drop == null || drop.getType() == Material.AIR || drop.getAmount() < 1) {
                        continue;
                    }
                    GlowItem item = world.dropItemNaturally(getLocation(), drop);
                    item.setPickupDelay(30);
                    item.setBias(player);
                }
            }
        }
        remove();
    }
    return true;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder)

Example 23 with InventoryHolder

use of org.bukkit.inventory.InventoryHolder in project Essentials by drtshock.

the class EssentialsPlayerListener method onInventoryCloseEvent.

@EventHandler(priority = EventPriority.MONITOR)
public void onInventoryCloseEvent(final InventoryCloseEvent event) {
    Player refreshPlayer = null;
    final Inventory top = event.getView().getTopInventory();
    final InventoryType type = top.getType();
    if (type == InventoryType.PLAYER) {
        final User user = ess.getUser((Player) event.getPlayer());
        user.setInvSee(false);
        refreshPlayer = user.getBase();
    } else if (type == InventoryType.ENDER_CHEST) {
        final User user = ess.getUser((Player) event.getPlayer());
        user.setEnderSee(false);
        refreshPlayer = user.getBase();
    } else if (type == InventoryType.WORKBENCH) {
        final User user = ess.getUser((Player) event.getPlayer());
        if (user.isRecipeSee()) {
            user.setRecipeSee(false);
            event.getView().getTopInventory().clear();
            refreshPlayer = user.getBase();
        }
    } else if (type == InventoryType.CHEST && top.getSize() == 9) {
        final InventoryHolder invHolder = top.getHolder();
        if (invHolder != null && invHolder instanceof HumanEntity) {
            final User user = ess.getUser((Player) event.getPlayer());
            user.setInvSee(false);
            refreshPlayer = user.getBase();
        }
    }
    if (refreshPlayer != null) {
        final Player player = refreshPlayer;
        ess.scheduleSyncDelayedTask(new Runnable() {

            @Override
            public void run() {
                player.updateInventory();
            }
        }, 1);
    }
}
Also used : Player(org.bukkit.entity.Player) InventoryType(org.bukkit.event.inventory.InventoryType) HumanEntity(org.bukkit.entity.HumanEntity) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory) 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