implement Bukkit-specific commands

This commit is contained in:
dfsek
2021-03-10 16:08:25 -07:00
parent 1f16a82a8d
commit d6b5f60b18
14 changed files with 41 additions and 558 deletions

View File

@@ -112,7 +112,7 @@ warning:
error:
severe-config: "A severe configuration error has prevented Terra from properly generating terrain at coordinates: %1$s, %2$s. Please check your configuration for errors. Any config errors will have been reported above."
debug:
data-save: "Saved population data for world \"%s\""
data-save : "Saved population data."
use-paper:
- "You appear to be using Spigot/CraftBukkit."
- "While Terra &odoes&r work on Spigot, some functionality will be lost. (Terra is untested on CraftBukkit; no support will be given for CraftBukkit)."

View File

@@ -23,6 +23,8 @@ import com.dfsek.terra.api.util.logging.JavaLogger;
import com.dfsek.terra.api.util.logging.Logger;
import com.dfsek.terra.api.world.generation.TerraChunkGenerator;
import com.dfsek.terra.bukkit.command.BukkitCommandAdapter;
import com.dfsek.terra.bukkit.command.FixChunkCommand;
import com.dfsek.terra.bukkit.command.SaveDataCommand;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
import com.dfsek.terra.bukkit.handles.BukkitItemHandle;
import com.dfsek.terra.bukkit.handles.BukkitWorldHandle;
@@ -178,6 +180,8 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
try {
CommandUtil.registerAll(manager);
manager.register("save-data", SaveDataCommand.class);
manager.register("fix-chunk", FixChunkCommand.class);
} catch(MalformedCommandException e) { // This should never happen.
logger().severe("Errors occurred while registering commands.");
e.printStackTrace();

View File

@@ -1,113 +0,0 @@
package com.dfsek.terra.bukkit.command;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Represents a command or subcommands, can be nested via getSubCommands.
*/
public abstract class Command implements CommandExecutor, TabCompleter {
private final TerraPlugin main;
public Command(TerraPlugin main) {
this.main = main;
}
public Command(Command parent) {
main = parent.getMain();
}
public TerraPlugin getMain() {
return main;
}
/**
* Gets the name of the command/subcommands
*
* @return Name of command
*/
public abstract String getName();
/**
* Gets a list of subcommands
* @return List of subcommands
*/
public abstract List<Command> getSubCommands();
/**
* Executes the given command, returning its success.
* <br>
* If false is returned, then the "usage" plugin.yml entry for this command
* (if defined) will be sent to the player.
*
* @param sender Source of the command
* @param command CommandTemplate which was executed
* @param label Alias of the command which was used
* @param args Passed command arguments
* @return true if a valid command, otherwise false
*/
public abstract boolean execute(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command, @NotNull String label, @NotNull String[] args);
/**
* Gets the number of arguments
* @return Number of arguments
*/
public abstract int arguments();
/**
* Executes the given command, invoking subcommands if applicable and returning its success.
* <br>
* If false is returned, then the "usage" plugin.yml entry for this command
* (if defined) will be sent to the player.
*
* @param sender Source of the command
* @param command CommandTemplate which was executed
* @param label Alias of the command which was used
* @param args Passed command arguments
* @return true if a valid command, otherwise false
*/
@Override
public final boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command, @NotNull String label, @NotNull String[] args) {
if(this instanceof DebugCommand && ! main.isDebug()) {
main.getLanguage().send("command.debug-only", new BukkitCommandSender(sender));
return true;
}
if(args.length > 0) {
for(Command c : getSubCommands()) {
if(c.getName().equals(args[0])) {
return c.onCommand(sender, command, label, Arrays.stream(args, 1, args.length).toArray(String[]::new));
}
}
if(args.length != arguments()) {
main.getLanguage().send("command.invalid", new BukkitCommandSender(sender), String.valueOf(arguments()), String.valueOf(args.length));
return true;
}
return execute(sender, command, label, args);
}
if(args.length != arguments()) {
main.getLanguage().send("command.invalid", new BukkitCommandSender(sender), String.valueOf(arguments()), String.valueOf(args.length));
return true;
}
return execute(sender, command, label, new String[] {});
}
public abstract List<String> getTabCompletions(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args);
@Override
public final @Nullable List<String> onTabComplete(@NotNull CommandSender sender, org.bukkit.command.@NotNull Command command, @NotNull String alias, @NotNull String[] args) {
List<String> complete = new ArrayList<>();
if(args.length > 0) for(Command c : getSubCommands()) {
if(c.getName().startsWith(args[0])) complete.add(c.getName());
if(c.getName().equals(args[0])) return c.onTabComplete(sender, command, alias, Arrays.stream(args, 1, args.length).toArray(String[]::new));
}
complete.addAll(getTabCompletions(sender, alias, args));
return complete;
}
}

