Splited AdminSchemCommand in various sub commands

Locale needs to be updated.
This commit is contained in:
Florian CUNY 2018-09-02 11:13:58 +02:00
parent ffe1363c48
commit e380860b23
9 changed files with 340 additions and 141 deletions

View File

@ -1,141 +0,0 @@
package world.bentobox.bentobox.api.commands.admin;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.util.Util;
public class AdminSchemCommand extends ConfirmableCommand {
private Map<UUID, Clipboard> clipboards;
public AdminSchemCommand(CompositeCommand parent) {
super(parent, "schem");
}
@Override
public void setup() {
setPermission("admin.schem");
setParametersHelp("commands.admin.schem.parameters");
setDescription("commands.admin.schem.description");
setOnlyPlayer(true);
clipboards = new HashMap<>();
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.isEmpty()) {
showHelp(this, user);
return false;
}
File schemFolder = new File(getIWM().getDataFolder(getWorld()), "schems");
Clipboard cb = clipboards.getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), schemFolder));
if (args.get(0).equalsIgnoreCase("paste")) {
if (cb.isFull()) {
cb.pasteClipboard(user.getLocation());
user.sendMessage("general.success");
return true;
} else {
user.sendMessage("commands.admin.schem.copy-first");
return false;
}
}
if (args.get(0).equalsIgnoreCase("load")) {
if (args.size() == 2) {
if (cb.load(user, args.get(1))) {
clipboards.put(user.getUniqueId(), cb);
return true;
}
} else {
showHelp(this, user);
return false;
}
return false;
}
if (args.get(0).equalsIgnoreCase("origin")) {
if (cb.getPos1() == null || cb.getPos2() == null) {
user.sendMessage("commands.admin.schem.need-pos1-pos2");
return false;
}
// Get the block player is looking at
Block b = user.getPlayer().getLineOfSight(null, 20).stream().filter(x -> !x.getType().equals(Material.AIR)).findFirst().orElse(null);
if (b != null) {
cb.setOrigin(b.getLocation());
user.getPlayer().sendBlockChange(b.getLocation(), Material.REDSTONE_BLOCK.createBlockData());
Bukkit.getScheduler().runTaskLater(getPlugin(),
() -> user.getPlayer().sendBlockChange(b.getLocation(), b.getBlockData()), 20L);
user.sendMessage("general.success");
return true;
} else {
user.sendMessage("commands.admin.schem.look-at-a-block");
return false;
}
}
if (args.get(0).equalsIgnoreCase("copy")) {
boolean copyAir = (args.size() == 2 && args.get(1).equalsIgnoreCase("air"));
return cb.copy(user, copyAir);
}
if (args.get(0).equalsIgnoreCase("save")) {
if (cb.isFull()) {
if (args.size() == 2) {
// Check if file exists
File newFile = new File(schemFolder, args.get(1) + ".schem");
if (newFile.exists()) {
user.sendMessage("commands.admin.schem.file-exists");
this.askConfirmation(user, () -> cb.save(user, args.get(1)));
return false;
} else {
return cb.save(user, args.get(1));
}
} else {
showHelp(this, user);
return false;
}
} else {
user.sendMessage("commands.admin.schem.copy-first");
return false;
}
}
if (args.get(0).equalsIgnoreCase("pos1")) {
if (user.getLocation().equals(cb.getPos2())) {
user.sendMessage("commands.admin.schem.set-different-pos");
return false;
}
cb.setPos1(user.getLocation());
user.sendMessage("commands.admin.schem.set-pos1", "[vector]", Util.xyz(user.getLocation().toVector()));
clipboards.put(user.getUniqueId(), cb);
return true;
}
if (args.get(0).equalsIgnoreCase("pos2")) {
if (user.getLocation().equals(cb.getPos1())) {
user.sendMessage("commands.admin.schem.set-different-pos");
return false;
}
cb.setPos2(user.getLocation());
user.sendMessage("commands.admin.schem.set-pos2", "[vector]", Util.xyz(user.getLocation().toVector()));
clipboards.put(user.getUniqueId(), cb);
return true;
}
return false;
}
}

View File

@ -0,0 +1,51 @@
package world.bentobox.bentobox.api.commands.admin.schem;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
public class AdminSchemCommand extends ConfirmableCommand {
private Map<UUID, Clipboard> clipboards;
public AdminSchemCommand(CompositeCommand parent) {
super(parent, "schem");
}
@Override
public void setup() {
setPermission("admin.schem");
setParametersHelp("commands.admin.schem.parameters");
setDescription("commands.admin.schem.description");
setOnlyPlayer(true);
clipboards = new HashMap<>();
new AdminSchemLoadCommand(this);
new AdminSchemPasteCommand(this);
new AdminSchemOriginCommand(this);
new AdminSchemCopyCommand(this);
new AdminSchemSaveCommand(this);
new AdminSchemPos1Command(this);
new AdminSchemPos2Command(this);
}
@Override
public boolean execute(User user, String label, List<String> args) {
showHelp(this, user);
return true;
}
Map<UUID, Clipboard> getClipboards() {
return clipboards;
}
File getSchemsFolder() {
return new File(getIWM().getDataFolder(getWorld()), "schems");
}
}

View File

@ -0,0 +1,34 @@
package world.bentobox.bentobox.api.commands.admin.schem;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import java.util.List;
public class AdminSchemCopyCommand extends CompositeCommand {
public AdminSchemCopyCommand(AdminSchemCommand parent) {
super(parent, "copy");
}
@Override
public void setup() {
setParametersHelp("commands.admin.schem.copy.parameters");
setDescription("commands.admin.schem.copy.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.size() > 1) {
showHelp(this, user);
return false;
}
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
boolean copyAir = (args.size() == 1 && args.get(0).equalsIgnoreCase("air"));
return clipboard.copy(user, copyAir);
}
}

