/
CreateWorldCommand.java
244 lines (236 loc) · 10.5 KB
/
CreateWorldCommand.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class CreateWorldCommand extends AbstractCommand implements Holdable {
public CreateWorldCommand() {
setName("createworld");
setSyntax("createworld [<name>] (generator:<id>) (worldtype:<type>) (environment:<environment>) (copy_from:<world>) (seed:<seed>) (settings:<json>)");
setRequiredArguments(1, 7);
isProcedural = false;
}
// <--[command]
// @Name CreateWorld
// @Syntax createworld [<name>] (generator:<id>) (worldtype:<type>) (environment:<environment>) (copy_from:<world>) (seed:<seed>) (settings:<json>)
// @Required 1
// @Maximum 7
// @Short Creates a new world, or loads an existing world.
// @Group world
//
// @Description
// This command creates a new minecraft world with the specified name, or loads an existing world by that name.
//
// Optionally specify a plugin-based world generator by it's generator ID.
// If you want an empty void world, you can use "generator:denizen:void".
//
// Optionally specify additional generator settings as JSON input.
//
// Optionally specify a world type which can be specified with 'worldtype:' (defaults to NORMAL).
// For all world types, see: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/WorldType.html>
//
// Optionally specify an environment (defaults to NORMAL, can also be NETHER or THE_END).
//
// Optionally specify an existing world to copy files from.
// The 'copy_from' argument is ~waitable. Refer to <@link language ~waitable>.
//
// It's often ideal to put this command inside <@link event server prestart>.
//
// @Tags
// <server.world_types>
// <server.worlds>
//
// @Usage
// Use to create a normal world with name 'survival'
// - createworld survival
//
// @Usage
// Use to create a flat world with the name 'superflat'
// - createworld superflat worldtype:FLAT
//
// @Usage
// Use to create an end world with the name 'space'
// - createworld space environment:THE_END
//
// @Usage
// Use to create a new world named 'dungeon3' as a copy of an existing world named 'dungeon_template'.
// - ~createworld dungeon3 copy_from:dungeon_template
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
if (arg.startsWith("environment:")) {
for (World.Environment env : World.Environment.values()) {
addOne.accept("environment:" + env);
}
}
if (arg.startsWith("worldtype:")) {
for (WorldType type : WorldType.values()) {
addOne.accept("worldtype:" + type);
}
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("generator")
&& arg.matchesPrefix("generator", "g")) {
scriptEntry.addObject("generator", arg.asElement());
}
else if (!scriptEntry.hasObject("worldtype")
&& arg.matchesPrefix("worldtype")
&& arg.matchesEnum(WorldType.values())) {
scriptEntry.addObject("worldtype", arg.asElement());
}
else if (!scriptEntry.hasObject("environment")
&& arg.matchesPrefix("environment")
&& arg.matchesEnum(World.Environment.values())) {
scriptEntry.addObject("environment", arg.asElement());
}
else if (!scriptEntry.hasObject("copy_from")
&& arg.matchesPrefix("copy_from")) {
scriptEntry.addObject("copy_from", arg.asElement());
}
else if (!scriptEntry.hasObject("settings")
&& arg.matchesPrefix("settings")) {
scriptEntry.addObject("settings", arg.asElement());
}
else if (!scriptEntry.hasObject("seed")
&& arg.matchesPrefix("seed", "s")
&& arg.matchesInteger()) {
scriptEntry.addObject("seed", arg.asElement());
}
else if (!scriptEntry.hasObject("world_name")) {
scriptEntry.addObject("world_name", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("world_name")) {
throw new InvalidArgumentsException("Must specify a world name.");
}
if (!scriptEntry.hasObject("worldtype")) {
scriptEntry.addObject("worldtype", new ElementTag("NORMAL"));
}
scriptEntry.defaultObject("environment", new ElementTag("NORMAL"));
}
public static HashSet<String> excludedExtensionsForCopyFrom = new HashSet<>(Collections.singleton("lock"));
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag worldName = scriptEntry.getElement("world_name");
ElementTag generator = scriptEntry.getElement("generator");
ElementTag worldType = scriptEntry.getElement("worldtype");
ElementTag environment = scriptEntry.getElement("environment");
ElementTag copy_from = scriptEntry.getElement("copy_from");
ElementTag settings = scriptEntry.getElement("settings");
ElementTag seed = scriptEntry.getElement("seed");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), worldName.debug() +
(generator != null ? generator.debug() : "") +
environment.debug() +
(copy_from != null ? copy_from.debug() : "") +
(settings != null ? settings.debug() : "") +
worldType.debug() +
(seed != null ? seed.debug() : ""));
}
if (Bukkit.getWorld(worldName.asString()) != null) {
Debug.echoDebug(scriptEntry, "CreateWorld doing nothing, world by that name already loaded.");
scriptEntry.setFinished(true);
return;
}
Supplier<Boolean> copyRunnable = () -> {
try {
if (copy_from.asString().contains("..")) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid copy from world name!");
return false;
}
File folder = new File(copy_from.asString().replace("w@", ""));
final File newFolder = new File(worldName.asString());
if (!Utilities.canReadFile(folder)) {
Debug.echoError("Cannot copy from that folder path due to security settings in Denizen/config.yml.");
return false;
}
if (!Utilities.canWriteToFile(newFolder)) {
Debug.echoError("Cannot copy to that new folder path due to security settings in Denizen/config.yml.");
return false;
}
if (!folder.exists() || !folder.isDirectory()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid copy from world folder - does not exist!");
return false;
}
if (newFolder.exists()) {
Debug.echoError("Cannot copy to new world - that folder already exists.");
return false;
}
CoreUtilities.copyDirectory(folder, newFolder, excludedExtensionsForCopyFrom);
Debug.echoDebug(scriptEntry, "Copied " + folder.getName() + " to " + newFolder.getName());
File file = new File(worldName.asString() + "/uid.dat");
if (file.exists()) {
file.delete();
}
File file2 = new File(worldName.asString() + "/session.lock");
if (file2.exists()) {
file2.delete();
}
}
catch (Throwable ex) {
Debug.echoError(ex);
return false;
}
return true;
};
Runnable createRunnable = () -> {
World world;
WorldCreator worldCreator = WorldCreator.name(worldName.asString())
.environment(World.Environment.valueOf(environment.asString().toUpperCase()))
.type(WorldType.valueOf(worldType.asString().toUpperCase()));
if (generator != null) {
worldCreator.generator(generator.asString());
}
if (seed != null) {
worldCreator.seed(seed.asLong());
}
if (settings != null) {
worldCreator.generatorSettings(settings.asString());
}
world = Bukkit.getServer().createWorld(worldCreator);
if (world == null) {
Debug.echoError("World is null, something went wrong in creation!");
}
else {
Debug.echoDebug(scriptEntry, "Created new world " + world.getName());
}
scriptEntry.setFinished(true);
};
if (scriptEntry.shouldWaitFor() && copy_from != null) {
Bukkit.getScheduler().runTaskAsynchronously(Denizen.getInstance(), () -> {
if (!copyRunnable.get()) {
scriptEntry.setFinished(true);
return;
}
Bukkit.getScheduler().runTask(Denizen.getInstance(), createRunnable);
});
}
else {
if (copy_from != null && !copyRunnable.get()) {
return;
}
createRunnable.run();
}
}
}