Search in sources :

Example 6 with InventoryHolder

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

the class RollbackScheduler method run.

@Override
public void run() {
    long time = System.nanoTime();
    while (iterator.hasNext()) {
        BlockData bdata = iterator.next();
        bdata.getBlockState().update(true);
        if (System.nanoTime() - time > Minigames.plugin.getConfig().getDouble("regeneration.maxDelay") * 1000000)
            return;
    }
    while (physIterator.hasNext()) {
        BlockData bdata = physIterator.next();
        bdata.getBlockState().update(true);
        if ((bdata.getBlockState().getType() == Material.SIGN_POST || bdata.getBlockState().getType() == Material.WALL_SIGN) && bdata.getBlockState() instanceof Sign) {
            Sign sign = (Sign) bdata.getLocation().getBlock().getState();
            Sign signOld = (Sign) bdata.getBlockState();
            sign.setLine(0, signOld.getLine(0));
            sign.setLine(1, signOld.getLine(1));
            sign.setLine(2, signOld.getLine(2));
            sign.setLine(3, signOld.getLine(3));
            sign.update();
        } else if (bdata.getLocation().getBlock().getState() instanceof InventoryHolder) {
            InventoryHolder block = (InventoryHolder) bdata.getLocation().getBlock().getState();
            if (bdata.getItems() != null)
                block.getInventory().setContents(bdata.getItems().clone());
        } else if (bdata.getBlockState() instanceof FlowerPot) {
            FlowerPot pot = (FlowerPot) bdata.getLocation().getBlock().getState();
            if (bdata.getSpecialData("contents") != null)
                pot.setContents((MaterialData) bdata.getSpecialData("contents"));
        } else if (bdata.getBlockState().getType() == Material.JUKEBOX) {
            Jukebox jbox = (Jukebox) bdata.getLocation().getBlock().getState();
            Jukebox orig = (Jukebox) bdata.getBlockState();
            jbox.setPlaying(orig.getPlaying());
            jbox.update();
        } else if (bdata.getBlockState().getType() == Material.SKULL) {
            Skull skull = (Skull) bdata.getBlockState().getBlock().getState();
            Skull orig = (Skull) bdata.getBlockState();
            if (orig.getOwningPlayer() != null)
                skull.setOwningPlayer(orig.getOwningPlayer());
            skull.setRotation(orig.getRotation());
            skull.setSkullType(orig.getSkullType());
            skull.update();
        }
        if (System.nanoTime() - time > Minigames.plugin.getConfig().getDouble("regeneration.maxDelay") * 1000000)
            return;
    }
    // When rolling back a single player's changes dont change the overall games state
    if (modifier == null) {
        HandlerList.unregisterAll(minigame.getBlockRecorder());
        HandlerList.bakeAll();
        minigame.setState(MinigameState.IDLE);
    }
    task.cancel();
}
Also used : Jukebox(org.bukkit.block.Jukebox) FlowerPot(org.bukkit.block.FlowerPot) Skull(org.bukkit.block.Skull) Sign(org.bukkit.block.Sign) MaterialData(org.bukkit.material.MaterialData) InventoryHolder(org.bukkit.inventory.InventoryHolder)

Example 7 with InventoryHolder

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

the class BasicRecorder method takeItem.

@EventHandler(ignoreCancelled = true)
private void takeItem(PlayerInteractEvent event) {
    MinigamePlayer ply = pdata.getMinigamePlayer(event.getPlayer());
    if (ply == null)
        return;
    if (ply.isInMinigame() && event.getAction() == Action.RIGHT_CLICK_BLOCK && !ply.getMinigame().isSpectator(ply)) {
        Minigame mgm = ply.getMinigame();
        RecorderData d = mgm.getBlockRecorder();
        if (event.getClickedBlock().getState() instanceof InventoryHolder) {
            d.addBlock(event.getClickedBlock().getLocation().getBlock(), ply);
        }
    }
}
Also used : MinigamePlayer(au.com.mineauz.minigames.MinigamePlayer) Minigame(au.com.mineauz.minigames.minigame.Minigame) InventoryHolder(org.bukkit.inventory.InventoryHolder) EventHandler(org.bukkit.event.EventHandler)

Example 8 with InventoryHolder

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

the class RandomItemsContent method fillContainer.

public boolean fillContainer(Random random, DirectionalContainer container, BlockState state, int maxStacks) {
    if (state.getBlock().getState() instanceof InventoryHolder) {
        Inventory inventory = ((InventoryHolder) state.getBlock().getState()).getInventory();
        int size = inventory.getSize();
        for (int i = 0; i < maxStacks; i++) {
            RandomAmountItem item = getRandomItem(random);
            if (item != null) {
                for (ItemStack stack : item.getItemStacks(random)) {
                    // slot can be overriden hence maxStacks can be less than what's expected
                    inventory.setItem(random.nextInt(size), stack);
                }
            }
        }
    } else {
        throw new IllegalArgumentException("Bad container type");
    }
    return true;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory)

