Skip to content

Commit

Permalink
If using enforce type - set the sign type upon right click.
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Nov 29, 2016
1 parent efb36f0 commit b3c4eef
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/main/java/com/sk89q/craftbook/mechanics/area/simple/Gate.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public class Gate extends AbstractCraftBookMechanic {
/**
* Toggles the gate closest to a location.
*
* @param player
* @param pt
* @param smallSearchSize
* @param player The player
* @param block The base block
* @param smallSearchSize Small or large search radius
* @param close null to toggle, true to close, false to open
*
* @return true if a gate was found and blocks were changed; false otherwise.
Expand All @@ -84,7 +84,7 @@ public boolean toggleGates(LocalPlayer player, Block block, boolean smallSearchS
for (int x1 = x - 1; x1 <= x + 1; x1++) {
for (int y1 = y - 2; y1 <= y + 1; y1++) {
for (int z1 = z - 1; z1 <= z + 1; z1++) {
if (recurseColumn(player, BukkitUtil.toChangedSign(block), block.getWorld().getBlockAt(x1, y1, z1), visitedColumns, close, smallSearchSize)) {
if (recurseColumn(player, BukkitUtil.toChangedSign(block), block.getWorld().getBlockAt(x1, y1, z1), visitedColumns, close, true)) {
foundGate = true;
}
}
Expand All @@ -95,7 +95,7 @@ public boolean toggleGates(LocalPlayer player, Block block, boolean smallSearchS
for (int x1 = x - searchRadius; x1 <= x + searchRadius; x1++) {
for (int y1 = y - searchRadius; y1 <= y + searchRadius*2; y1++) {
for (int z1 = z - searchRadius; z1 <= z + searchRadius; z1++) {
if (recurseColumn(player, BukkitUtil.toChangedSign(block), block.getWorld().getBlockAt(x1, y1, z1), visitedColumns, close, smallSearchSize)) {
if (recurseColumn(player, BukkitUtil.toChangedSign(block), block.getWorld().getBlockAt(x1, y1, z1), visitedColumns, close, false)) {
foundGate = true;
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ private boolean recurseColumn(LocalPlayer player, ChangedSign sign, Block block,

if (!isValidGateBlock(sign, smallSearchSize, new ItemInfo(block), true)) return false;

CraftBookPlugin.logDebugMessage("Found a possible gate column at " + block.getX() + ":" + block.getY() + ":" + block.getZ(), "gates.search");
CraftBookPlugin.logDebugMessage("Found a possible gate column at " + block.getX() + ':' + block.getY() + ':' + block.getZ(), "gates.search");

int x = block.getX();
int z = block.getZ();
Expand All @@ -144,7 +144,7 @@ private boolean recurseColumn(LocalPlayer player, ChangedSign sign, Block block,
if (close == null)
close = !isValidGateBlock(sign, smallSearchSize, new ItemInfo(block.getWorld().getBlockAt(x, column.getStartingY() - 1, z)), true);

CraftBookPlugin.logDebugMessage("Valid column at " + block.getX() + ":" + block.getY() + ":" + block.getZ() + " is being " + (close ? "closed" : "opened"), "gates.search");
CraftBookPlugin.logDebugMessage("Valid column at " + block.getX() + ':' + block.getY() + ':' + block.getZ() + " is being " + (close ? "closed" : "opened"), "gates.search");
CraftBookPlugin.logDebugMessage("Column Top: " + column.getStartingY() + " End: " + column.getEndingY(), "gates.search");
// Recursively go to connected fence blocks of the same level
// and 'close' or 'open' them
Expand All @@ -155,7 +155,7 @@ private boolean recurseColumn(LocalPlayer player, ChangedSign sign, Block block,
* Actually does the closing/opening. Also recurses to nearby columns.
*
* @param player The player.
* @param signBlock The sign block.
* @param sign The sign block.
* @param block The top point of the gate.
* @param close To open or close.
* @param visitedColumns Previously searched columns.
Expand All @@ -171,7 +171,7 @@ private boolean toggleColumn(LocalPlayer player, ChangedSign sign, Block block,
else
item = new ItemInfo(Material.AIR, 0);

CraftBookPlugin.logDebugMessage("Setting column at " + block.getX() + ":" + block.getY() + ":" + block.getZ() + " to " + item.toString(), "gates.search");
CraftBookPlugin.logDebugMessage("Setting column at " + block.getX() + ':' + block.getY() + ':' + block.getZ() + " to " + item.toString(), "gates.search");

for (Vector bl : column.getRegion()) {

Expand Down Expand Up @@ -206,15 +206,15 @@ private boolean toggleColumn(LocalPlayer player, ChangedSign sign, Block block,
else if (close && canPassThrough(sign, smallSearchSize, blo) && isValidGateBlock(sign, smallSearchSize, item, true) && !item.isSame(blo))
removeBlocks(sign, 1);
blo.setTypeIdAndData(item.getId(), (byte) item.getData(), true);
} else if (close && !hasEnoughBlocks(sign, otherSign) && isValidGateBlock(sign, smallSearchSize, item, true))
} else if (!hasEnoughBlocks(sign, otherSign) && isValidGateBlock(sign, smallSearchSize, item, true))
if (player != null) {
player.printError("mech.not-enough-blocks");
return false;
}
} else
blo.setTypeIdAndData(item.getId(), (byte) item.getData(), true);

CraftBookPlugin.logDebugMessage("Set block " + bl.getX() + ":" + bl.getY() + ":" + bl.getZ() + " to " + item.toString(), "gates.search");
CraftBookPlugin.logDebugMessage("Set block " + bl.getX() + ':' + bl.getY() + ':' + bl.getZ() + " to " + item.toString(), "gates.search");

recurseColumn(player, sign, blo.getRelative(1, 0, 0), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, blo.getRelative(-1, 0, 0), visitedColumns, close, smallSearchSize);
Expand All @@ -237,7 +237,7 @@ else if (close && canPassThrough(sign, smallSearchSize, blo) && isValidGateBlock
/**
* Raised when a block is right clicked.
*
* @param event
* @param event SignClickEvent
*/
@EventHandler(priority = EventPriority.HIGH)
public void onRightClick(SignClickEvent event) {
Expand Down Expand Up @@ -270,6 +270,11 @@ public void onRightClick(SignClickEvent event) {
amount = Math.min(5, event.getPlayer().getItemInHand().getAmount());
addBlocks(sign, amount);

if (enforceType) {
sign.setLine(0, player.getHeldItemInfo().toString());
sign.update(false);
}

if (!(event.getPlayer().getGameMode() == GameMode.CREATIVE))
if (event.getPlayer().getItemInHand().getAmount() <= amount)
event.getPlayer().setItemInHand(null);
Expand Down Expand Up @@ -343,7 +348,7 @@ public void onSignChange(SignChangeEvent event) {
}
// get the material that this gate should toggle and verify it
String line0 = event.getLine(0).trim();
if (line0 != null && !line0.isEmpty()) {
if (!line0.isEmpty()) {
if (!isValidGateBlock(new ItemInfo(line0))) {
player.printError("Line 1 needs to be a valid block id.");
SignUtil.cancelSign(event);
Expand All @@ -365,7 +370,7 @@ else if (!event.getLine(3).equalsIgnoreCase("infinite"))
}
// get the material that this gate should toggle and verify it
String line0 = event.getLine(0).trim();
if (line0 != null && !line0.isEmpty()) {
if (!line0.isEmpty()) {
if (!isValidGateBlock(new ItemInfo(line0))) {
player.printError("mech.gate.valid-item");
SignUtil.cancelSign(event);
Expand All @@ -389,7 +394,7 @@ public boolean isValidGateBlock(ItemInfo block) {
/**
* Checks if a block can be used in gate.
*
* @param signBlock The sign block.
* @param sign The sign block.
* @param smallSearchSize Search small or large.
* @param block The block to check.
* @param check Should search.
Expand Down Expand Up @@ -483,8 +488,8 @@ public ItemInfo getGateBlock(ChangedSign sign, boolean smallSearchSize) {
for (int x1 = x - 1; x1 <= x + 1; x1++) {
for (int y1 = y - 2; y1 <= y + 1; y1++) {
for (int z1 = z - 1; z1 <= z + 1; z1++) {
if (getFirstBlock(sign, sign.getSign().getBlock().getWorld().getBlockAt(x1, y1, z1), smallSearchSize) != null) {
gateBlock = new ItemInfo(getFirstBlock(sign, sign.getSign().getBlock().getWorld().getBlockAt(x1, y1, z1), smallSearchSize));
if (getFirstBlock(sign, sign.getSign().getBlock().getWorld().getBlockAt(x1, y1, z1), true) != null) {
gateBlock = new ItemInfo(getFirstBlock(sign, sign.getSign().getBlock().getWorld().getBlockAt(x1, y1, z1), true));
}
}
}
Expand All @@ -493,15 +498,15 @@ public ItemInfo getGateBlock(ChangedSign sign, boolean smallSearchSize) {
for (int x1 = x - searchRadius; x1 <= x + searchRadius; x1++) {
for (int y1 = y - searchRadius; y1 <= y + searchRadius*2; y1++) {
for (int z1 = z - searchRadius; z1 <= z + searchRadius; z1++) {
if (getFirstBlock(sign, sign.getSign().getBlock().getWorld().getBlockAt(x1, y1, z1), smallSearchSize) != null) {
gateBlock = new ItemInfo(getFirstBlock(sign, sign.getSign().getBlock().getWorld().getBlockAt(x1, y1, z1), smallSearchSize));
if (getFirstBlock(sign, sign.getSign().getBlock().getWorld().getBlockAt(x1, y1, z1), false) != null) {
gateBlock = new ItemInfo(getFirstBlock(sign, sign.getSign().getBlock().getWorld().getBlockAt(x1, y1, z1), false));
}
}
}
}
}

if(enforceType && gateBlock != null && gateBlock.getType() != Material.AIR && sign != null) {
if(enforceType && gateBlock != null && gateBlock.getType() != Material.AIR) {
sign.setLine(0, gateBlock.toString());
sign.update(false);
}
Expand Down Expand Up @@ -546,7 +551,7 @@ public int getBlocks(ChangedSign s, ChangedSign other) {

if (s.getLine(3).equalsIgnoreCase("infinite") || other != null && other.getLine(3).equalsIgnoreCase("infinite"))
return 0;
int curBlocks = 0;
int curBlocks;
try {
curBlocks = Integer.parseInt(s.getLine(3));
if(other != null && other.getLine(0).equals(s.getLine(0))) {
Expand Down Expand Up @@ -669,13 +674,13 @@ public int hashCode() {
}
}

boolean allowRedstone;
boolean limitColumns;
int columnLimit;
List<ItemInfo> blocks;
boolean enforceType;
int columnHeight;
int searchRadius;
private boolean allowRedstone;
private boolean limitColumns;
private int columnLimit;
private List<ItemInfo> blocks;
private boolean enforceType;
private int columnHeight;
private int searchRadius;

@Override
public void loadConfiguration (YAMLProcessor config, String path) {
Expand Down

0 comments on commit b3c4eef

Please sign in to comment.