/
SignCommand.java
162 lines (145 loc) · 6.65 KB
/
SignCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.nms.interfaces.BlockData;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.MaterialCompat;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.blocks.DirectionalBlocksHelper;
import net.aufdemrand.denizen.utilities.debugging.dB;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Element;
import com.denizenscript.denizencore.objects.aH;
import com.denizenscript.denizencore.objects.dList;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
public class SignCommand extends AbstractCommand {
// <--[command]
// @Name Sign
// @Syntax sign (type:{automatic}/sign_post/wall_sign) ["<line>|..."] [<location>] (direction:n/e/w/s)
// @Required 1
// @Short Modifies a sign.
// @Group world
//
// @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.
// The direction arguement tells which direction the text shown. If a direction is not specified, it defaults to south.
// 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.
//
// @Tags
// <l@location.block.sign_contents>
//
// @Usage
// Use to edit some text on a sign
// - sign type:automatic "Hello|this is|some|text" <player.location>
//
// @Usage
// Use to show the time on a sign that points north
// - sign type:automatic "I point|North.|System Time<&co>|<util.date.time>" l@233,65,123,world direction:n
//
// @Usage
// Use to force a sign to be a wall_sign if no sign is found.
// - sign type:wall_sign "Player<&co>|<player.name>|Online Players<&co>|<server.list_online_players.size>" l@233,65,123,world
//
// -->
private enum Type {AUTOMATIC, SIGN_POST, WALL_SIGN}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpretArguments(scriptEntry.aHArgs)) {
if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", arg.asElement());
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("location", arg.asType(dLocation.class).setPrefix("location"));
}
else if (!scriptEntry.hasObject("direction")
&& arg.matchesPrefix("direction", "dir")) {
scriptEntry.addObject("direction", arg.asElement());
}
else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", arg.asType(dList.class));
}
else {
arg.reportUnhandled();
}
}
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a Sign location!");
}
if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Must specify sign text!");
}
// Default to AUTOMATIC
scriptEntry.defaultObject("type", new Element(Type.AUTOMATIC.name()));
}
public void setWallSign(Block sign, BlockFace bf) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
// TODO: 1.14 - allow new sign types?
sign.setType(MaterialCompat.WALL_SIGN, false);
DirectionalBlocksHelper.setFace(sign, bf);
}
else {
// TODO: 1.14 - allow new sign types?
org.bukkit.material.Sign sgntmp = new org.bukkit.material.Sign(MaterialCompat.WALL_SIGN);
sgntmp.setFacingDirection(bf);
BlockData blockData = NMSHandler.getInstance().getBlockHelper().getBlockData(MaterialCompat.WALL_SIGN, sgntmp.getData());
blockData.setBlock(sign, false);
}
}
@Override
public void execute(final ScriptEntry scriptEntry) {
// Get objects
String direction = scriptEntry.hasObject("direction") ? ((Element) scriptEntry.getObject("direction")).asString() : null;
Element typeElement = scriptEntry.getElement("type");
dList text = (dList) scriptEntry.getObject("text");
dLocation location = (dLocation) scriptEntry.getObject("location");
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), typeElement.debug()
+ location.debug()
+ text.debug());
}
Type type = Type.valueOf(typeElement.asString().toUpperCase());
Block sign = location.getBlock();
if (type != Type.AUTOMATIC
|| !MaterialCompat.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);
}
else {
// TODO: 1.14 - allow new sign types?
sign.setType(MaterialCompat.SIGN, false);
if (direction != null) {
Utilities.setSignRotation(dLocation.getBlockStateFor(sign), direction);
}
}
}
else if (!MaterialCompat.isAnySign(sign.getType())) {
if (sign.getRelative(BlockFace.DOWN).getType().isSolid()) {
// TODO: 1.14 - allow new sign types?
sign.setType(MaterialCompat.SIGN, false);
}
else {
BlockFace bf = Utilities.chooseSignRotation(sign);
setWallSign(sign, bf);
}
}
BlockState signState = dLocation.getBlockStateFor(sign);
Utilities.setSignLines((Sign) signState, text.toArray(4));
}
}