View File

@ -0,0 +1,52 @@
package world.bentobox.bentobox.api.commands.admin.schem;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.util.Util;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class AdminSchemLoadCommand extends CompositeCommand {
public AdminSchemLoadCommand(AdminSchemCommand parent) {
super(parent, "load");
}
@Override
public void setup() {
setParametersHelp("commands.admin.schem.load.parameters");
setDescription("commands.admin.schem.load.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.isEmpty() || args.size() != 1) {
showHelp(this, user);
return false;
}
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
if (clipboard.load(user, args.get(0))) {
parent.getClipboards().put(user.getUniqueId(), clipboard);
return true;
}
return false;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
List<String> options = new ArrayList<>();
options.add("island");
options.add("nether-island");
options.add("end-island");
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
return Optional.of(Util.tabLimit(options, lastArg));
}
}

View File

@ -0,0 +1,49 @@
package world.bentobox.bentobox.api.commands.admin.schem;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import java.util.List;
public class AdminSchemOriginCommand extends CompositeCommand {
public AdminSchemOriginCommand(AdminSchemCommand parent) {
super(parent, "origin");
}
@Override
public void setup() {
setParametersHelp("commands.admin.schem.origin.parameters");
setDescription("commands.admin.schem.origin.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
if (clipboard.getPos1() == null || clipboard.getPos2() == null) {
user.sendMessage("commands.admin.schem.need-pos1-pos2");
return false;
}
// Get the block player is looking at
Block b = user.getPlayer().getLineOfSight(null, 20).stream().filter(x -> !x.getType().equals(Material.AIR)).findFirst().orElse(null);
if (b != null) {
clipboard.setOrigin(b.getLocation());
user.getPlayer().sendBlockChange(b.getLocation(), Material.REDSTONE_BLOCK.createBlockData());
Bukkit.getScheduler().runTaskLater(getPlugin(),
() -> user.getPlayer().sendBlockChange(b.getLocation(), b.getBlockData()), 20L);
user.sendMessage("general.success");
return true;
}
user.sendMessage("commands.admin.schem.look-at-a-block");
return false;
}
}

View File

@ -0,0 +1,35 @@
package world.bentobox.bentobox.api.commands.admin.schem;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import java.util.List;
public class AdminSchemPasteCommand extends CompositeCommand {
public AdminSchemPasteCommand(AdminSchemCommand parent) {
super(parent, "paste");
}
@Override
public void setup() {
setParametersHelp("commands.admin.schem.paste.parameters");
setDescription("commands.admin.schem.paste.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
if (clipboard.isFull()) {
clipboard.pasteClipboard(user.getLocation());
user.sendMessage("general.success");
return true;
}
user.sendMessage("commands.admin.schem.copy-first");
return false;
}
}

View File

@ -0,0 +1,36 @@
package world.bentobox.bentobox.api.commands.admin.schem;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.util.Util;
import java.util.List;
public class AdminSchemPos1Command extends CompositeCommand {
public AdminSchemPos1Command(AdminSchemCommand parent) {
super(parent, "pos1");
}
@Override
public void setup() {
setParametersHelp("commands.admin.schem.pos1.parameters");
setDescription("commands.admin.schem.pos1.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
if (user.getLocation().equals(clipboard.getPos2())) {
user.sendMessage("commands.admin.schem.set-different-pos");
return false;
}
clipboard.setPos1(user.getLocation());
user.sendMessage("commands.admin.schem.set-pos1", "[vector]", Util.xyz(user.getLocation().toVector()));
parent.getClipboards().put(user.getUniqueId(), clipboard);
return true;
}
}

View File

@ -0,0 +1,36 @@
package world.bentobox.bentobox.api.commands.admin.schem;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.util.Util;
import java.util.List;
public class AdminSchemPos2Command extends CompositeCommand {
public AdminSchemPos2Command(AdminSchemCommand parent) {
super(parent, "pos2");
}
@Override
public void setup() {
setParametersHelp("commands.admin.schem.pos2.parameters");
setDescription("commands.admin.schem.pos2.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
if (user.getLocation().equals(clipboard.getPos1())) {
user.sendMessage("commands.admin.schem.set-different-pos");
return false;
}
clipboard.setPos2(user.getLocation());
user.sendMessage("commands.admin.schem.set-pos2", "[vector]", Util.xyz(user.getLocation().toVector()));
parent.getClipboards().put(user.getUniqueId(), clipboard);
return true;
}
}

View File

@ -0,0 +1,47 @@
package world.bentobox.bentobox.api.commands.admin.schem;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import java.io.File;
import java.util.List;
public class AdminSchemSaveCommand extends ConfirmableCommand {
public AdminSchemSaveCommand(AdminSchemCommand parent) {
super(parent, "save");
}
@Override
public void setup() {
setParametersHelp("commands.admin.schem.save.parameters");
setDescription("commands.admin.schem.save.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.size() != 1) {
showHelp(this, user);
return false;
}
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
if (clipboard.isFull()) {
// Check if file exists
File newFile = new File(parent.getSchemsFolder(), args.get(0) + ".schem");
if (newFile.exists()) {
user.sendMessage("commands.admin.schem.file-exists");
this.askConfirmation(user, () -> clipboard.save(user, args.get(0)));
return false;
} else {
return clipboard.save(user, args.get(0));
}
} else {
user.sendMessage("commands.admin.schem.copy-first");
return false;
}
}
}