Implement decent command handling

This commit is contained in:
dfsek 2020-10-01 02:16:54 -07:00
parent 215b30e611
commit f7f98b6dcc
20 changed files with 363 additions and 83 deletions

View File

@ -33,4 +33,9 @@ public class BiomeCommand extends PlayerCommand {
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
}

View File

@ -10,16 +10,19 @@ public abstract class Command {
public abstract String getName();
public abstract List<Command> getSubCommands();
public abstract boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command, @NotNull String label, @NotNull String[] args);
public abstract int arguments();
public final boolean execute(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command, @NotNull String label, @NotNull String[] args) {
if(args.length > 0) {
for(com.dfsek.terra.command.Command c : getSubCommands()) {
if(c.getName().equals(args[0])) return c.execute(sender, command, label, Arrays.stream(args, 1, args.length).toArray(String[]::new));
}
if(args.length != arguments()) {
sender.sendMessage("Invalid command.");
return true;
} else {
onCommand(sender, command, label, args);
}
return true;
return onCommand(sender, command, label, args);
}
return onCommand(sender, command, label, new String[] {});
}
}

View File

@ -14,6 +14,7 @@ public class OreCommand extends PlayerCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
Block bl = sender.getTargetBlockExact(25);
if(args.length > 0) {
OreConfig ore = OreConfig.fromID(args[0]);
if(ore == null) {
sender.sendMessage("Unable to find Ore");
@ -24,6 +25,10 @@ public class OreCommand extends PlayerCommand {
return true;
}
ore.doVein(bl.getLocation(), new Random());
} else {
sender.sendMessage("---------------Terra/ore---------------");
sender.sendMessage("Generates a vein of ore at the block you are looking at.");
}
return true;
}
@ -32,6 +37,11 @@ public class OreCommand extends PlayerCommand {
return Collections.emptyList();
}
@Override
public int arguments() {
return 1;
}
@Override
public String getName() {
return "ore";

View File

@ -25,4 +25,9 @@ public class ReloadCommand extends Command {
sender.sendMessage("Reloaded Terra config.");
return true;
}
@Override
public int arguments() {
return 0;
}
}

View File

@ -20,6 +20,11 @@ public class SaveDataCommand extends Command {
return true;
}
@Override
public int arguments() {
return 0;
}
@Override
public List<Command> getSubCommands() {
return Collections.emptyList();

View File

@ -1,5 +1,6 @@
package com.dfsek.terra.command;
import com.dfsek.terra.command.image.ImageCommand;
import com.dfsek.terra.command.profile.ProfileCommand;
import com.dfsek.terra.command.structure.StructureCommand;
import com.dfsek.terra.config.genconfig.BiomeConfig;
@ -22,7 +23,8 @@ public class TerraCommand implements CommandExecutor, TabExecutor {
new OreCommand(),
new ProfileCommand(),
new SaveDataCommand(),
new StructureCommand());
new StructureCommand(),
new ImageCommand());
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {

View File

@ -6,26 +6,16 @@ import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public abstract class WorldCommand extends PlayerCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if(sender.getWorld().getGenerator() instanceof TerraChunkGenerator) {
return onCommand(sender, command, label, args, sender.getWorld());
} else {
sender.sendMessage("World is not a Terra world!");
}
return true;
}
public abstract boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world);
@Override
public String getName() {
return null;
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return null;
}
}

View File

