Command confirmation for `/as import`

- Can also be used for all WordEdit based add/delete actions in the future
This commit is contained in:
Thijs Wiefferink 2018-01-26 16:16:11 +01:00
parent 741bbd879f
commit 4530717045
3 changed files with 37 additions and 5 deletions

View File

@ -1,10 +1,14 @@
package me.wiefferink.areashop.commands;
import javafx.util.Pair;
import me.wiefferink.areashop.AreaShop;
import me.wiefferink.interactivemessenger.processing.Message;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -15,7 +19,11 @@ public abstract class CommandAreaShop {
AreaShop plugin = AreaShop.getInstance();
private Map<String, Long> confirmed;
private Map<String, Pair<String, Long>> lastUsed;
public CommandAreaShop() {
lastUsed = new HashMap<>();
}
/**
* Check if this Command instance can execute the given command and arguments.
@ -24,10 +32,7 @@ public abstract class CommandAreaShop {
* @return true if it can execute the command, false otherwise
*/
public boolean canExecute(Command command, String[] args) {
String commandString = command.getName();
for(String argument : args) {
commandString += " " + argument;
}
String commandString = command.getName() + " " + StringUtils.join(args, " ");
if(commandString.length() > getCommandStart().length()) {
return commandString.toLowerCase().startsWith(getCommandStart().toLowerCase() + " ");
}
@ -65,5 +70,24 @@ public abstract class CommandAreaShop {
*/
public abstract void execute(CommandSender sender, String[] args);
/**
* Confirm a command.
* @param sender To confirm it for, or send a message to confirm
* @param args Command args
* @param message Message to send when confirmation is required
* @return true if confirmed, false if confirmation is required
*/
public boolean confirm(CommandSender sender, String[] args, Message message) {
String command = "/" + getCommandStart() + " " + StringUtils.join(args, " ", 1, args.length);
long now = System.currentTimeMillis();
Pair<String, Long> last = lastUsed.get(sender.getName());
if(last != null && last.getKey().equalsIgnoreCase(command) && last.getValue() > (now - 1000 * 60)) {
return true;
}
message.prefix().append(Message.fromKey("confirm-yes").replacements(command)).send(sender);
lastUsed.put(sender.getName(), new Pair<>(command, now));
return false;
}
}

View File

@ -1,5 +1,6 @@
package me.wiefferink.areashop.commands;
import me.wiefferink.interactivemessenger.processing.Message;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
@ -49,6 +50,10 @@ public class ImportCommand extends CommandAreaShop {
return;
}
if(!confirm(sender, args, Message.fromKey("import-confirm"))) {
return;
}
ImportJob importJob = new ImportJob(sender);
}

View File

@ -47,6 +47,8 @@ landlord:
total-maximum: "You can't rent and buy more than %0% region(s) in total (you already have %1% in group '%2%')."
confirm-yes: " [gray]Click to %lang:command|Confirm|%0%|%[gray], or repeat the command."
general-notReady: "AreaShop has not fully loaded yet, please wait."
general-noWorld: "You cannot do that, the world of %lang:region% is currently unavailable (world '%world%')."
general-noRegion: "You cannot do that, the WorldGuard region is currently unavailable (region %lang:region%)."
@ -545,6 +547,7 @@ import-help: "/as import RegionForSale"
import-wrongSource: "Specify from where you want to import data, options: RegionForSale."
import-noPluginFolder: "Could not find the %0% folder, are you sure you are importing from the correct source?"
import-noWorldsFolder: "There is no 'plugins/RegionForSale/worlds' folder, therefore nothing could be imported."
import-confirm: "Are you sure you want to import all regions from RegionForSale?"
import-start: "Starting import of RegionForSale data:"
import-doWorld: "[darkgreen][bold]► World %0%:"
import-noBukkitWorld: "[red]Could not find world in Bukkit."