View File

@@ -1,8 +0,0 @@
package com.dfsek.terra.bukkit.command;
/**
* Implementing this interface marks a command as debug-only.
* If a parent command implements this interface, all child commands will be considered debug commands, regardless of whether they implement DebugCommand as well.
*/
public interface DebugCommand {
}

View File

@@ -0,0 +1,20 @@
package com.dfsek.terra.bukkit.command;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.platform.entity.Player;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
@Command
@WorldCommand
@PlayerCommand
public class FixChunkCommand implements CommandTemplate {
@Override
public void execute(CommandSender sender) {
Player player = (Player) sender;
BukkitChunkGeneratorWrapper.fixChunk(player.getWorld().getChunkAt(player.getLocation()));
}
}

View File

@@ -1,51 +0,0 @@
package com.dfsek.terra.bukkit.command;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
/**
* A command that may only be executed by a player. If executor is not a player, a message will be displayed and no action will be performed.
*/
public abstract class PlayerCommand extends Command {
public PlayerCommand(Command parent) {
super(parent);
}
/**
* Executes the given command, returning its success.
* <br>
* If false is returned, then the "usage" plugin.yml entry for this command
* (if defined) will be sent to the player.
*
* @param sender Source of the command
* @param command CommandTemplate which was executed
* @param label Alias of the command which was used
* @param args Passed command arguments
* @return true if a valid command, otherwise false
*/
@Override
public final boolean execute(@NotNull CommandSender sender, org.bukkit.command.@NotNull Command command, @NotNull String label, @NotNull String[] args) {
if(!(sender instanceof Player)) {
getMain().getLanguage().send("command.players-only", new BukkitCommandSender(sender));
return true;
}
Player p = (Player) sender;
return execute(p, command, label, args);
}
/**
* Executes the given command, returning its success.
* <br>
* If false is returned, then the "usage" plugin.yml entry for this command
* (if defined) will be sent to the player.
*
* @param sender Player that executed command
* @param command CommandTemplate which was executed
* @param label Alias of the command which was used
* @param args Passed command arguments
* @return true if a valid command, otherwise false
*/
public abstract boolean execute(@NotNull Player sender, org.bukkit.command.@NotNull Command command, @NotNull String label, @NotNull String[] args);
}

View File

@@ -0,0 +1,16 @@
package com.dfsek.terra.bukkit.command;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
import com.dfsek.terra.config.lang.LangUtil;
@Command
public class SaveDataCommand implements CommandTemplate {
@Override
public void execute(CommandSender sender) {
BukkitChunkGeneratorWrapper.saveAll();
LangUtil.send("debug.data-save", sender);
}
}

View File

@@ -1,53 +0,0 @@
package com.dfsek.terra.bukkit.command;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.generator.ChunkGenerator;
import org.jetbrains.annotations.NotNull;
/**
* A command that must be executed by a player, in a Terra world.
*/
public abstract class WorldCommand extends PlayerCommand {
public WorldCommand(Command parent) {
super(parent);
}
/**
* Executes the given command, returning its success.
* <br>
* If false is returned, then the "usage" plugin.yml entry for this command
* (if defined) will be sent to the player.
*
* @param sender Source of the command
* @param command Command which was executed
* @param label Alias of the command which was used
* @param args Passed command arguments
* @return true if a valid command, otherwise false
*/
@Override
public boolean execute(@NotNull Player sender, org.bukkit.command.@NotNull Command command, @NotNull String label, @NotNull String[] args) {
if(sender.getWorld().getGenerator() instanceof ChunkGenerator) { // TODO: implementation
return execute(sender, command, label, args, sender.getWorld());
} else {
getMain().getLanguage().send("command.world", new BukkitCommandSender(sender));
}
return true;
}
/**
* Executes the given command, returning its success.
* <br>
* If false is returned, then the "usage" plugin.yml entry for this command
* (if defined) will be sent to the player.
*
* @param sender Player that executed command
* @param command Command which was executed
* @param label Alias of the command which was used
* @param args Passed command arguments
* @param world World in which command was executed
* @return true if a valid command, otherwise false
*/
public abstract boolean execute(@NotNull Player sender, @NotNull org.bukkit.command.Command command, @NotNull String label, @NotNull String[] args, World world);
}

