/
CompassCommand.java
107 lines (97 loc) · 3.85 KB
/
CompassCommand.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
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public class CompassCommand extends AbstractCommand {
public CompassCommand() {
setName("compass");
setSyntax("compass [<location>/reset]");
setRequiredArguments(1, 1);
isProcedural = false;
}
// <--[command]
// @Name Compass
// @Syntax compass [<location>/reset]
// @Required 1
// @Maximum 1
// @Short Redirects the player's compass to target the given location.
// @Group player
//
// @Description
// Redirects the compass of the player, who is attached to the script queue.
//
// This is not the compass item, but the command is controlling the pointer the item should direct at.
// This means that all item compasses will point the same direction but differently for each player.
//
// The y-axis is not used but its fine to be included in the location argument.
//
// Reset argument will turn the direction to default (spawn or bed)
//
// @Tags
// <PlayerTag.compass_target>
//
// @Usage
// Use to reset the compass direction to its default.
// - compass reset
//
// @Usage
// Use to point with a compass to the player's current location.
// - compass <player.location>
//
// @Usage
// Use to point with a compass to the world's spawn location.
// - compass <player.world.spawn_location>
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (Notable note : NoteManager.notesByType.get(LocationTag.class)) {
addOne.accept(NoteManager.getSavedId(note));
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("reset")
&& arg.matches("reset")) {
scriptEntry.addObject("reset", new ElementTag(true));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("location") && !scriptEntry.hasObject("reset")) {
throw new InvalidArgumentsException("Missing location argument!");
}
scriptEntry.defaultObject("reset", new ElementTag(false));
}
@Override
public void execute(ScriptEntry scriptEntry) {
LocationTag location = scriptEntry.getObjectTag("location");
ElementTag reset = scriptEntry.getElement("reset");
Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), location, reset);
}
if (reset.asBoolean()) {
Location bed = player.getBedSpawnLocation();
player.setCompassTarget(bed != null ? bed : player.getWorld().getSpawnLocation());
}
else {
player.setCompassTarget(location);
}
}
}