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;
}
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
}
}
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"));
}
Aggregations