Improve chest rollbacks

Fixes #533
This commit is contained in:
Brokkonaut
2018-08-02 01:18:43 +02:00
parent 0eb2b0dbc8
commit 3dbb7a6c43
2 changed files with 60 additions and 8 deletions

View File

@@ -8,10 +8,11 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.block.data.Bisected.Half; import org.bukkit.block.data.Bisected.Half;
import org.bukkit.block.data.Bisected;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Bed; import org.bukkit.block.data.type.Bed;
import org.bukkit.block.data.type.Chest;
import org.bukkit.block.data.type.Bed.Part; import org.bukkit.block.data.type.Bed.Part;
import org.bukkit.block.data.type.Door;
import org.bukkit.block.data.type.Piston; import org.bukkit.block.data.type.Piston;
import org.bukkit.block.data.type.PistonHead; import org.bukkit.block.data.type.PistonHead;
import org.bukkit.block.data.type.TechnicalPiston.Type; import org.bukkit.block.data.type.TechnicalPiston.Type;
@@ -187,9 +188,10 @@ public class WorldEditor implements Runnable {
if (leftover > 0 && ca.remove) { if (leftover > 0 && ca.remove) {
throw new WorldEditorException("Not enough space left in " + block.getType(), block.getLocation()); throw new WorldEditorException("Not enough space left in " + block.getType(), block.getLocation());
} }
}
return PerformResult.SUCCESS; return PerformResult.SUCCESS;
} }
return PerformResult.NO_ACTION;
}
} }
if (block.getType() != setBlock.getMaterial() && !replaceAnyway.contains(block.getType())) { if (block.getType() != setBlock.getMaterial() && !replaceAnyway.contains(block.getType())) {
return PerformResult.NO_ACTION; return PerformResult.NO_ACTION;
@@ -219,13 +221,13 @@ public class WorldEditor implements Runnable {
bed2.setPart(bed.getPart() == Part.HEAD ? Part.FOOT : Part.HEAD); bed2.setPart(bed.getPart() == Part.HEAD ? Part.FOOT : Part.HEAD);
secBlock.setBlockData(bed2); secBlock.setBlockData(bed2);
} }
} else if (newData instanceof Door) { } else if (newData instanceof Bisected) {
final Door door = (Door) newData; final Bisected firstPart = (Bisected) newData;
final Block secBlock = door.getHalf() == Half.TOP ? block.getRelative(BlockFace.DOWN) : block.getRelative(BlockFace.UP); final Block secBlock = block.getRelative(firstPart.getHalf() == Half.TOP ? BlockFace.DOWN : BlockFace.UP);
if (secBlock.isEmpty()) { if (secBlock.isEmpty()) {
Door door2 = (Door) door.clone(); Bisected secondPart = (Bisected) firstPart.clone();
door2.setHalf(door.getHalf() == Half.TOP ? Half.BOTTOM : Half.TOP); secondPart.setHalf(firstPart.getHalf() == Half.TOP ? Half.BOTTOM : Half.TOP);
secBlock.setBlockData(door2); secBlock.setBlockData(secondPart);
} }
} else if ((curtype == Material.PISTON || curtype == Material.STICKY_PISTON)) { } else if ((curtype == Material.PISTON || curtype == Material.STICKY_PISTON)) {
Piston piston = (Piston) newData; Piston piston = (Piston) newData;
@@ -247,6 +249,14 @@ public class WorldEditor implements Runnable {
piston.setExtended(true); piston.setExtended(true);
secBlock.setBlockData(piston); secBlock.setBlockData(piston);
} }
} else if (newData instanceof Chest) {
Chest chest = (Chest) newData;
if (chest.getType() != org.bukkit.block.data.type.Chest.Type.SINGLE) {
if (getConnectedChest(block) == null) {
chest.setType(org.bukkit.block.data.type.Chest.Type.SINGLE);
block.setBlockData(chest);
}
}
} }
return PerformResult.SUCCESS; return PerformResult.SUCCESS;
} }

View File

@@ -667,4 +667,46 @@ public class BukkitUtils {
public static boolean isBed(Material type) { public static boolean isBed(Material type) {
return bedBlocks.contains(type); return bedBlocks.contains(type);
} }
public static Block getConnectedChest(Block chestBlock) {
// is this a chest?
BlockData blockData = chestBlock.getBlockData();
if (!(blockData instanceof org.bukkit.block.data.type.Chest)) {
return null;
}
// so check if is should have a neighbour
org.bukkit.block.data.type.Chest chestData = (org.bukkit.block.data.type.Chest) blockData;
org.bukkit.block.data.type.Chest.Type chestType = chestData.getType();
if (chestType != org.bukkit.block.data.type.Chest.Type.SINGLE) {
// check if the neighbour exists
BlockFace chestFace = chestData.getFacing();
BlockFace faceToSecondChest;
if (chestFace == BlockFace.WEST) {
faceToSecondChest = BlockFace.NORTH;
} else if (chestFace == BlockFace.NORTH) {
faceToSecondChest = BlockFace.EAST;
} else if (chestFace == BlockFace.EAST) {
faceToSecondChest = BlockFace.SOUTH;
} else if (chestFace == BlockFace.SOUTH) {
faceToSecondChest = BlockFace.WEST;
} else {
return null;
}
org.bukkit.block.data.type.Chest.Type wantedChestType = org.bukkit.block.data.type.Chest.Type.RIGHT;
if (chestType == org.bukkit.block.data.type.Chest.Type.RIGHT) {
faceToSecondChest = faceToSecondChest.getOppositeFace();
wantedChestType = org.bukkit.block.data.type.Chest.Type.LEFT;
}
Block face = chestBlock.getRelative(faceToSecondChest);
if (face.getType() == chestBlock.getType()) {
// check is the neighbour connects to this chest
org.bukkit.block.data.type.Chest otherChestData = (org.bukkit.block.data.type.Chest) face.getBlockData();
if(otherChestData.getType() != wantedChestType || otherChestData.getFacing() != chestFace) {
return null;
}
return face;
}
}
return null;
}
} }