Create ConfirmationUtil

This is a util primarily used for commands that need extra confirmation
This commit is contained in:
Magnus Ulf 2019-01-19 12:19:20 +01:00
parent 6c41b3d06b
commit d602d4cd61
3 changed files with 269 additions and 34 deletions

View File

@ -1263,10 +1263,55 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
public Mson getTemplate(boolean addDesc, boolean onlyFirstAlias, CommandSender sender) public Mson getTemplate(boolean addDesc, boolean onlyFirstAlias, CommandSender sender)
{ {
// Create Ret // Get base
Mson ret = this.getTemplateChain(onlyFirstAlias, sender);
List<MassiveCommand> commands = this.getChain(true);
// Check if last command is parentCommand and make command suggestable/clickable
if (commands.get(commands.size() - 1).isParent())
{
ret = ret.command(this);
}
else
{
ret = ret.suggest(this);
}
// Add args
for (Mson parameter : this.getTemplateParameters(sender))
{
ret = ret.add(Mson.SPACE);
ret = ret.add(parameter.color(ChatColor.DARK_AQUA));
}
// Add desc
if (addDesc)
{
ret = ret.add(Mson.SPACE);
ret = ret.add(mson(this.getDesc()).color(ChatColor.YELLOW));
}
// Return Ret
return ret;
}
public Mson getTemplateWithArgs(CommandSender sender, List<String> args)
{
Mson ret = this.getTemplateChain(true, sender);
for (String arg : args)
{
ret = ret.add(Mson.SPACE);
ret = ret.add(mson(arg).color(ChatColor.DARK_AQUA));
}
return ret;
}
public Mson getTemplateChain(boolean onlyFirstAlias, CommandSender sender)
{
Mson ret = TEMPLATE_CORE; Mson ret = TEMPLATE_CORE;
// Get commands
List<MassiveCommand> commands = this.getChain(true); List<MassiveCommand> commands = this.getChain(true);
// Add commands // Add commands
@ -1298,31 +1343,6 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
first = false; first = false;
} }
// Check if last command is parentCommand and make command suggestable/clickable
if (commands.get(commands.size() - 1).isParent())
{
ret = ret.command(this);
}
else
{
ret = ret.suggest(this);
}
// Add args
for (Mson parameter : this.getTemplateParameters(sender))
{
ret = ret.add(Mson.SPACE);
ret = ret.add(parameter.color(ChatColor.DARK_AQUA));
}
// Add desc
if (addDesc)
{
ret = ret.add(Mson.SPACE);
ret = ret.add(mson(this.getDesc()).color(ChatColor.YELLOW));
}
// Return Ret
return ret; return ret;
} }

View File

@ -0,0 +1,41 @@
package com.massivecraft.massivecore.command.type.primitive;
import com.massivecraft.massivecore.command.type.TypeAbstract;
import org.bukkit.command.CommandSender;
import java.util.Collection;
import java.util.Collections;
public class TypeStringConfirmation extends TypeAbstract<String>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypeStringConfirmation i = new TypeStringConfirmation();
public static TypeStringConfirmation get() { return i; }
public TypeStringConfirmation() { super(String.class); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String getName()
{
return "confirmation text";
}
@Override
public String read(String arg, CommandSender sender)
{
return arg;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return Collections.emptySet();
}
}

View File

