Search in sources :

Example 36 with BlockState

use of org.bukkit.block.BlockState in project Towny by ElgarL.

the class TownyRegenAPI method regenChunk.

/**
	 * Regenerate the chunk the player is stood in and store the block data so it can be undone later.
	 * 
	 * @param player
	 */
public static void regenChunk(Player player) {
    try {
        Coord coord = Coord.parseCoord(player);
        World world = player.getWorld();
        Chunk chunk = world.getChunkAt(player.getLocation());
        int maxHeight = world.getMaxHeight();
        Object[][][] snapshot = new Object[16][maxHeight][16];
        for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                for (int y = 0; y < maxHeight; y++) {
                    //Current block to save
                    BlockState state = chunk.getBlock(x, y, z).getState();
                    if (state instanceof org.bukkit.block.Sign) {
                        BlockSign sign = new BlockSign(BukkitTools.getTypeId(state), BukkitTools.getDataData(state), ((org.bukkit.block.Sign) state).getLines());
                        sign.setLocation(state.getLocation());
                        snapshot[x][y][z] = sign;
                    } else if (state instanceof CreatureSpawner) {
                        BlockMobSpawner spawner = new BlockMobSpawner(((CreatureSpawner) state).getSpawnedType());
                        spawner.setLocation(state.getLocation());
                        spawner.setDelay(((CreatureSpawner) state).getDelay());
                        snapshot[x][y][z] = spawner;
                    } else if ((state instanceof InventoryHolder) && !(state instanceof Player)) {
                        BlockInventoryHolder holder = new BlockInventoryHolder(BukkitTools.getTypeId(state), BukkitTools.getDataData(state), ((InventoryHolder) state).getInventory().getContents());
                        holder.setLocation(state.getLocation());
                        snapshot[x][y][z] = holder;
                    } else {
                        snapshot[x][y][z] = new BlockObject(BukkitTools.getTypeId(state), BukkitTools.getDataData(state), state.getLocation());
                    }
                }
            }
        }
        TownyUniverse.getDataSource().getResident(player.getName()).addUndo(snapshot);
        Bukkit.getWorld(player.getWorld().getName()).regenerateChunk(coord.getX(), coord.getZ());
    } catch (NotRegisteredException e) {
    // Failed to get resident
    }
}
Also used : Player(org.bukkit.entity.Player) BlockInventoryHolder(com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) World(org.bukkit.World) Chunk(org.bukkit.Chunk) CreatureSpawner(org.bukkit.block.CreatureSpawner) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) Coord(com.palmergames.bukkit.towny.object.Coord) BlockMobSpawner(com.palmergames.bukkit.towny.regen.block.BlockMobSpawner) BlockState(org.bukkit.block.BlockState) BlockSign(com.palmergames.bukkit.towny.regen.block.BlockSign) BlockObject(com.palmergames.bukkit.towny.regen.block.BlockObject) Sign(org.bukkit.block.Sign) BlockSign(com.palmergames.bukkit.towny.regen.block.BlockSign) InventoryHolder(org.bukkit.inventory.InventoryHolder) BlockInventoryHolder(com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder) BlockObject(com.palmergames.bukkit.towny.regen.block.BlockObject)

Example 37 with BlockState

use of org.bukkit.block.BlockState in project Denizen-For-Bukkit by DenizenScript.

the class StructureGrowsScriptEvent method onStructureGrow.

@EventHandler
public void onStructureGrow(StructureGrowEvent event) {
    world = new dWorld(event.getWorld());
    location = new dLocation(event.getLocation());
    structure = new Element(event.getSpecies().name());
    blocks = new dList();
    new_materials = new dList();
    for (BlockState block : event.getBlocks()) {
        blocks.add(new dLocation(block.getLocation()).identify());
        new_materials.add(dMaterial.getMaterialFrom(block.getType(), block.getRawData()).identify());
    }
    this.event = event;
    cancelled = event.isCancelled();
    fire();
    event.setCancelled(cancelled);
}
Also used : BlockState(org.bukkit.block.BlockState) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) net.aufdemrand.denizen.objects.dWorld(net.aufdemrand.denizen.objects.dWorld) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) EventHandler(org.bukkit.event.EventHandler)

Example 38 with BlockState

use of org.bukkit.block.BlockState in project Minigames by AddstarMC.

the class RecorderData method restoreBlockData.