View File

@@ -1,45 +0,0 @@
package com.dfsek.terra.bukkit.command.command;
import com.dfsek.terra.bukkit.command.WorldCommand;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
import com.dfsek.terra.bukkit.world.BukkitAdapter;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class FixChunkCommand extends WorldCommand {
public FixChunkCommand(com.dfsek.terra.bukkit.command.Command parent) {
super(parent);
}
@Override
public boolean execute(@NotNull Player player, @NotNull Command command, @NotNull String s, @NotNull String[] strings, World world) {
BukkitChunkGeneratorWrapper.fixChunk(BukkitAdapter.adapt(player.getLocation().getChunk()));
return true;
}
@Override
public String getName() {
return "fixchunk";
}
@Override
public List<com.dfsek.terra.bukkit.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
@Override
public List<String> getTabCompletions(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] strings) {
return Collections.emptyList();
}
}

View File

@@ -1,47 +0,0 @@
package com.dfsek.terra.bukkit.command.command;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import com.dfsek.terra.bukkit.command.Command;
import com.dfsek.terra.bukkit.command.WorldCommand;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
import com.dfsek.terra.config.lang.LangUtil;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class SaveDataCommand extends WorldCommand {
public SaveDataCommand(Command parent) {
super(parent);
}
@Override
public String getName() {
return "save-data";
}
@Override
public List<Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
@Override
public List<String> getTabCompletions(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
return Collections.emptyList();
}
@Override
public boolean execute(@NotNull Player sender, org.bukkit.command.@NotNull Command command, @NotNull String label, @NotNull String[] args, World w) {
BukkitChunkGeneratorWrapper.saveAll();
LangUtil.send("debug.data-save", new BukkitCommandSender(sender), w.getName());
return true;
}
}

View File

@@ -1,75 +0,0 @@
package com.dfsek.terra.bukkit.command.command.geometry;
import com.dfsek.terra.api.math.noise.samplers.noise.simplex.OpenSimplex2Sampler;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.math.voxel.DeformedSphere;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import com.dfsek.terra.bukkit.command.PlayerCommand;
import com.dfsek.terra.bukkit.world.BukkitAdapter;
import com.dfsek.terra.config.lang.LangUtil;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class DeformedSphereCommand extends PlayerCommand {
public DeformedSphereCommand(com.dfsek.terra.bukkit.command.Command parent) {
super(parent);
}
@Override
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
int radius;
try {
radius = Integer.parseInt(args[0]);
} catch(NumberFormatException e) {
LangUtil.send("command.geometry.deform.invalid-radius", new BukkitCommandSender(sender), args[0]);
return true;
}
double deform;
try {
deform = Double.parseDouble(args[1]);
} catch(NumberFormatException e) {
LangUtil.send("command.geometry.deform.invalid-deform", new BukkitCommandSender(sender), args[1]);
return true;
}
double freq;
try {
freq = Float.parseFloat(args[2]);
} catch(NumberFormatException e) {
LangUtil.send("command.geometry.deform.invalid-frequency", new BukkitCommandSender(sender), args[2]);
return true;
}
OpenSimplex2Sampler n = new OpenSimplex2Sampler((int) sender.getWorld().getSeed());
n.setFrequency(freq);
DeformedSphere sphere = new DeformedSphere(BukkitAdapter.adapt(sender).getLocation().toVector(), radius, deform, n);
for(Vector3 v : sphere.getGeometry()) {
v.toLocation(BukkitAdapter.adapt(sender.getWorld())).getBlock().setBlockData(getMain().getWorldHandle().createBlockData("minecraft:stone"), false);
}
return true;
}
@Override
public String getName() {
return "deformedsphere";
}
@Override
public List<com.dfsek.terra.bukkit.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 3;
}
@Override
public List<String> getTabCompletions(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
return Collections.emptyList();
}
}

