From 29bc19fc4b78a14d1cf48ba2dcfa4c4b2f67274f Mon Sep 17 00:00:00 2001 From: Alex 'mcmonkey' Goodwin Date: Tue, 11 Aug 2020 11:58:15 -0700 Subject: [PATCH] allow createworld to async the copy_from arg --- .../scripts/commands/entity/LookCommand.java | 9 --- .../commands/world/CreateWorldCommand.java | 81 ++++++++++++++----- .../nms/v1_16/helpers/EntityHelperImpl.java | 3 +- 3 files changed, 61 insertions(+), 32 deletions(-) diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/entity/LookCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/entity/LookCommand.java index b5657a244b..7dcfd34835 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/entity/LookCommand.java +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/entity/LookCommand.java @@ -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!"); } @@ -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 entities = (List) 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); @@ -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()) { diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/world/CreateWorldCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/world/CreateWorldCommand.java index 9ddcdf6372..a77153f849 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/world/CreateWorldCommand.java +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/world/CreateWorldCommand.java @@ -1,11 +1,14 @@ 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; @@ -13,8 +16,9 @@ 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"); @@ -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 // // @@ -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 @@ -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 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"); @@ -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(); } } } diff --git a/v1_16/src/main/java/com/denizenscript/denizen/nms/v1_16/helpers/EntityHelperImpl.java b/v1_16/src/main/java/com/denizenscript/denizen/nms/v1_16/helpers/EntityHelperImpl.java index ce87503ac6..1a636d3d4a 100644 --- a/v1_16/src/main/java/com/denizenscript/denizen/nms/v1_16/helpers/EntityHelperImpl.java +++ b/v1_16/src/main/java/com/denizenscript/denizen/nms/v1_16/helpers/EntityHelperImpl.java @@ -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