-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
SignCommand.java
81 lines (60 loc) · 2.62 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
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
/**
* Creates a sign with a certain text at a location.
*
* @author David Cernat
*/
public class SignCommand extends AbstractCommand {
private enum Type { SIGN_POST, WALL_SIGN }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.values()))
// add Type
scriptEntry.addObject("type", Type.valueOf(arg.getValue().toUpperCase()));
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class))
// Location arg
scriptEntry.addObject("location", arg.asType(dLocation.class).setPrefix("location"));
else if (!scriptEntry.hasObject("text")
&& arg.matchesArgumentType(dList.class))
// add text
scriptEntry.addObject("text", arg.asType(dList.class));
}
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException("Must specify a Sign location!");
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
Type type = (Type) scriptEntry.getObject("type");
dList text = (dList) scriptEntry.getObject("text");
dLocation location = (dLocation) scriptEntry.getObject("location");
Block sign = location.getBlock();
if (type.equals(Type.WALL_SIGN))
sign.setType(Material.WALL_SIGN);
else
sign.setType(Material.SIGN_POST);
BlockState signState = sign.getState();
int n = 0;
for (String line : text) {
((Sign) signState).setLine(n, line);
n++;
}
signState.update();
}
}