Example 9 with InventoryHolder

use of org.bukkit.inventory.InventoryHolder in project BKCommonLib by bergerhealer.

the class CommonEntity method replaceEntity.

@SuppressWarnings("unchecked")
private void replaceEntity(final EntityHandle newInstance) {
    final EntityHandle oldInstance = this.handle;
    if (oldInstance.getRaw() == newInstance.getRaw()) {
        throw new RuntimeException("Can not replace an entity with itself!");
    }
    // Reset entity state
    oldInstance.setDead(true);
    newInstance.setDead(false);
    oldInstance.setValid(false);
    newInstance.setValid(true);
    // *** Bukkit Entity ***
    CraftEntityHandle craftEntity = CraftEntityHandle.createHandle(this.entity);
    craftEntity.setHandle(newInstance);
    if (entity instanceof InventoryHolder) {
        Inventory inv = ((InventoryHolder) entity).getInventory();
        if (CraftInventoryHandle.T.isAssignableFrom(inv)) {
            CraftInventoryHandle cInv = CraftInventoryHandle.createHandle(inv);
            if (IInventoryHandle.T.isAssignableFrom(newInstance.getRaw())) {
                IInventoryHandle iinvHandle = IInventoryHandle.createHandle(newInstance.getRaw());
                cInv.setHandleField(iinvHandle);
            }
        }
    }
    // *** Give the old entity a new Bukkit Entity ***
    oldInstance.setBukkitEntityField(CraftEntityHandle.createCraftEntity(Bukkit.getServer(), oldInstance));
    this.handle = newInstance;
    // *** Replace entity in passenger and vehicle fields ***
    EntityHandle vehicle = newInstance.getVehicle();
    if (vehicle != null) {
        replaceInList(vehicle.getPassengers(), newInstance);
    }
    if (newInstance.getPassengers() != null) {
        for (EntityHandle passenger : newInstance.getPassengers()) {
            if (oldInstance.getRaw() == passenger.getVehicle().getRaw()) {
                passenger.setVehicle(newInstance);
            }
        }
    }
    // *** DataWatcher field of the old Entity ***
    Object dataWatcher = EntityHandle.T.datawatcherField.raw.get(newInstance.getRaw());
    if (dataWatcher != null) {
        DataWatcherHandle.T.owner.set(dataWatcher, newInstance);
    }
    // *** Perform further replacement all over the place in the server ***
    replaceEntityInServer(oldInstance, newInstance);
    // *** Repeat the replacement in the server the next tick to make sure nothing lingers ***
    CommonUtil.nextTick(new Runnable() {

        @Override
        public void run() {
            replaceEntityInServer(oldInstance, newInstance);
        }
    });
    // *** Make sure a controller is set when hooked ***
    if (this.isHooked()) {
        DefaultEntityController controller = new DefaultEntityController();
        controller.bind(this);
    }
}
Also used : CraftEntityHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle) IInventoryHandle(com.bergerkiller.generated.net.minecraft.server.IInventoryHandle) CraftEntityHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle) EntityHandle(com.bergerkiller.generated.net.minecraft.server.EntityHandle) InventoryHolder(org.bukkit.inventory.InventoryHolder) CraftInventoryHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftInventoryHandle) Inventory(org.bukkit.inventory.Inventory)

Example 10 with InventoryHolder

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

the class EssentialsPlayerListener method onInventoryClickEvent.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onInventoryClickEvent(final InventoryClickEvent 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.getWhoClicked());
        final InventoryHolder invHolder = top.getHolder();
        if (invHolder != null && invHolder instanceof HumanEntity) {
            final User invOwner = ess.getUser((Player) invHolder);
            if (user.isInvSee() && (!user.isAuthorized("essentials.invsee.modify") || invOwner.isAuthorized("essentials.invsee.preventmodify") || !invOwner.getBase().isOnline())) {
                event.setCancelled(true);
                refreshPlayer = user.getBase();
            }
        }
    } else if (type == InventoryType.ENDER_CHEST) {
        final User user = ess.getUser((Player) event.getWhoClicked());
        if (user.isEnderSee() && (!user.isAuthorized("essentials.enderchest.modify"))) {
            event.setCancelled(true);
            refreshPlayer = user.getBase();
        }
    } else if (type == InventoryType.WORKBENCH) {
        User user = ess.getUser((Player) event.getWhoClicked());
        if (user.isRecipeSee()) {
            event.setCancelled(true);
            refreshPlayer = user.getBase();
        }
    } else if (type == InventoryType.CHEST && top.getSize() == 9) {
        final User user = ess.getUser((Player) event.getWhoClicked());
        final InventoryHolder invHolder = top.getHolder();
        if (invHolder != null && invHolder instanceof HumanEntity && user.isInvSee()) {
            event.setCancelled(true);
            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