-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
LightCommand.java
135 lines (117 loc) · 5.36 KB
/
LightCommand.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
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.objects.notable.NotableManager;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.BlockLight;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import java.util.function.Consumer;
public class LightCommand extends AbstractCommand {
public LightCommand() {
setName("light");
setSyntax("light [<location>] [<#>/reset] (duration:<duration>)");
setRequiredArguments(2, 3);
isProcedural = false;
}
// <--[command]
// @Name Light
// @Syntax light [<location>] [<#>/reset] (duration:<duration>)
// @Required 2
// @Maximum 3
// @Short Creates a light source at the location with a specified brightness.
// @Group world
//
// @Description
// This command can create and reset a light source at a specified location, regardless of the type
// of block. It will be shown to all players near the location until it is reset.
// The brightness must be between 0 and 15, inclusive.
// Optionally, specify the amount of time the light should exist before being removed.
// WARNING: May cause lag spikes, use carefully.
//
// Note that lights do not persist across server restarts, but will still be visible in the world
// after a restart until there is a block change near the location (to reset the light).
//
// @Tags
// <LocationTag.light>
// <LocationTag.light.blocks>
//
// @Usage
// Use to create a bright light at a noted location.
// - light MyFancyLightOfWool 15
//
// @Usage
// Use to reset the brightness of the location to its original state.
// - light MyFancyLightOfWool reset
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (Notable note : NotableManager.notesByType.get(LocationTag.class)) {
addOne.accept(NotableManager.getSavedId(note));
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("light")
&& arg.matchesInteger()) {
scriptEntry.addObject("light", arg.asElement());
}
else if (!scriptEntry.hasObject("reset")
&& arg.matches("reset")) {
scriptEntry.addObject("reset", new ElementTag(true));
}
else if (!scriptEntry.hasObject("duration")
&& arg.matchesPrefix("d", "duration")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
}
if (!scriptEntry.hasObject("location") ||
(!scriptEntry.hasObject("light") && !scriptEntry.hasObject("reset"))) {
throw new InvalidArgumentsException("Must specify a valid location and light level.");
}
scriptEntry.defaultObject("reset", new ElementTag(false));
}
@Override
public void execute(ScriptEntry scriptEntry) {
LocationTag location = scriptEntry.getObjectTag("location");
ElementTag light = scriptEntry.getElement("light");
ElementTag reset = scriptEntry.getElement("reset");
DurationTag duration = scriptEntry.getObjectTag("duration");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), location.debug() + reset.debug()
+ (light != null ? light.debug() : "") + (duration != null ? duration.debug() : ""));
}
if (!Utilities.isLocationYSafe(location)) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid light location!");
return;
}
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
location.clone().add(x * 16, 0, z * 16).getChunk().load();
}
}
if (!reset.asBoolean()) {
int brightness = light.asInt();
if (brightness < 0 || brightness > 15) {
Debug.echoError("Light brightness must be between 0 and 15, inclusive!");
return;
}
NMSHandler.getInstance().createBlockLight(location, brightness, duration == null ? 0 : duration.getTicks());
}
else {
BlockLight.removeLight(location);
}
}
}