Search in sources :

Example 1 with BlockMap

use of com.github.jikoo.planarwrappers.collections.BlockMap in project Easterlyn by Easterlyn.

the class Machine method reassemble.

/**
 * For use when Machine would be affected by an explosion without CreeperHeal enabled.
 *
 * <p>Machines can be abused in combination with CreeperHeal, so instead of flat out cancelling
 * the event, making it rather difficult to do anything fun like drop a meteor on someone's
 * computer, we'll hook it and play nice.
 *
 * @param storage the ConfigurationSection of data specific to the given Machine
 */
public void reassemble(@NotNull ConfigurationSection storage) {
    final Location key = getKey(storage);
    final BlockMap<ItemStack[]> inventories = new BlockMap<>();
    for (Block block : getMachines().getMachineBlocks(key.getBlock())) {
        if (block.getState() instanceof InventoryHolder) {
            InventoryHolder inventoryHolder = (InventoryHolder) block.getState();
            inventories.put(block, inventoryHolder.getInventory().getContents());
            inventoryHolder.getInventory().clear();
        }
        block.setType(Material.AIR, false);
    }
    Bukkit.getScheduler().scheduleSyncDelayedTask(getMachines(), () -> {
        assemble(key.getBlock(), getDirection(storage), storage);
        for (Entry<Block, ItemStack[]> e : inventories.entrySet()) {
            try {
                ((InventoryHolder) e.getKey().getState()).getInventory().setContents(e.getValue());
            } catch (Exception e1) {
                for (ItemStack is : e.getValue()) {
                    if (key.getWorld() != null && is != null && is.getType() != Material.AIR) {
                        key.getWorld().dropItem(key, is);
                    }
                }
            }
        }
    });
}
Also used : BlockMap(com.github.jikoo.planarwrappers.collections.BlockMap) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Location(org.bukkit.Location)

Aggregations

BlockMap (com.github.jikoo.planarwrappers.collections.BlockMap)1 Location (org.bukkit.Location)1 Block (org.bukkit.block.Block)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1 ItemStack (org.bukkit.inventory.ItemStack)1