@ -0,0 +1,174 @@
package com.massivecraft.massivecore.util;
import com.massivecraft.massivecore.Lang;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.command.type.primitive.TypeStringConfirmation;
import com.massivecraft.massivecore.mixin.MixinMessage;
import com.massivecraft.massivecore.mson.Mson;
import org.apache.commons.lang.RandomStringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
public class ConfirmationUtil
{
// -------------------------------------------- //
// STORE
// -------------------------------------------- //
private static Map<Object, WeakHashMap<CommandSender, String>> objectToConfirmationMap = new HashMap<>();
// -------------------------------------------- //
// DERP
// -------------------------------------------- //
private static Map<CommandSender, String> getConfirmationMap(Object obj)
{
if (obj == null) throw new NullPointerException("obj");
objectToConfirmationMap.putIfAbsent(obj, new WeakHashMap<>());
return objectToConfirmationMap.get(obj);
}
public static String createConfirmationString(Object obj, CommandSender sender)
{
if (obj == null) throw new NullPointerException("obj");
if (sender == null) throw new NullPointerException("sender");
String str = RandomStringUtils.randomAlphanumeric(6);
getConfirmationMap(obj).put(sender, str);
scheduleRemove(obj, sender, 5 * 60);
return str;
}
public static void scheduleRemove(Object obj, CommandSender sender, long seconds)
{
if (obj == null) throw new NullPointerException("obj");
if (sender == null) throw new NullPointerException("sender");
Bukkit.getScheduler().runTaskLater(MassiveCore.get(), () -> removeConfirmationString(obj, sender), 20L * seconds);
}
public static boolean removeConfirmationString(Object obj, CommandSender sender)
{
if (obj == null) throw new NullPointerException("obj");
if (sender == null) throw new NullPointerException("sender");
return getConfirmationMap(obj).remove(sender) != null;
}
public static String getConfirmationString(Object obj, CommandSender sender)
{
if (obj == null) throw new NullPointerException("obj");
if (sender == null) throw new NullPointerException("sender");
return getConfirmationMap(obj).get(sender);
}
public static boolean isConfirmationString(Object obj, CommandSender sender, String str)
{
if (obj == null) throw new NullPointerException("obj");
if (sender == null) throw new NullPointerException("sender");
if (str == null) throw new NullPointerException("str");
return str.equals(getConfirmationString(obj, sender));
}
public static boolean hasConfirmationString(Object obj, CommandSender sender)
{
if (obj == null) throw new NullPointerException("obj");
if (sender == null) throw new NullPointerException("sender");
return getConfirmationMap(obj).containsKey(sender);
}
public static void tryConfirm(MassiveCommand object) throws MassiveException
{
if (object == null) throw new NullPointerException("object");
CommandSender sender = object.sender;
if (sender == null) throw new NullPointerException("sender");
int idx = getConfirmationIdx(object);
boolean hasConfirmationString = hasConfirmationString(object, sender);
boolean hasTypedConfirmationString = object.argIsSet(idx);
boolean hasTypedOther = object.argIsSet(idx-1);
// Assetion should not happen
if (hasTypedConfirmationString && !hasTypedOther) throw new RuntimeException();
if (!hasTypedOther)
{
MassiveException ex = new MassiveException();
ex.addMsg(Lang.COMMAND_TOO_FEW_ARGUMENTS);
ex.addMessage(object.getTemplate(false, true, sender));
throw ex;
}
else if (hasConfirmationString && !hasTypedConfirmationString)
{
throw getException(object);
}
else if (hasConfirmationString && hasTypedConfirmationString)
{
// If they typed the wrong thing
String typedString = object.argAt(idx);
if (!isConfirmationString(object, sender, typedString))
{
// Make them type the right string
throw getException(object);
}
// Otherwise do nothing, because success
}
else if (!hasConfirmationString && hasTypedOther)
{
String confirmationString = createConfirmationString(object, sender);
MixinMessage.get().msgOne(sender, "<i>Created confirmation text <h>%s<i> for you.", confirmationString);
throw getException(object);
}
}
private static int getConfirmationIdx(MassiveCommand command)
{
int idx = -1;
for (int i = 0; i < command.getParameters().size(); i++)
{
Type<?> type = command.getParameterType(i);
if (!(type instanceof TypeStringConfirmation)) continue;
idx = i;
}
if (idx == -1) throw new IllegalStateException("no confirmation string type");
return idx;
}
private static MassiveException getException(MassiveCommand command)
{
CommandSender sender = command.sender;
if (sender == null) throw new NullPointerException("sender");
int idx = getConfirmationIdx(command);
List<String> args = new MassiveList<>(command.getArgs());
args.set(idx, getConfirmationString(command, sender));
Mson template = command.getTemplateWithArgs(sender, args);
MassiveException ex = new MassiveException();
ex.addMsg("<b>To <h>%s <b>confirm it by typing:", command.getDesc());
ex.addMessage(template);
return ex;
}
}