@ -2,54 +2,37 @@ package com.dfsek.terra.command.image;
import com.dfsek.terra.Terra;
import com.dfsek.terra.command.PlayerCommand;
import com.dfsek.terra.command.WorldCommand;
import com.dfsek.terra.command.image.gui.GUICommand;
import com.dfsek.terra.config.WorldConfig;
import com.dfsek.terra.image.WorldImageGenerator;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ImageCommand extends PlayerCommand {
public class ImageCommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if("render".equals(args[0])) {
if(args.length != 4) return false;
try {
WorldImageGenerator g = new WorldImageGenerator(sender.getWorld(), Integer.parseInt(args[2]), Integer.parseInt(args[3]));
g.drawWorld(sender.getLocation().getBlockX(), sender.getLocation().getBlockZ());
File file = new File(Terra.getInstance().getDataFolder() + File.separator + "export" + File.separator + "map" + File.separator + "map_" + System.currentTimeMillis() + ".png");
file.mkdirs();
file.createNewFile();
g.save(file);
sender.sendMessage("Saved image to " + file.getPath());
return true;
} catch(Exception e) {
e.printStackTrace();
return false;
}
} else if("gui".equals(args[0])) {
try {
if("raw".equals(args[1]))
WorldConfig.fromWorld(sender.getWorld()).imageLoader.debug(false, sender.getWorld());
else if("step".equals(args[1]))
WorldConfig.fromWorld(sender.getWorld()).imageLoader.debug(true, sender.getWorld());
else {
return true;
}
return true;
} catch(NullPointerException e) {
return true;
}
}
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World w) {
sender.sendMessage("---------------Terra/image---------------");
sender.sendMessage("render - Render an image with a given width and height, that can later be imported as a world.");
sender.sendMessage("gui - Open debug GUI (Must be enabled in config)");
return true;
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
return Arrays.asList(new RenderCommand(), new GUICommand());
}
@Override
public int arguments() {
return 1;
}
@Override

View File

@ -0,0 +1,48 @@
package com.dfsek.terra.command.image;
import com.dfsek.terra.Terra;
import com.dfsek.terra.command.WorldCommand;
import com.dfsek.terra.image.WorldImageGenerator;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.Collections;
import java.util.List;
public class RenderCommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) {
try {
WorldImageGenerator g = new WorldImageGenerator(world, Integer.parseInt(args[0]), Integer.parseInt(args[1]));
g.drawWorld(sender.getLocation().getBlockX(), sender.getLocation().getBlockZ());
File file = new File(Terra.getInstance().getDataFolder() + File.separator + "export" + File.separator + "map" + File.separator + "map_" + System.currentTimeMillis() + ".png");
file.mkdirs();
file.createNewFile();
g.save(file);
sender.sendMessage("Saved image to " + file.getPath());
return true;
} catch(Exception e) {
e.printStackTrace();
sender.sendMessage("An error occurred while generating the image!");
return true;
}
}
@Override
public String getName() {
return "render";
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 2;
}
}

View File

@ -0,0 +1,36 @@
package com.dfsek.terra.command.image.gui;
import com.dfsek.terra.command.WorldCommand;
import com.dfsek.terra.config.WorldConfig;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
public class GUICommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) {
sender.sendMessage("-------------Terra/image/gui-------------");
sender.sendMessage("raw - Open GUI with raw Biome data");
sender.sendMessage("step - Re-render data to show borders more clearly");
return true;
}
@Override
public String getName() {
return "gui";
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Arrays.asList(new StepGUICommand(), new RawGUICommand());
}
@Override
public int arguments() {
return 1;
}
}

View File

@ -0,0 +1,34 @@
package com.dfsek.terra.command.image.gui;
import com.dfsek.terra.command.WorldCommand;
import com.dfsek.terra.config.WorldConfig;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class RawGUICommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) {
WorldConfig.fromWorld(world).imageLoader.debug(false, sender.getWorld());
return true;
}
@Override
public String getName() {
return "raw";
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
}

View File

@ -0,0 +1,34 @@
package com.dfsek.terra.command.image.gui;
import com.dfsek.terra.command.WorldCommand;
import com.dfsek.terra.config.WorldConfig;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class StepGUICommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) {
WorldConfig.fromWorld(world).imageLoader.debug(true, sender.getWorld());
return true;
}
@Override
public String getName() {
return "step";
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
}

View File