View File

@@ -1,46 +0,0 @@
package com.dfsek.terra.bukkit.command.command.geometry;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import com.dfsek.terra.bukkit.command.DebugCommand;
import com.dfsek.terra.bukkit.command.PlayerCommand;
import com.dfsek.terra.config.lang.LangUtil;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class GeometryCommand extends PlayerCommand implements DebugCommand {
public GeometryCommand(com.dfsek.terra.bukkit.command.Command parent) {
super(parent);
}
@Override
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
LangUtil.send("command.geometry.main-menu", new BukkitCommandSender(sender));
return true;
}
@Override
public String getName() {
return "geometry";
}
@Override
public List<com.dfsek.terra.bukkit.command.Command> getSubCommands() {
return Arrays.asList(new SphereCommand(this), new TubeCommand(this), new DeformedSphereCommand(this));
}
@Override
public int arguments() {
return 0;
}
@Override
public List<String> getTabCompletions(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
return Collections.emptyList();
}
}

View File

@@ -1,58 +0,0 @@
package com.dfsek.terra.bukkit.command.command.geometry;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.math.voxel.Sphere;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import com.dfsek.terra.bukkit.BukkitPlayer;
import com.dfsek.terra.bukkit.command.PlayerCommand;
import com.dfsek.terra.bukkit.world.BukkitAdapter;
import com.dfsek.terra.config.lang.LangUtil;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class SphereCommand extends PlayerCommand {
public SphereCommand(com.dfsek.terra.bukkit.command.Command parent) {
super(parent);
}
@Override
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
int radius;
try {
radius = Integer.parseInt(args[0]);
} catch(NumberFormatException e) {
LangUtil.send("command.geometry.sphere.invalid-radius", new BukkitCommandSender(sender), args[0]);
return true;
}
Sphere sphere = new Sphere(new BukkitPlayer(sender).getLocation().toVector(), radius);
for(Vector3 v : sphere.getGeometry()) {
v.toLocation(BukkitAdapter.adapt(sender.getWorld())).getBlock().setBlockData(getMain().getWorldHandle().createBlockData("minecraft:stone"), false);
}
return true;
}
@Override
public String getName() {
return "sphere";
}
@Override
public List<com.dfsek.terra.bukkit.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 1;
}
@Override
public List<String> getTabCompletions(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
return Collections.emptyList();
}
}

View File

@@ -1,61 +0,0 @@
package com.dfsek.terra.bukkit.command.command.geometry;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.math.voxel.Tube;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import com.dfsek.terra.bukkit.command.PlayerCommand;
import com.dfsek.terra.bukkit.structure.WorldEditUtil;
import com.dfsek.terra.bukkit.world.BukkitAdapter;
import com.dfsek.terra.config.lang.LangUtil;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class TubeCommand extends PlayerCommand {
public TubeCommand(com.dfsek.terra.bukkit.command.Command parent) {
super(parent);
}
@Override
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
Location[] l = WorldEditUtil.getSelectionPositions(sender);
if(l == null) return true;
int radius;
try {
radius = Integer.parseInt(args[0]);
} catch(NumberFormatException e) {
LangUtil.send("command.geometry.tube.invalid-radius", new BukkitCommandSender(sender), args[0]);
return true;
}
Tube tube = new Tube(BukkitAdapter.adapt(l[0].toVector()), BukkitAdapter.adapt(l[1].toVector()), radius);
for(Vector3 v : tube.getGeometry()) {
v.toLocation(BukkitAdapter.adapt(sender.getWorld())).getBlock().setBlockData(getMain().getWorldHandle().createBlockData("minecraft:stone"), false);
}
return true;
}
@Override
public String getName() {
return "tube";
}
@Override
public List<com.dfsek.terra.bukkit.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 1;
}
@Override
public List<String> getTabCompletions(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
return Collections.emptyList();
}
}