@SuppressWarnings("deprecation")
public boolean restoreBlockData() {
    File f = new File(plugin.getDataFolder() + "/minigames/" + minigame.getName(false) + "/backup.dat");
    if (!f.exists()) {
        Bukkit.getLogger().info("No backup file found for " + minigame.getName(false));
        return false;
    }
    try {
        BufferedReader br = new BufferedReader(new FileReader(f));
        Map<String, String> args = new HashMap<String, String>();
        String line;
        String[] blocks;
        String[] block;
        World w;
        BlockData bd;
        BlockState state;
        ItemStack[] items;
        String[] sitems;
        ItemStack item;
        Map<String, String> iargs = new HashMap<String, String>();
        while (br.ready()) {
            line = br.readLine();
            blocks = line.split("\\}\\{");
            for (String bl : blocks) {
                args.clear();
                bl = bl.replace("{", "");
                bl = bl.replace("}", "");
                block = bl.split(";");
                for (String b : block) {
                    String[] spl = b.split(":");
                    if (spl.length > 1) {
                        args.put(spl[0], spl[1]);
                    }
                }
                w = Bukkit.getWorld(args.get("world"));
                state = w.getBlockAt(Integer.valueOf(args.get("x")), Integer.valueOf(args.get("y")), Integer.valueOf(args.get("z"))).getState();
                state.setType(Material.getMaterial(args.get("mat")));
                state.setRawData(Byte.valueOf(args.get("data")));
                bd = new BlockData(state, null);
                if (args.containsKey("items")) {
                    if (state.getType() == Material.DISPENSER || state.getType() == Material.DROPPER) {
                        items = new ItemStack[InventoryType.DISPENSER.getDefaultSize()];
                    } else if (state.getType() == Material.HOPPER) {
                        items = new ItemStack[InventoryType.HOPPER.getDefaultSize()];
                    } else if (state.getType() == Material.FURNACE) {
                        items = new ItemStack[InventoryType.FURNACE.getDefaultSize()];
                    } else if (state.getType() == Material.BREWING_STAND) {
                        items = new ItemStack[InventoryType.BREWING.getDefaultSize()];
                    } else {
                        items = new ItemStack[InventoryType.CHEST.getDefaultSize()];
                    }
                    sitems = args.get("items").split("\\)\\(");
                    for (String i : sitems) {
                        i = i.replace("(", "");
                        i = i.replace(")", "");
                        for (String s : i.split("\\|")) {
                            String[] spl = s.split("-");
                            if (spl.length > 1) {
                                iargs.put(s.split("-")[0], s.split("-")[1]);
                            }
                        }
                        item = new ItemStack(Material.getMaterial(iargs.get("item")), Integer.valueOf(iargs.get("c")), Short.valueOf(iargs.get("dur")));
                        if (iargs.containsKey("enc")) {
                            for (String s : iargs.get("enc").split("\\]\\[")) {
                                item.addUnsafeEnchantment(Enchantment.getByName(s.split(",")[0].replace("[", "")), Integer.valueOf(s.split(",")[1].replace("]", "")));
                            }
                        }
                        items[Integer.valueOf(iargs.get("slot"))] = item;
                        iargs.clear();
                    }
                    bd.setItems(items);
                }
                blockdata.put(MinigameUtils.createLocationID(bd.getLocation()), bd);
            }
        }
        br.close();
    } catch (FileNotFoundException e) {
        Bukkit.getLogger().severe("File not found!!!");
        e.printStackTrace();
    } catch (IOException e) {
        Bukkit.getLogger().severe("IO Error!");
        e.printStackTrace();
    }
    return true;
}
Also used : HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) World(org.bukkit.World) BlockState(org.bukkit.block.BlockState) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ItemStack(org.bukkit.inventory.ItemStack) File(java.io.File)

Example 39 with BlockState

use of org.bukkit.block.BlockState in project Minigames by AddstarMC.

the class PulseRedstoneAction method executeNodeAction.

@Override
public void executeNodeAction(MinigamePlayer player, Node node) {
    Material block = Material.REDSTONE_BLOCK;
    if (torch.getFlag())
        block = Material.REDSTONE_TORCH_ON;
    final BlockState last = node.getLocation().getBlock().getState();
    node.getLocation().getBlock().setType(block);
    Bukkit.getScheduler().scheduleSyncDelayedTask(Minigames.plugin, new Runnable() {

        @Override
        public void run() {
            last.update(true);
        }
    }, 20 * time.getFlag());
}
Also used : BlockState(org.bukkit.block.BlockState) Material(org.bukkit.Material)

Example 40 with BlockState

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

the class PrismWorldEvents method onStructureGrow.

/**
     * 
     * @param event
     */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onStructureGrow(final StructureGrowEvent event) {
    String type = "tree-grow";
    final TreeType species = event.getSpecies();
    if (species != null && species.name().toLowerCase().contains("mushroom"))
        type = "mushroom-grow";
    if (!Prism.getIgnore().event(type, event.getWorld()))
        return;
    for (final BlockState block : event.getBlocks()) {
        if (me.botsko.elixr.BlockUtils.isGrowableStructure(block.getType())) {
            String player = "Environment";
            if (event.getPlayer() != null) {
                player = event.getPlayer().getName();
            }
            RecordingQueue.addToQueue(ActionFactory.createGrow(type, block, player));
        }
    }
}
Also used : TreeType(org.bukkit.TreeType) BlockState(org.bukkit.block.BlockState) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BlockState (org.bukkit.block.BlockState)62 Block (org.bukkit.block.Block)27 EventHandler (org.bukkit.event.EventHandler)16 Location (org.bukkit.Location)12 MaterialData (org.bukkit.material.MaterialData)12 BlockFace (org.bukkit.block.BlockFace)8 Material (org.bukkit.Material)7 World (org.bukkit.World)6 Player (org.bukkit.entity.Player)6 ArrayList (java.util.ArrayList)5 ItemStack (org.bukkit.inventory.ItemStack)5 BlockStateChange (me.botsko.prism.events.BlockStateChange)4 GlowBlockState (net.glowstone.block.GlowBlockState)4 CreatureSpawner (org.bukkit.block.CreatureSpawner)4 Sign (org.bukkit.block.Sign)4 Vector (org.bukkit.util.Vector)4 GlowBlock (net.glowstone.block.GlowBlock)3 TreeType (org.bukkit.TreeType)3 InventoryHolder (org.bukkit.inventory.InventoryHolder)3 HashMap (java.util.HashMap)2