Skip to content

Commit

Permalink
add tag location.other_block
Browse files Browse the repository at this point in the history
needs testing
  • Loading branch information
mcmonkey4eva committed Mar 6, 2019
1 parent 463b8d2 commit ef03b09
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
54 changes: 54 additions & 0 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Attachable;
import org.bukkit.material.Door;
import org.bukkit.material.MaterialData;
import org.bukkit.util.BlockIterator;
import org.bukkit.util.Vector;
Expand Down Expand Up @@ -2283,6 +2284,59 @@ else if (this.getWorldName().equalsIgnoreCase(toLocation.getWorldName())) {
}
}

// <--[tag]
// @attribute <l@location.other_block>
// @returns dLocation
// @description
// If the location is part of a double-block structure
// (double chests, doors, beds, etc), returns the location of the other block in the double-block structure.
// -->
if (attribute.startsWith("other_block")) {
BlockState state = getBlock().getState();
if (state instanceof Chest) {
// There is no remotely sane API for this.
InventoryHolder holder = ((Chest) state).getBlockInventory().getHolder();
if (holder instanceof DoubleChest) {
Location left = ((DoubleChest) holder).getLeftSide().getInventory().getLocation();
Location right = ((DoubleChest) holder).getRightSide().getInventory().getLocation();
if (left.getBlockX() == getBlockX() && left.getBlockY() == getBlockY() && left.getBlockZ() == getBlockZ()) {
return new dLocation(right).getAttribute(attribute.fulfill(1));
}
else {
return new dLocation(left).getAttribute(attribute.fulfill(1));
}
}
}
else if (state instanceof Bed
&& NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
// There's no pre-1.13 API for this *at all*, and the new API isn't very sane, but can be used.
boolean isTop = DirectionalBlocksHelper.isBedTopHalf(getBlock());
BlockFace direction = DirectionalBlocksHelper.getFace(getBlock());
if (!isTop) {
direction = direction.getOppositeFace();
}
return new dLocation(this.clone().add(direction.getDirection())).getAttribute(attribute.fulfill(1));
}
else if (state.getData() instanceof Door) {
if (((Door) state.getData()).isTopHalf()) {
return new dLocation(this.clone().subtract(0, 1, 0)).getAttribute(attribute.fulfill(1));
}
else {
return new dLocation(this.clone().add(0, 1, 0)).getAttribute(attribute.fulfill(1));
}
}
else {
if (!attribute.hasAlternative()) {
dB.echoError("Block of type " + getBlock().getType().name() + " isn't supported by other_block.");
}
return null;
}
if (!attribute.hasAlternative()) {
dB.echoError("Block of type " + getBlock().getType().name() + " doesn't have an other block.");
}
return null;
}

// <--[tag]
// @attribute <l@location.custom_name>
// @returns Element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Rotatable;
import org.bukkit.block.data.type.Bed;
import org.bukkit.util.Vector;

public class DirectionalBlocksHelper {

public static boolean isBedTopHalf(Block b) {
if (b.getBlockData() instanceof Bed) {
return ((Bed) b.getBlockData()).getPart() == Bed.Part.HEAD;
}
return false;
}

public static BlockFace getFace(Block b) {
if (b.getBlockData() instanceof Directional) {
return ((Directional) b.getBlockData()).getFacing();
Expand Down

0 comments on commit ef03b09

Please sign in to comment.