@ -1,45 +1,37 @@
package com.dfsek.terra.command.profile;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.command.WorldCommand;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.profiler.WorldProfiler;
import java.util.Arrays;
import java.util.List;
public class ProfileCommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World w) {
WorldProfiler profile = TerraProfiler.fromWorld(w);
if(args.length > 1 && "query".equals(args[1])) {
sender.sendMessage(profile.getResultsFormatted());
return true;
} else if(args.length > 1 && "reset".equals(args[1])) {
profile.reset();
sender.sendMessage("Profiler has been reset.");
return true;
} else if(args.length > 1 && "start".equals(args[1])) {
profile.setProfiling(true);
sender.sendMessage("Profiler has started.");
return true;
} else if(args.length > 1 && "stop".equals(args[1])) {
profile.setProfiling(false);
sender.sendMessage("Profiler has stopped.");
return true;
}
sender.sendMessage("---------------Terra/profile---------------");
sender.sendMessage("start - Starts the profiler");
sender.sendMessage("stop - Stops the profiler");
sender.sendMessage("query - Fetches profiler data");
sender.sendMessage("reset - Resets profiler data");
return true;
}
@Override
public String getName() {
return null;
return "profile";
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return null;
return Arrays.asList(new QueryCommand(), new ResetCommand(), new StartCommand(), new StopCommand());
}
@Override
public int arguments() {
return 1;
}
}

View File

@ -2,6 +2,8 @@ package com.dfsek.terra.command.profile;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.command.PlayerCommand;
import com.dfsek.terra.command.WorldCommand;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -10,10 +12,10 @@ import org.polydev.gaea.profiler.WorldProfiler;
import java.util.Collections;
import java.util.List;
public class QueryCommand extends PlayerCommand {
public class QueryCommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
WorldProfiler profile = TerraProfiler.fromWorld(sender.getWorld());
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World w) {
WorldProfiler profile = TerraProfiler.fromWorld(w);
sender.sendMessage(profile.getResultsFormatted());
return true;
}
@ -27,4 +29,9 @@ public class QueryCommand extends PlayerCommand {
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
}

View File

@ -0,0 +1,37 @@
package com.dfsek.terra.command.profile;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.command.WorldCommand;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.profiler.WorldProfiler;
import java.util.Collections;
import java.util.List;
public class ResetCommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) {
WorldProfiler profile = TerraProfiler.fromWorld(world);
profile.reset();
sender.sendMessage("Profiler has been reset.");
return true;
}
@Override
public String getName() {
return "reset";
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
}

View File

@ -0,0 +1,37 @@
package com.dfsek.terra.command.profile;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.command.WorldCommand;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.profiler.WorldProfiler;
import java.util.Collections;
import java.util.List;
public class StartCommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) {
WorldProfiler profile = TerraProfiler.fromWorld(world);
profile.setProfiling(true);
sender.sendMessage("Profiler has started.");
return true;
}
@Override
public String getName() {
return "start";
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
}

View File

@ -0,0 +1,37 @@
package com.dfsek.terra.command.profile;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.command.WorldCommand;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.profiler.WorldProfiler;
import java.util.Collections;
import java.util.List;
public class StopCommand extends WorldCommand {
@Override
public boolean onCommand(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) {
WorldProfiler profile = TerraProfiler.fromWorld(world);
profile.setProfiling(false);
sender.sendMessage("Profiler has stopped.");
return true;
}
@Override
public String getName() {
return "stop";
}
@Override
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
}

View File

@ -72,4 +72,9 @@ public class ExportCommand extends PlayerCommand {
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 1;
}
}

View File

@ -36,4 +36,9 @@ public class LoadCommand extends PlayerCommand {
public List<com.dfsek.terra.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 3;
}
}

View File

@ -22,6 +22,11 @@ public class StructureCommand extends PlayerCommand {
return Arrays.asList(new ExportCommand(), new LoadCommand());
}
@Override
public int arguments() {
return 1;
}
@Override
public String getName() {
return "structure";