Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class EntitySize extends EntityProperty<ElementTag> {
// <--[property]
// @object EntityTag
// @name size
// @input ElementTag
// @input ElementTag(Number)
// @description
// Controls the size of an entity.
// Cube-type (slime, magma cube, sulfur cube) mob sizes are between 1 and 127.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,32 @@
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.objects.properties.material.MaterialDirectional;
import com.denizenscript.denizen.utilities.PaperAPITools;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.scripts.commands.generator.*;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.Tag;
import org.bukkit.block.*;

public class SignCommand extends AbstractCommand {

public static final boolean SIGN_SIDES_SUPPORTED = NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20);

public SignCommand() {
setName("sign");
setSyntax("sign (type:{automatic}/sign_post/wall_sign) (material:<material>) [<line>|...] [<location>] (direction:north/east/south/west)");
setSyntax("sign (type:{automatic}/sign_post/wall_sign/hanging/hanging_wall) (material:<material>) (side:{both}/front/back) [<line>|...] [<location>] (direction:north/east/south/west)");
setRequiredArguments(1, 5);
isProcedural = false;
autoCompile();
}

// <--[command]
// @Name Sign
// @Syntax sign (type:{automatic}/sign_post/wall_sign) (material:<material>) [<line>|...] [<location>] (direction:north/east/south/west)
// @Syntax sign (type:{automatic}/sign_post/wall_sign/hanging/hanging_wall) (material:<material>) (side:{both}/front/back) [<line>|...] [<location>] (direction:north/east/south/west)
// @Required 1
// @Maximum 5
// @Short Modifies a sign.
Expand All @@ -41,6 +39,9 @@ public SignCommand() {
// @Description
// Modifies a sign that replaces the text shown on it. If no sign is at the location, it replaces the location with the modified sign.
//
// For MC 1.20+, optionally specify a side to set the text of. If 'both' is used, the first four entries in the 'line' argument will be used on the front, and the second four on the back.
// If 'front' or 'back' is specified, sets the lines on that side while leaving the other one as-is.
//
// Specify 'automatic' as a type to use whatever sign type and direction is already placed there.
// If there is not already a sign there, defaults to a sign_post.
//
Expand All @@ -59,6 +60,14 @@ public SignCommand() {
// - sign "Hello|this is|some|text" <context.location>
//
// @Usage
// Use to edit some text on the front and back of an existing sign.
// - sign side:both "Hi!|This is|the|front.|This|is|the|back." <context.location>
//
// @Usage
// Use to edit some text on just the back of an existing sign.
// - sign side:back "This is|the back.|The front|is unchanged." <context.location>
//
// @Usage
// Use to show the time on a sign and ensure that it points north.
// - sign "I point|North.|System Time<&co>|<util.time_now.formatted>" <[location]> direction:north
//
Expand All @@ -73,127 +82,147 @@ public void addCustomTabCompletions(TabCompletionsBuilder tab) {
tab.addNotesOfType(LocationTag.class);
}

private enum Type {AUTOMATIC, SIGN_POST, WALL_SIGN}
public enum Type {AUTOMATIC, SIGN_POST, WALL_SIGN, HANGING, HANGING_WALL}

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.class)) {
scriptEntry.addObject("type", arg.asElement());
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class).setPrefix("location"));
}
else if (!scriptEntry.hasObject("direction")
&& arg.matchesPrefix("direction", "dir")) {
scriptEntry.addObject("direction", arg.asElement());
public enum Side {BOTH, FRONT, BACK}

public static void autoExecute(ScriptEntry scriptEntry,
@ArgName("type") @ArgPrefixed @ArgDefaultText("automatic") Type type,
@ArgName("material") @ArgPrefixed @ArgDefaultNull MaterialTag material,
@ArgName("side") @ArgPrefixed @ArgDefaultNull Side side,
@ArgName("text") @ArgLinear @ArgDefaultNull ListTag text,
@ArgName("location") @ArgLinear @ArgDefaultNull LocationTag location,
@ArgName("direction") @ArgPrefixed @ArgDefaultNull String direction) {
if (location == null) {
throw new InvalidArgumentsRuntimeException("Must specify a Sign location!");
}
if (text == null) {
throw new InvalidArgumentsRuntimeException("Must specify sign text!");
}
Block sign = location.getBlock();
if (type != Type.AUTOMATIC || !isAnySign(sign.getType())) {
if (type == Type.WALL_SIGN || (SIGN_SIDES_SUPPORTED && (type == Type.HANGING || type == Type.HANGING_WALL))) {
BlockFace bf;
if (direction != null) {
bf = Utilities.chooseSignRotation(direction);
}
else {
bf = Utilities.chooseSignRotation(sign);
}
if (type == Type.WALL_SIGN) {
setWallSign(sign, bf, material);
}
else if (type == Type.HANGING) {
setHangingSign(sign, bf, material);
}
else {
setHangingWallSign(sign, bf, material);
}
}
else if (!scriptEntry.hasObject("material")
&& arg.matchesPrefix("material")
&& arg.matchesArgumentType(MaterialTag.class)) {
scriptEntry.addObject("material", arg.asType(MaterialTag.class));
else {
sign.setType(material == null ? Material.OAK_SIGN : material.getMaterial(), false);
if (direction != null) {
Utilities.setSignRotation(sign.getState(), direction);
}
}
else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", arg.asType(ListTag.class));
}
else if (!isAnySign(sign.getType())) {
if (sign.getRelative(BlockFace.DOWN).getType().isSolid()) {
sign.setType(material == null ? Material.OAK_SIGN : material.getMaterial(), false);
}
else {
arg.reportUnhandled();
BlockFace bf = Utilities.chooseSignRotation(sign);
setWallSign(sign, bf, material);
}
}
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a Sign location!");
Sign signBlock = (Sign) sign.getState();
String[] lines4 = text.toArray(new String[4]);
String[] lines8 = text.toArray(new String[8]);
if (!SIGN_SIDES_SUPPORTED || side == Side.FRONT) {
for (int n = 0; n < 4; n++) {
PaperAPITools.instance.setSignLine(signBlock, n, lines4[n]);
}
}
if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Must specify sign text!");
else if (side == Side.BACK) {
for (int n = 0; n < 4; n++) {
PaperAPITools.instance.setSignBackLine(signBlock, n, lines4[n]);
}
}
scriptEntry.defaultObject("type", new ElementTag(Type.AUTOMATIC));
else {
for (int n = 0; n < 4; n++) {
PaperAPITools.instance.setSignLine(signBlock, n, lines8[n]);
}
for (int n = 4; n < 8; n++) {
PaperAPITools.instance.setSignBackLine(signBlock, n, lines8[n]);
}
}
signBlock.update();
}

public void setWallSign(Block sign, BlockFace bf, MaterialTag material) {
public static void setWallSign(Block sign, BlockFace bf, MaterialTag material) {
sign.setType(material == null ? Material.OAK_WALL_SIGN : material.getMaterial(), false);
MaterialTag signMaterial = new MaterialTag(sign);
MaterialDirectional.getFrom(signMaterial).setFacing(bf);
sign.setBlockData(signMaterial.getModernData());
}

public static void setHangingSign(Block sign, BlockFace bf, MaterialTag material) {
sign.setType(material == null ? Material.OAK_HANGING_SIGN : material.getMaterial(), false);
MaterialTag signMaterial = new MaterialTag(sign);
MaterialDirectional.getFrom(signMaterial).setFacing(bf);
sign.setBlockData(signMaterial.getModernData());
}

public static void setHangingWallSign(Block sign, BlockFace bf, MaterialTag material) {
sign.setType(material == null ? Material.OAK_WALL_HANGING_SIGN : material.getMaterial(), false);
MaterialTag signMaterial = new MaterialTag(sign);
MaterialDirectional.getFrom(signMaterial).setFacing(bf);
sign.setBlockData(signMaterial.getModernData());
}

public static boolean isStandingSign(Material material) {
switch (material) {
case CRIMSON_SIGN:
case WARPED_SIGN:
case ACACIA_SIGN:
case BIRCH_SIGN:
case DARK_OAK_SIGN:
case JUNGLE_SIGN:
case OAK_SIGN:
case SPRUCE_SIGN:
for (Material signType : Tag.STANDING_SIGNS.getValues()) {
if (signType == material) {
return true;
default:
return false;
}
}
return false;
}

public static boolean isWallSign(Material material) {
switch (material) {
case CRIMSON_WALL_SIGN:
case WARPED_WALL_SIGN:
case ACACIA_WALL_SIGN:
case BIRCH_WALL_SIGN:
case DARK_OAK_WALL_SIGN:
case JUNGLE_WALL_SIGN:
case OAK_WALL_SIGN:
case SPRUCE_WALL_SIGN:
for (Material signType : Tag.WALL_SIGNS.getValues()) {
if (signType == material) {
return true;
default:
return false;
}
}
return false;
}

public static boolean isAnySign(Material material) {
return isStandingSign(material) || isWallSign(material);
}

@Override
public void execute(final ScriptEntry scriptEntry) {
String direction = scriptEntry.hasObject("direction") ? ((ElementTag) scriptEntry.getObject("direction")).asString() : null;
ElementTag typeElement = scriptEntry.getElement("type");
ListTag text = scriptEntry.getObjectTag("text");
LocationTag location = scriptEntry.getObjectTag("location");
MaterialTag material = scriptEntry.getObjectTag("material");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), typeElement, location, db("direction", direction), material, text);
public static boolean isHangingSign(Material material) {
if (!SIGN_SIDES_SUPPORTED) {
return false;
}
Type type = Type.valueOf(typeElement.asString().toUpperCase());
Block sign = location.getBlock();
if (type != Type.AUTOMATIC || !isAnySign(sign.getType())) {
if (type == Type.WALL_SIGN) {
BlockFace bf;
if (direction != null) {
bf = Utilities.chooseSignRotation(direction);
}
else {
bf = Utilities.chooseSignRotation(sign);
}
setWallSign(sign, bf, material);
}
else {
sign.setType(material == null ? Material.OAK_SIGN : material.getMaterial(), false);
if (direction != null) {
Utilities.setSignRotation(sign.getState(), direction);
}
for (Material signType : Tag.CEILING_HANGING_SIGNS.getValues()) {
if (signType == material) {
return true;
}
}
else if (!isAnySign(sign.getType())) {
if (sign.getRelative(BlockFace.DOWN).getType().isSolid()) {
sign.setType(material == null ? Material.OAK_SIGN : material.getMaterial(), false);
}
else {
BlockFace bf = Utilities.chooseSignRotation(sign);
setWallSign(sign, bf, material);
return false;
}

public static boolean isHangingWallSign(Material material) {
if (!SIGN_SIDES_SUPPORTED) {
return false;
}
for (Material signType : Tag.WALL_HANGING_SIGNS.getValues()) {
if (signType == material) {
return true;
}
}
BlockState signState = sign.getState();
Utilities.setSignLines((Sign) signState, text.toArray(new String[4]));
return false;
}

public static boolean isAnySign(Material material) {
return isStandingSign(material) || isWallSign(material) || isHangingSign(material) || isHangingWallSign(material);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,6 @@ public static boolean checkLocation(Location baseLocation, Location theLocation,
return baseLocation.distanceSquared(theLocation) < theLeeway * theLeeway;
}

public static void setSignLines(Sign sign, String[] lines) {
for (int n = 0; n < 4; n++) {
PaperAPITools.instance.setSignLine(sign, n, lines[n]);
}
sign.update();
}

public static BlockFace chooseSignRotation(Block signBlock) {
BlockFace[] blockFaces = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
for (BlockFace blockFace : blockFaces) {
Expand All @@ -401,13 +394,12 @@ public static BlockFace chooseSignRotation(String direction) {
return blockFace;
}
}
switch (dirUpper.charAt(0)) {
case 'N': return BlockFace.NORTH;
case 'S': return BlockFace.SOUTH;
case 'E': return BlockFace.EAST;
case 'W': return BlockFace.WEST;
}
return BlockFace.SOUTH;
return switch (dirUpper.charAt(0)) {
case 'N' -> BlockFace.NORTH;
case 'E' -> BlockFace.EAST;
case 'W' -> BlockFace.WEST;
default -> BlockFace.SOUTH;
};
}

public static void setSignRotation(BlockState signState, String direction) {
Expand Down