Search in sources :

Example 1 with BlockObject

use of com.palmergames.bukkit.towny.regen.block.BlockObject in project Towny by ElgarL.

the class PlotBlockData method restoreNextBlock.

/**
	 * Reverts an area to the stored image.
	 * 
	 * @return true if there are more blocks to check.
	 */
public boolean restoreNextBlock() {
    Block block = null;
    int x, y, z, blockId, reverse, scale;
    int worldx = getX() * size, worldz = getZ() * size;
    BlockObject storedData;
    World world = this.townBlock.getWorldCoord().getBukkitWorld();
    if (!world.isChunkLoaded(BukkitTools.calcChunk(getX()), BukkitTools.calcChunk(getZ())))
        return true;
    //Scale for the number of elements
    switch(version) {
        case 1:
        case 2:
            scale = 2;
            break;
        default:
            scale = 1;
    }
    reverse = (blockList.size() - blockListRestored) / scale;
    while (reverse > 0) {
        //regen bottom up to stand a better chance of restoring tree's and plants.
        reverse--;
        y = height - (reverse % height);
        x = (int) (reverse / height) % size;
        z = ((int) (reverse / height) / size) % size;
        block = world.getBlockAt(worldx + x, y, worldz + z);
        blockId = BukkitTools.getTypeId(block);
        storedData = getStoredBlockData((blockList.size() - 1) - blockListRestored);
        // Increment based upon number of elements
        blockListRestored += scale;
        // and return as done.
        if ((blockId != storedData.getTypeId())) {
            Material mat = BukkitTools.getMaterial(storedData.getTypeId());
            if ((mat == null) || ((mat != null) && !this.townBlock.getWorld().isPlotManagementIgnoreIds(mat.name()))) {
                try {
                    //restore based upon version
                    switch(version) {
                        case 1:
                        case 2:
                            BukkitTools.setTypeIdAndData(block, storedData.getTypeId(), storedData.getData(), false);
                            break;
                        default:
                            BukkitTools.setTypeId(block, storedData.getTypeId(), false);
                    }
                } catch (Exception e) {
                }
            } else {
                BukkitTools.setTypeId(block, 0, false);
            }
            return true;
        }
    }
    // reset as we are finished with the regeneration
    resetBlockListRestored();
    return false;
}
Also used : Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Material(org.bukkit.Material) World(org.bukkit.World) BlockObject(com.palmergames.bukkit.towny.regen.block.BlockObject)

Example 2 with BlockObject

use of com.palmergames.bukkit.towny.regen.block.BlockObject 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 3 with BlockObject

use of com.palmergames.bukkit.towny.regen.block.BlockObject in project Towny by ElgarL.

the class TownyRegenAPI method regenUndo.

/**
	 * Restore the relevant chunk using the snapshot data stored in the resident
	 * object.
	 * 
	 * @param snapshot
	 * @param resident
	 */
public static void regenUndo(Object[][][] snapshot, Resident resident) {
    BlockObject key = ((BlockObject) snapshot[0][0][0]);
    World world = key.getLocation().getWorld();
    Chunk chunk = key.getLocation().getChunk();
    int maxHeight = world.getMaxHeight();
    for (int x = 0; x < 16; x++) {
        for (int z = 0; z < 16; z++) {
            for (int y = 0; y < maxHeight; y++) {
                // Snapshot data we need to update the world.
                Object state = snapshot[x][y][z];
                // The block we will be updating
                Block block = chunk.getBlock(x, y, z);
                if (state instanceof BlockSign) {
                    BlockSign signData = (BlockSign) state;
                    BukkitTools.setTypeIdAndData(block, signData.getTypeId(), signData.getData(), false);
                    Sign sign = (Sign) block.getState();
                    int i = 0;
                    for (String line : signData.getLines()) sign.setLine(i++, line);
                    sign.update(true);
                } else if (state instanceof BlockMobSpawner) {
                    BlockMobSpawner spawnerData = (BlockMobSpawner) state;
                    BukkitTools.setTypeIdAndData(block, spawnerData.getTypeId(), spawnerData.getData(), false);
                    ((CreatureSpawner) block.getState()).setSpawnedType(spawnerData.getSpawnedType());
                    ((CreatureSpawner) block.getState()).setDelay(spawnerData.getDelay());
                } else if ((state instanceof BlockInventoryHolder) && !(state instanceof Player)) {
                    BlockInventoryHolder containerData = (BlockInventoryHolder) state;
                    BukkitTools.setTypeIdAndData(block, containerData.getTypeId(), containerData.getData(), false);
                    // Container to receive the inventory
                    InventoryHolder container = (InventoryHolder) block.getState();
                    // Contents we are respawning.						
                    if (containerData.getItems().length > 0)
                        container.getInventory().setContents(containerData.getItems());
                } else {
                    BlockObject blockData = (BlockObject) state;
                    BukkitTools.setTypeIdAndData(block, blockData.getTypeId(), blockData.getData(), false);
                }
            }
        }
    }
    TownyMessaging.sendMessage(BukkitTools.getPlayerExact(resident.getName()), TownySettings.getLangString("msg_undo_complete"));
}
Also used : BlockInventoryHolder(com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder) Player(org.bukkit.entity.Player) World(org.bukkit.World) Chunk(org.bukkit.Chunk) BlockMobSpawner(com.palmergames.bukkit.towny.regen.block.BlockMobSpawner) BlockSign(com.palmergames.bukkit.towny.regen.block.BlockSign) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) 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)

Aggregations

BlockObject (com.palmergames.bukkit.towny.regen.block.BlockObject)3 World (org.bukkit.World)3 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)2 BlockInventoryHolder (com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder)2 BlockMobSpawner (com.palmergames.bukkit.towny.regen.block.BlockMobSpawner)2 BlockSign (com.palmergames.bukkit.towny.regen.block.BlockSign)2 Chunk (org.bukkit.Chunk)2 Block (org.bukkit.block.Block)2 Sign (org.bukkit.block.Sign)2 Player (org.bukkit.entity.Player)2 InventoryHolder (org.bukkit.inventory.InventoryHolder)2 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)1 Coord (com.palmergames.bukkit.towny.object.Coord)1 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)1 Material (org.bukkit.Material)1 BlockState (org.bukkit.block.BlockState)1 CreatureSpawner (org.bukkit.block.CreatureSpawner)1