Merge pull request #518 from CocoTheOwner/irisCreateRework

Rework /iris create & patch /iris pregen start
This commit is contained in:
Dan 2021-08-10 07:21:38 -04:00 committed by GitHub
commit 1231b13dfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 253 additions and 238 deletions

View File

@ -11,47 +11,32 @@ import com.volmit.iris.util.plugin.VolmitSender;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import java.util.Objects;
public class CommandIrisPregenStart extends MortarCommand { public class CommandIrisPregenStart extends MortarCommand {
private static final KList<String> argus = new KList<>("radius=", "x=", "z="); private static final KList<String> argus = new KList<>("radius=", "x=", "z=");
public CommandIrisPregenStart() { public CommandIrisPregenStart() {
super("start", "s", "create", "c", "new", "+"); super("start", "create", "c", "new", "+");
requiresPermission(Iris.perm); requiresPermission(Iris.perm);
setCategory("Pregen"); setCategory("Pregen");
setDescription(""" setDescription("Create a new pregeneration task.");
Create a new pregeneration task.
Command usage:
/iris pregen create [radius=<radius>] [x=<centerX>] [z=<centerZ>] [world=<world>] [-here]
Examples:
/iris pregen start 5k -here
/iris pregen start radius=5000 x=10r z=10r world=IrisWorld
/iris pregen start 10k world=WorldName
<radius>: Sets both width and height to a value. (Size is: 2 * radius by 2 * radius)
<x> & <z>: Give the center point of the pregen.
<world>: Specify a different world name for generation than the one you're currently in.
The console is not in any world, so this is required for console!
If you specify this, the `-here` is ignored.
-here: If added, the center location is set to your position (player only)
This overrides <x> and <z>.
For all numeric values (radius, centerX, etc.) you may use:
c => 16, r => 512, k => 1000
Example: entering '1000' is the same as '1k' (1 * 1000)
https://docs.volmit.com/iris/pregeneration""");
} }
@Override @Override
public void addTabOptions(VolmitSender sender, String[] args, KList<String> list) { public void addTabOptions(VolmitSender sender, String[] args, KList<String> list) {
if (args.length == 0){
return;
}
// Add arguments // Add arguments
argus.forEach(p -> { argus.forEach(p -> {
boolean hasArg = false; boolean hasArg = false;
for (String arg : args) { for (String arg : args) {
if (!arg.contains("=") || !p.contains("=")) { if (!arg.contains("=") || !p.contains("=") || arg.equals("=")) {
continue; continue;
} }
if (arg.split("=")[0].equals(p.split("=")[0])) { if (arg.split("=")[0].equals(p.split("=")[0])) {
@ -96,6 +81,11 @@ public class CommandIrisPregenStart extends MortarCommand {
@Override @Override
public boolean handle(VolmitSender sender, String[] args) { public boolean handle(VolmitSender sender, String[] args) {
if (args.length == 0){
sender.sendMessage(getHelp());
return true;
}
if (PregeneratorJob.getInstance() != null) { if (PregeneratorJob.getInstance() != null) {
sender.sendMessage("Pregeneration task already ongoing. You can stop it with /ir p stop."); sender.sendMessage("Pregeneration task already ongoing. You can stop it with /ir p stop.");
sender.sendMessage("Cannot create new pregen while one is already going. Cancelling..."); sender.sendMessage("Cannot create new pregen while one is already going. Cancelling...");
@ -123,7 +113,7 @@ public class CommandIrisPregenStart extends MortarCommand {
failed.add(a + " (invalid world)"); failed.add(a + " (invalid world)");
sender.sendMessage("Entered world is " + val + ", but that world does not exist."); sender.sendMessage("Entered world is " + val + ", but that world does not exist.");
sender.sendMessage("Cancelling the command."); sender.sendMessage("Cancelling the command.");
sender.sendMessage(getDescription()); sender.sendMessage(getHelp());
return true; return true;
} }
} else if (!isVal(val)) { } else if (!isVal(val)) {
@ -152,7 +142,7 @@ public class CommandIrisPregenStart extends MortarCommand {
// Checking if a radius was specified or forgotten // Checking if a radius was specified or forgotten
if (width == -1 || height == -1) { if (width == -1 || height == -1) {
sender.sendMessage("Radius not specified! Cancelling..."); sender.sendMessage("Radius not specified! Cancelling...");
sender.sendMessage(getDescription()); sender.sendMessage(getHelp());
return true; return true;
} }
@ -162,19 +152,13 @@ public class CommandIrisPregenStart extends MortarCommand {
world = sender.player().getWorld(); world = sender.player().getWorld();
} else { } else {
sender.sendMessage("Must specify world=<name> if sending from console! Cancelling..."); sender.sendMessage("Must specify world=<name> if sending from console! Cancelling...");
sender.sendMessage(getDescription()); sender.sendMessage(getHelp());
return true; return true;
} }
} else { } else if (sender.isPlayer() && !world.equals(sender.player().getWorld()) && here) {
if (sender.isPlayer()) {
if (!world.equals(sender.player().getWorld())) {
if (here) {
sender.sendMessage("Ignoring `-here` because `world=` is specified!"); sender.sendMessage("Ignoring `-here` because `world=` is specified!");
here = false; here = false;
} }
}
}
}
// Checking if -here is used // Checking if -here is used
if (here) { if (here) {
@ -213,7 +197,7 @@ public class CommandIrisPregenStart extends MortarCommand {
// Start pregen and append info to details // Start pregen and append info to details
if (pregenerate(world, width, height, x, z)) { if (pregenerate(world, width, height, x, z)) {
details.append("Successfully started pregen"); details.append("Successfully started pregen.");
} else { } else {
details.append("Failed to start pregen. Doublecheck your arguments!"); details.append("Failed to start pregen. Doublecheck your arguments!");
} }
@ -295,4 +279,29 @@ public class CommandIrisPregenStart extends MortarCommand {
} }
return true; return true;
} }
/**
* Get command help
* @return help string
*/
private String getHelp() {
return """
Create a new pregeneration task.
Command usage:
/iris pregen create [radius=<radius>] [x=<centerX>] [z=<centerZ>] [world=<world>] [-here]
Examples:
/iris pregen start 5k -here
/iris pregen start radius=5000 x=10r z=10r world=IrisWorld
/iris pregen start 10k world=WorldName
<radius>: Sets both width and height to a value
<x> & <z>: Give the center point of the pregen
-here: Sets the center x and z to the current location
<world>: Specify a world name for generation
In radius, x and z multiply the value by c => 16, r => 512, k => 1000
Example: entering '1000' is the same as '1k' (1 * 1000)
Make sure to check https://docs.volmit.com/iris/pregeneration for guidance""";
}
} }

View File

@ -20,9 +20,7 @@ package com.volmit.iris.core.command.world;
import com.volmit.iris.Iris; import com.volmit.iris.Iris;
import com.volmit.iris.core.IrisSettings; import com.volmit.iris.core.IrisSettings;
import com.volmit.iris.core.link.MultiverseCoreLink;
import com.volmit.iris.core.nms.INMS; import com.volmit.iris.core.nms.INMS;
import com.volmit.iris.core.project.loader.IrisData;
import com.volmit.iris.core.tools.IrisWorldCreator; import com.volmit.iris.core.tools.IrisWorldCreator;
import com.volmit.iris.engine.object.dimensional.IrisDimension; import com.volmit.iris.engine.object.dimensional.IrisDimension;
import com.volmit.iris.engine.platform.PlatformChunkGenerator; import com.volmit.iris.engine.platform.PlatformChunkGenerator;
@ -38,7 +36,6 @@ import org.bukkit.WorldCreator;
import java.io.File; import java.io.File;
import java.util.Random; import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
public class CommandIrisCreate extends MortarCommand { public class CommandIrisCreate extends MortarCommand {
@ -51,85 +48,60 @@ public class CommandIrisCreate extends MortarCommand {
@Override @Override
public void addTabOptions(VolmitSender sender, String[] args, KList<String> list) { public void addTabOptions(VolmitSender sender, String[] args, KList<String> list) {
if (args.length == 0) {
list.add("[worldname]");
return;
}
if (args.length >= 1 || args[args.length - 1].equals("")) { //They are about to type a new argument boolean seed = false;
list.addAll(getBase(args)); boolean type = false;
return;
}
String[] split = args[args.length - 1].split("\\Q=\\E"); File packsFolder = new File("plugins/Iris/packs/");
if (split.length == 0) { //They haven't typed the = yet so just keep the options there packsFolder.mkdirs();
list.addAll(getBase(args));
return;
}
String pre = split[0].toLowerCase(); for (String arg : args) {
if (arg.equals("seed=")){
switch (pre) {
case "type" -> {
for (String s : Iris.proj.getListing(true).keySet()) {
list.add("type=" + s);
}
if (!list.contains("type=overworld")) {
list.contains("type=overworld");
}
}
case "seed" -> {
list.add("seed=1337");
list.add("seed=" + new Random().nextInt());
list.add("seed=random"); list.add("seed=random");
list.add("seed=1234");
} else if (arg.startsWith("seed=")){
seed = true;
} else if (arg.equals("type=")){
for (File dim : packsFolder.listFiles()){
if (dim.isDirectory()) {
list.add("type=" + dim.getName());
} }
case "pregen" -> {
list.add("500");
list.add("1000");
list.add("2000");
list.add("5k");
list.add("10k");
list.add("25k");
} }
type = true;
} else if (arg.startsWith("type=")){
type = true;
} }
} }
private KList<String> getBase(String[] args) { if (!seed){
KList<String> list = new KList<>(); list.add("seed=random");
boolean seed = true; list.add("seed=1234");
boolean type = true;
boolean pregen = true;
for (String s : args) {
if (s.toLowerCase().startsWith("seed=")) seed = false;
else if (s.toLowerCase().startsWith("type=")) type = false;
else if (s.toLowerCase().startsWith("pregen=")) pregen = false;
} }
if (seed) list.add("seed="); if (!type){
if (type) list.add("type="); for (File dim : packsFolder.listFiles()){
if (pregen) list.add("pregen="); if (dim.isDirectory()) {
return list; list.add("type=" + dim.getName());
}
}
}
} }
@Override @Override
public boolean handle(VolmitSender sender, String[] args) { public boolean handle(VolmitSender sender, String[] args) {
String worldName;
File folder;
String dimensionName;
IrisDimension dimension;
long seed;
if (args.length < 1) { if (args.length < 1) {
sender.sendMessage("/iris create <NAME> [type=overworld] [seed=1337] [pregen=5000]"); sender.sendMessage(getArgsUsage());
return true; return true;
} }
Random random = new Random();
String worldName = args[0];
String type = IrisSettings.get().getGenerator().getDefaultWorldType();
long seed = random.nextLong(); //Random seed when creating a world
AtomicInteger pregen = new AtomicInteger(0);
boolean multiverse = Iris.linkMultiverseCore.supported();
for (String i : args) { worldName = args[0];
type = i.startsWith("type=") ? i.split("\\Q=\\E")[1] : type;
seed = i.startsWith("seed=") ? (i.split("\\Q=\\E")[1].equalsIgnoreCase("random") ? random.nextLong() : Long.valueOf(i.split("\\Q=\\E")[1])) : seed;
pregen.set(i.startsWith("pregen=") ? getVal(i.split("\\Q=\\E")[1]) : pregen.get());
}
if (worldName.equalsIgnoreCase("iris")) { if (worldName.equalsIgnoreCase("iris")) {
sender.sendMessage("You cannot use the world name \"iris\" for creating worlds as Iris uses this directory for studio worlds."); sender.sendMessage("You cannot use the world name \"iris\" for creating worlds as Iris uses this directory for studio worlds.");
@ -137,92 +109,150 @@ public class CommandIrisCreate extends MortarCommand {
return true; return true;
} }
Iris.linkMultiverseCore.assignWorldType(worldName, type); folder = new File(worldName);
final AtomicReference<World> world = new AtomicReference<>();
IrisDimension dim;
File folder = new File(worldName);
Runnable onDone = () -> { if (folder.exists()) {
sender.sendMessage("That world folder already exists!");
return true;
}
sender.sendMessage(worldName + " Spawn Area generated."); dimensionName = IrisSettings.get().getGenerator().getDefaultWorldType();
seed = new Random().nextLong(); //Random seed when creating a world
for (String i : args) {
dimensionName = i.startsWith("type=") ? i.split("\\Q=\\E")[1] : dimensionName;
seed = i.startsWith("seed=") ? Long.parseLong(i.split("\\Q=\\E")[1]) : seed;
}
dimension = Iris.proj.installIntoWorld(sender, dimensionName, folder);
if (dimension == null) {
sender.sendMessage("Cannot find dimension '" + dimensionName + "'. Did you forget to /iris download " + dimensionName + "?");
return true;
}
if (dimension.getEnvironment() == null){
dimension.setEnvironment(World.Environment.NORMAL);
}
File iris = new File(folder, "iris");
iris.mkdirs();
onDone(sender, createWorld(sender, worldName, dimension, seed));
return true;
}
@Override
protected String getArgsUsage() {
return "<name> [type=<type>] [seed=<seed>]";
}
/**
* Ran when world is created
* @param sender The sender to send updates to
* @param world The created world
*/
private void onDone(VolmitSender sender, World world){
sender.sendMessage(world.getName() + " Spawn Area generated.");
sender.sendMessage("You must remember to either have multiverse installed or use the Bukkit method to load this world with the Iris Generator on startup."); sender.sendMessage("You must remember to either have multiverse installed or use the Bukkit method to load this world with the Iris Generator on startup.");
sender.sendMessage("Wiki: https://volmitsoftware.gitbook.io/iris/getting-started"); sender.sendMessage("Wiki: https://volmitsoftware.gitbook.io/iris/getting-started");
O<Boolean> b = new O<>();
b.set(true);
if (sender.isPlayer()) { if (sender.isPlayer()) {
try { try {
sender.player().teleport(world.get().getSpawnLocation()); sender.player().teleport(world.getSpawnLocation());
} catch (Throwable e) { } catch (Throwable e) {
Iris.reportError(e); Iris.reportError(e);
} }
} }
if (pregen.get() > 0) { O<Boolean> b = new O<>();
b.set(false); b.set(true);
int size = pregen.get();
size *= 2;
sender.sendMessage("Pregenerating " + worldName + " " + size + " x " + size);
sender.sendMessage("Expect server lag during this time. Use '/iris pregen stop' to cancel");
}
World ww = world.get();
if (ww == null) {
sender.sendMessage("World not created, can not finish");
return;
}
J.a(() -> J.a(() ->
{ {
while (!b.get()) { while (!b.get()) {
J.sleep(1000); J.sleep(1000);
} }
Bukkit.getScheduler().scheduleSyncDelayedTask(Iris.instance, () -> Bukkit.getScheduler().scheduleSyncDelayedTask(Iris.instance, () ->
{ {
ww.save(); world.save();
sender.sendMessage("All Done!"); sender.sendMessage("All Done!");
}); });
}); });
};
if (multiverse) {
dim = IrisData.loadAnyDimension(type);
if (dim == null) {
sender.sendMessage("Cant find dimension type: " + type + ". Did you forget to /ir download " + type + "?");
return true;
} }
if (dim.getEnvironment() == null) { /**
dim.setEnvironment(World.Environment.NORMAL); * Create a world with either Multiverse (preferred, if supported) or NMS
} * @param sender The sender to send updates to
* @param worldName The name of the world to create
if (Iris.linkMultiverseCore == null) { * @param dimension The dimension to create the world with
Iris.linkMultiverseCore = new MultiverseCoreLink(); * @param seed The seed to use to generate
} * @return The created world
*/
String command = "mv create " + worldName + " " + Iris.linkMultiverseCore.envName(dim.getEnvironment()); private World createWorld(VolmitSender sender, String worldName, IrisDimension dimension, long seed){
command += " -s " + seed; if (Iris.linkMultiverseCore.isSupported()) {
command += " -g Iris:" + dim.getLoadKey(); return createMultiverseWorld(sender, worldName, dimension, seed);
sender.sendMessage("Delegating " + command);
Bukkit.dispatchCommand(sender, command);
world.set(Bukkit.getWorld(worldName));
onDone.run();
} else { } else {
if (folder.exists()) { return createNMSWorld(sender, worldName, dimension, seed);
sender.sendMessage("That world folder already exists!"); }
return true;
} }
File iris = new File(folder, "iris"); /**
iris.mkdirs(); * Create a world with Multiverse
dim = Iris.proj.installIntoWorld(sender, type, folder); * @param sender The sender to send updates to
WorldCreator wc = new IrisWorldCreator().dimension(dim.getLoadKey()).name(worldName) * @param worldName The name of the world to create
.productionMode().seed(seed).create(); * @param dimension The dimension to create the world with
* @param seed The seed to use to generate
* @return The created world
*/
public World createMultiverseWorld(VolmitSender sender, String worldName, IrisDimension dimension, long seed){
if (!Iris.linkMultiverseCore.isSupported()){
sender.sendMessage("A world was attempted to be created with Multiverse but it is not supported!");
return null;
}
Iris.linkMultiverseCore.assignWorldType(worldName, dimension.getName());
StringBuilder command = new StringBuilder("mv create")
.append(worldName)
.append(" ")
.append(Iris.linkMultiverseCore.envName(dimension.getEnvironment()))
.append(" -s ")
.append(seed)
.append(" -g Iris:")
.append(dimension.getLoadKey());
sender.sendMessage("Delegating " + command);
Bukkit.dispatchCommand(sender, command.toString());
return Bukkit.getWorld(worldName);
}
/**
* Create a world using NMS
* @param sender The sender to send updates to
* @param worldName The name of the world to create
* @param dimension The dimension to create the world with
* @param seed The seed to use to generate
* @return The created world
*/
public World createNMSWorld(VolmitSender sender, String worldName, IrisDimension dimension, long seed){
WorldCreator wc = new IrisWorldCreator()
.dimension(dimension.getLoadKey())
.name(worldName)
.seed(seed)
.productionMode()
.create();
PlatformChunkGenerator gen = (PlatformChunkGenerator) wc.generator();
if (gen == null){
sender.sendMessage("Failed to create generator! Gen is null!");
return null;
}
AtomicReference<World> world = new AtomicReference<>();
J.s(() -> { J.s(() -> {
O<Boolean> done = new O<>(); O<Boolean> done = new O<>();
@ -233,50 +263,26 @@ public class CommandIrisCreate extends MortarCommand {
double last = 0; double last = 0;
int req = 800; int req = 800;
while (!done.get()) { while (!done.get()) {
boolean derp = false;
double v = (double) ((PlatformChunkGenerator) wc.generator()).getEngine().getGenerated() / (double) req; boolean shouldBeDone = false;
double v = (double) gen.getEngine().getGenerated() / req;
if (last > v || v > 1) { if (last > v || v > 1) {
derp = true; shouldBeDone = true;
v = last; v = last;
} else { } else {
last = v; last = v;
} }
sender.sendMessage("Generating " + Form.pc(v) + (derp ? " (Waiting on Server...)" : "")); sender.sendMessage("Generating " + Form.pc(v) + (shouldBeDone ? " (Waiting on Server...)" : ""));
J.sleep(3000); J.sleep(3000);
} }
}); });
World w = INMS.get().createWorld(wc); world.set(INMS.get().createWorld(wc));
world.set(w);
done.set(true); done.set(true);
}); });
}
return world.get();
return true;
}
private int getVal(String arg) {
if (arg.toLowerCase().endsWith("c") || arg.toLowerCase().endsWith("chunks")) {
return Integer.parseInt(arg.toLowerCase().replaceAll("\\Qc\\E", "").replaceAll("\\Qchunks\\E", "")) * 16;
}
if (arg.toLowerCase().endsWith("r") || arg.toLowerCase().endsWith("regions")) {
return Integer.parseInt(arg.toLowerCase().replaceAll("\\Qr\\E", "").replaceAll("\\Qregions\\E", "")) * 512;
}
if (arg.toLowerCase().endsWith("k")) {
return Integer.parseInt(arg.toLowerCase().replaceAll("\\Qk\\E", "")) * 1000;
}
return Integer.parseInt(arg.toLowerCase());
}
@Override
protected String getArgsUsage() {
return "<name> [type=overworld] [seed=1337] [pregen=5000]";
} }
} }

View File

@ -38,7 +38,7 @@ public class MultiverseCoreLink {
} }
public boolean addWorld(String worldName, IrisDimension dim, String seed) { public boolean addWorld(String worldName, IrisDimension dim, String seed) {
if (!supported()) { if (!isSupported()) {
return false; return false;
} }
@ -76,7 +76,7 @@ public class MultiverseCoreLink {
} }
public void removeFromConfig(World world) { public void removeFromConfig(World world) {
if (!supported()) { if (!isSupported()) {
return; return;
} }
@ -85,7 +85,7 @@ public class MultiverseCoreLink {
} }
public void removeFromConfig(String world) { public void removeFromConfig(String world) {
if (!supported()) { if (!isSupported()) {
return; return;
} }
@ -118,7 +118,7 @@ public class MultiverseCoreLink {
} }
} }
public boolean supported() { public boolean isSupported() {
return getMultiverse() != null; return getMultiverse() != null;
} }