Skip to content

Commit

Permalink
allow createworld to async the copy_from arg
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 11, 2020
1 parent c223c2f commit 29bc19f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
Expand Up @@ -72,20 +72,17 @@ else if (!scriptEntry.hasObject("duration")
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
// Entity arg
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}

if (!scriptEntry.hasObject("entities")) {
scriptEntry.defaultObject("entities",
Utilities.entryHasNPC(scriptEntry) && Utilities.getEntryNPC(scriptEntry).isSpawned() ? Arrays.asList(Utilities.getEntryNPC(scriptEntry).getDenizenEntity()) : null,
Utilities.entryHasPlayer(scriptEntry) && Utilities.getEntryPlayer(scriptEntry).isOnline() ? Arrays.asList(Utilities.getEntryPlayer(scriptEntry).getDenizenEntity()) : null);
}

if (!scriptEntry.hasObject("location") || !scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify a location and entity!");
}
Expand All @@ -94,18 +91,13 @@ else if (!scriptEntry.hasObject("entities")
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) {

final LocationTag loc = scriptEntry.getObjectTag("location");
final List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
final DurationTag duration = scriptEntry.getObjectTag("duration");

if (scriptEntry.dbCallShouldDebug()) {

Debug.report(scriptEntry, getName(), loc.debug() +
ArgumentHelper.debugObj("entities", entities.toString()));

}

for (EntityTag entity : entities) {
if (entity.isSpawned()) {
NMSHandler.getEntityHelper().faceLocation(entity.getBukkitEntity(), loc);
Expand All @@ -114,7 +106,6 @@ public void execute(ScriptEntry scriptEntry) {
if (duration != null && duration.getTicks() > 2) {
BukkitRunnable task = new BukkitRunnable() {
long bounces = 0;

public void run() {
bounces += 2;
if (bounces > duration.getTicks()) {
Expand Down
@@ -1,20 +1,24 @@
package com.denizenscript.denizen.scripts.commands.world;

import com.denizenscript.denizen.utilities.DenizenAPI;
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.function.Supplier;

public class CreateWorldCommand extends AbstractCommand {
public class CreateWorldCommand extends AbstractCommand implements Holdable {

public CreateWorldCommand() {
setName("createworld");
Expand All @@ -40,6 +44,8 @@ public CreateWorldCommand() {
// Optionally specify an existing world to copy files from.
// Optionally specify additional generator settings as JSON input.
//
// The 'copy_from' argument is ~waitable. Refer to <@link language ~waitable>.
//
// @Tags
// <server.world_types>
// <server.worlds>
Expand All @@ -55,6 +61,10 @@ public CreateWorldCommand() {
// @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
Expand Down Expand Up @@ -121,17 +131,30 @@ public void execute(ScriptEntry scriptEntry) {
worldType.debug() +
(seed != null ? seed.debug() : ""));
}
if (copy_from != null) {
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;
return false;
}
File newFolder = new File(worldName.asString());
File folder = new File(copy_from.asString().replace("w@", ""));
if (!Utilities.canReadFile(folder)) {
Debug.echoError("Cannot copy from that folder path.");
return false;
}
if (!Utilities.canWriteToFile(newFolder)) {
Debug.echoError("Cannot copy to that new folder path.");
return false;
}
if (!folder.exists() || !folder.isDirectory()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid copy from world folder - does not exist!");
return;
return false;
}
CoreUtilities.copyDirectory(folder, newFolder);
File file = new File(worldName.asString() + "/uid.dat");
Expand All @@ -145,25 +168,41 @@ public void execute(ScriptEntry scriptEntry) {
}
catch (Exception ex) {
Debug.echoError(ex);
return;
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.echoDebug(scriptEntry, "World is null, something went wrong in creation!");
}
scriptEntry.setFinished(true);
};
if (scriptEntry.shouldWaitFor() && copy_from != null) {
Bukkit.getScheduler().runTaskAsynchronously(DenizenAPI.getCurrentInstance(), () -> {
if (!copyRunnable.get()) {
scriptEntry.setFinished(true);
return;
}
Bukkit.getScheduler().runTask(DenizenAPI.getCurrentInstance(), 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.echoDebug(scriptEntry, "World is null, something went wrong in creation!");
else {
createRunnable.run();
}
}
}
Expand Up @@ -596,8 +596,7 @@ public BoundingBox getBoundingBox(Entity entity) {
public void setBoundingBox(Entity entity, BoundingBox boundingBox) {
Vector low = boundingBox.getLow();
Vector high = boundingBox.getHigh();
((CraftEntity) entity).getHandle().a(new AxisAlignedBB(low.getX(), low.getY(), low.getZ(),
high.getX(), high.getY(), high.getZ()));
((CraftEntity) entity).getHandle().a(new AxisAlignedBB(low.getX(), low.getY(), low.getZ(), high.getX(), high.getY(), high.getZ()));
}

@Override
Expand Down

0 comments on commit 29bc19f

Please sign in to comment.