package info.bytecraft.fill;
import info.bytecraft.chestbless.ChestBless;
import java.util.HashMap;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
public class Fill {
private HashMap<Location, Material> undo = new HashMap<Location, Material>();
private int x1, x2;
private int y1, y2;
private int z1, z2;
private World world;
private Player player;
public Fill(){
setX1(0);
setX2(0);
setY1(0);
setY2(0);
setZ1(0);
setZ2(0);
world = null;
player = null;
}
public Fill(int x1, int x2, int y1, int y2, int z1, int z2, final World world, Player player){
this.setX1(x1); this.setX2(x2);
this.setY1(y1); this.setY2(y2);
this.setZ1(z1); this.setZ2(z2);
this.world = world;
this.player = player;
}
public Fill(Location loc1, Location loc2, final World world, Player player){
this.setX1(loc1.getBlockX()); this.setX2(loc2.getBlockX());
this.setY1(loc1.getBlockY()); this.setY2(loc2.getBlockY());
this.setZ1(loc1.getBlockZ()); this.setZ2(loc2.getBlockZ());
this.world = world; this.player = player;
}
public Player getPlayer() {
return player;
}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public int getZ1() {
return z1;
}
public void setZ1(int z1) {
this.z1 = z1;
}
public int getZ2() {
return z2;
}
public void setZ2(int z2) {
this.z2 = z2;
}
public World getWorld() {
return world;
}
public Location getLocation1(){
return new Location(world, x1, y1, z1);
}
public Location getLocation2(){
return new Location(world, x2, y2, z2);
}
public boolean contains(Location loc){
BlockVector min = new BlockVector(Math.min(x1, x2), Math.min(y1, y2), Math.min(z1, z2));
BlockVector max = new BlockVector(Math.max(x2, x2), Math.max(y1, y2), Math.max(z1, z2));
BlockVector cur = new BlockVector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
return cur.isInAABB(min, max);
}
public int setBlocks(Material mat){
if(mat == null)return 0;
int startX = Math.min(x1, x2);
int endX = Math.max(x1, x2);
int startY = Math.min(y1, y2);
int endY = Math.max(y1, y2);
int startZ = Math.min(z1, z2);
int endZ = Math.max(z1, z2);
undo.clear();
for (int x = startX; x <= endX; x++) {
for (int y = startY; y <= endY; y++) {
for (int z = startZ; z <= endZ; z++) {
Location loc = new Location(world, x,y,z);
Block block = loc.getBlock();
undo.put(loc, block.getType());
if(!ChestBless.isBless(block)){
block.setType(mat);
}
}
}
}
return this.getSize();
}
public int replace(Material mat1, Material mat2){
if(mat1 == null || mat2 == null){
return 0;
}
int startX = Math.min(x1, x2);
int endX = Math.max(x1, x2);
int startY = Math.min(y1, y2);
int endY = Math.max(y1, y2);
int startZ = Math.min(z1, z2);
int endZ = Math.max(z1, z2);
undo.clear();
for (int x = startX; x <= endX; x++) {
for (int y = startY; y <= endY; y++) {
for (int z = startZ; z <= endZ; z++) {
Location loc = new Location(world, x, y ,z);
Block block = loc.getBlock();
undo.put(loc, block.getType());
if(block.getType() == mat1){
if(!ChestBless.isBless(block)){
block.setType(mat2);
}
}
}
}
}
return this.getSize();
}
public int replace(Material mat1, Material mat2, byte data){
if(mat1 == null || mat2 == null){
return 0;
}
undo.clear();
for(int x = Math.min(x2, x2); x <= Math.max(x1, x2); x++){
for(int y = Math.min(y1, y2); y <= Math.max(y1, y2); y++){
for(int z = Math.min(z1, z2); z <= Math.max(z1, z2); z++){
Location loc = new Location(world, x, y ,z);
Block block = loc.getBlock();
undo.put(loc, block.getType());
if(block.getType() == mat1){
if(!ChestBless.isBless(block)){
block.setType(mat2);
block.setData(data);
}
}
}
}
}
return getSize();
}
public int undo(){
Fill lastFill = FillPlugin.getLastFill(player);
int startX = Math.min(lastFill.x1, lastFill.x2);
int endX = Math.max(lastFill.x1, lastFill.x2);
int startY = Math.min(lastFill.y1, lastFill.y2);
int endY = Math.max(y1, y2);
int startZ = Math.min(lastFill.z1, lastFill.z2);
int endZ = Math.max(lastFill.z1, lastFill.z2);
for (int x = startX; x <= endX; x++) {
for (int y = startY; y <= endY; y++) {
for (int z = startZ; z <= endZ; z++) {
Location loc = new Location(world, x, y ,z);
if(undo.containsKey(loc)){
if(!ChestBless.isBless(loc.getBlock())){
loc.getBlock().setType(undo.get(loc));
}
}
}
}
}
return lastFill.getSize();
}
public int getSize() {
return (((Math.max(x1, x2) - Math.min(x1, x2)) + 1) *
((Math.max(y1, y2) - Math.min(y1, y2)) + 1) *
((Math.max(z1, z2) - Math.min(z1, z2)) + 1));
}
public Fill copy(){
return new Fill(this.getLocation1(), this.getLocation2(), this.world, this.player);
}
public int paste(Location loc1, Location loc2){
return 0;
}
}