Added plot swap method.

This commit is contained in:
boy0001 2014-10-23 18:09:54 +11:00
parent bcb1ddeccb
commit d2df275000
3 changed files with 184 additions and 5 deletions

View File

@ -0,0 +1,17 @@
package com.intellectualcrafters.plot;
public class BlockWrapper {
public int x;
public int y;
public int z;
public int id;
public byte data;
public BlockWrapper(int x, int y, int z, short id, byte data) {
this.x = x;
this.y = y;
this.z = z;
this.id = id;
this.data = data;
}
}

View File

@ -1,9 +1,16 @@
package com.intellectualcrafters.plot; package com.intellectualcrafters.plot;
import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import com.sk89q.worldedit.blocks.TileEntityBlock;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
/** /**
@ -18,6 +25,12 @@ public class PlotSelection {
private int width; private int width;
private Plot plot; private Plot plot;
private Biome biome;
private BlockState[] tiles = null;
private Location origin = null;
public PlotSelection(int width, World world, Plot plot) { public PlotSelection(int width, World world, Plot plot) {
this.width = width; this.width = width;
@ -36,6 +49,28 @@ public class PlotSelection {
minY = 1, minY = 1,
maxY = world.getMaxHeight(); maxY = world.getMaxHeight();
Block current; Block current;
this.biome = world.getBiome(minX, minZ);
ArrayList<BlockState> states = new ArrayList<BlockState>();
for (int i = (bot.getBlockX() / 16) * 16; i < (16 + ((top.getBlockX() / 16) * 16)); i += 16) {
for (int j = (bot.getBlockZ() / 16) * 16; j < (16 + ((top.getBlockZ() / 16) * 16)); j += 16) {
Chunk chunk = world.getChunkAt(i, j);
for (BlockState tile :chunk.getTileEntities()) {
PlotId id = PlayerFunctions.getPlot(tile.getLocation());
if ((id != null) && id.equals(plot.id)) {
states.add(tile);
}
}
}
}
if (states.size() > 0) {
this.tiles = (BlockState[]) states.toArray();
this.origin = bot;
}
int index = 0; int index = 0;
for (int x = minX; x < maxX; x++) { for (int x = minX; x < maxX; x++) {
@ -49,6 +84,9 @@ public class PlotSelection {
} }
} }
} }
//Yay :D //Yay :D
} }
@ -63,8 +101,125 @@ public class PlotSelection {
public Plot getPlot() { public Plot getPlot() {
return plot; return plot;
} }
public static boolean swap(World world, Plot plot1, Plot plot2) {
Location bot2 = PlotHelper.getPlotBottomLocAbs(world, plot2.getId()).add(1, 0, 1);
Location top2 = PlotHelper.getPlotTopLocAbs(world, plot2.getId());
ArrayList<BlockState> states2 = new ArrayList<BlockState>();
for (int i = (bot2.getBlockX() / 16) * 16; i < (16 + ((top2.getBlockX() / 16) * 16)); i += 16) {
for (int j = (bot2.getBlockZ() / 16) * 16; j < (16 + ((top2.getBlockZ() / 16) * 16)); j += 16) {
Chunk chunk = world.getChunkAt(i, j);
boolean result = chunk.load(false);
if (!result) {
return false;
}
for (BlockState tile :chunk.getTileEntities()) {
PlotId id = PlayerFunctions.getPlot(tile.getLocation());
if ((id != null) && id.equals(plot2.id)) {
states2.add(tile);
}
}
}
}
Location bot1 = PlotHelper.getPlotBottomLocAbs(world, plot1.getId()).add(1, 0, 1);
Location top1 = PlotHelper.getPlotTopLocAbs(world, plot1.getId());
ArrayList<BlockState> states1 = new ArrayList<BlockState>();
for (int i = (bot1.getBlockX() / 16) * 16; i < (16 + ((top1.getBlockX() / 16) * 16)); i += 16) {
for (int j = (bot1.getBlockZ() / 16) * 16; j < (16 + ((top1.getBlockZ() / 16) * 16)); j += 16) {
Chunk chunk = world.getChunkAt(i, j);
boolean result = chunk.load(false);
if (!result) {
return false;
}
for (BlockState tile :chunk.getTileEntities()) {
PlotId id = PlayerFunctions.getPlot(tile.getLocation());
if ((id != null) && id.equals(plot1.id)) {
states1.add(tile);
}
}
}
}
int
minX = bot1.getBlockX(),
maxX = top1.getBlockX(),
minZ = bot1.getBlockZ(),
maxZ = top1.getBlockZ(),
minX2 = bot2.getBlockX(),
minZ2 = bot2.getBlockZ();
boolean canSetFast = PlotHelper.canSetFast;
for (int x = 0; x < maxX - minX; x++) {
for (int z = 0; z < maxZ - minZ; z++) {
for (int y = 1; y < world.getMaxHeight(); y++) {
final Block block1 = world.getBlockAt(x + minX, y, z + minZ);
final Block block2 = world.getBlockAt(x + minX2, y, z + minZ2);
final BlockWrapper b1 = wrapBlock(block1);
final BlockWrapper b2 = wrapBlock(block2);
if (b1.id != b2.id || b1.data != b2.data) {
if (canSetFast) {
try {
SetBlockFast.set(world, b1.x, b1.y, b1.z, b2.id, b2.data);
SetBlockFast.set(world, b2.x, b2.y, b2.z, b1.id, b1.data);
} catch (NoSuchMethodException e) {
PlotHelper.canSetFast = false;
}
}
else {
if (b1.id != b2.id && b1.data != b2.data) {
block1.setTypeIdAndData(b2.id, b2.data, false);
block2.setTypeIdAndData(b1.id, b1.data, false);
}
else if (b1.id != b2.id) {
block1.setTypeId(b2.id);
block2.setTypeId(b1.id);
}
else {
block1.setData(b2.data);
block2.setData(b1.data);
}
}
}
}
}
}
for (BlockState state : states1) {
Location loc = new Location(world,state.getX() - minX + minX2, state.getY(), state.getZ() - minZ + minZ2);
world.getBlockAt(loc).getState().setRawData(state.getRawData());
}
for (BlockState state : states2) {
Location loc = new Location(world,state.getX() - minX2 + minX, state.getY(), state.getZ() - minZ2 + minZ);
world.getBlockAt(loc).getState().setRawData(state.getRawData());
}
return true;
}
private static BlockWrapper wrapBlock(Block block) {
return new BlockWrapper(block.getX(), block.getY(), block.getZ(), (short) block.getTypeId(), block.getData());
}
public void paste(World world, Plot plot) { public void paste(World world, Plot plot) {
Location Location
bot = PlotHelper.getPlotBottomLocAbs(world, plot.getId()), bot = PlotHelper.getPlotBottomLocAbs(world, plot.getId()),
top = PlotHelper.getPlotTopLocAbs(world, plot.getId()); top = PlotHelper.getPlotTopLocAbs(world, plot.getId());
@ -75,6 +230,18 @@ public class PlotSelection {
maxZ = top.getBlockZ(), maxZ = top.getBlockZ(),
minY = 1, minY = 1,
maxY = world.getMaxHeight(); maxY = world.getMaxHeight();
if (this.biome != world.getBiome(minX, minZ)) {
PlotHelper.setBiome(world, plot, this.biome);
}
if (this.origin!=null) {
for (BlockState state : this.tiles) {
Location loc = new Location(world,state.getX() - this.origin.getBlockX() + minX + 1, state.getY() - this.origin.getBlockY() + minY, state.getZ() - this.origin.getBlockZ() + minZ + 1);
world.getBlockAt(loc).getState().setRawData(state.getRawData());
}
}
int index = 0; int index = 0;
PlotBlock current; PlotBlock current;
for (int x = minX; x < maxX; x++) { for (int x = minX; x < maxX; x++) {

View File

@ -4,23 +4,18 @@ import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.intellectualcrafters.jnbt.*; import com.intellectualcrafters.jnbt.*;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
/** /**