MassiveCore - Various Improvements

This commit is contained in:
Olof Larsson 2016-03-04 14:11:42 +01:00
parent ab533bbd2b
commit de2c1bc249
20 changed files with 293 additions and 70 deletions

View File

@ -25,6 +25,7 @@ import com.massivecraft.massivecore.MassivePlugin;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.command.requirement.Requirement;
import com.massivecraft.massivecore.command.requirement.RequirementAbstract;
import com.massivecraft.massivecore.command.requirement.RequirementHasPerm;
import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.mixin.Mixin;
@ -331,9 +332,9 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
public void addChild(MassiveCommand child, int index)
{
if (this.children.isEmpty() && ! (child instanceof HelpCommand))
if (this.children.isEmpty() && ! (child instanceof MassiveCommandHelp))
{
this.children.add(0, HelpCommand.get());
this.children.add(0, MassiveCommandHelp.get());
index++;
}
child.addToChain(this);
@ -718,20 +719,12 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
public boolean isRequirementsMet(CommandSender sender, boolean verboose)
{
String error = this.getRequirementsError(sender, verboose);
if (error != null && verboose) Mixin.messageOne(sender, error);
return error == null;
return RequirementAbstract.isRequirementsMet(this.getRequirements(), sender, this, verboose);
}
public String getRequirementsError(CommandSender sender, boolean verboose)
{
for (Requirement requirement : this.getRequirements())
{
if (requirement.apply(sender, this)) continue;
if ( ! verboose) return "";
return requirement.createErrorMessage(sender, this);
}
return null;
return RequirementAbstract.getRequirementsError(this.getRequirements(), sender, this, verboose);
}
// -------------------------------------------- //
@ -943,7 +936,7 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
List<MassiveCommand> chain = new ArrayList<MassiveCommand>(this.getChain());
chain.add(this);
HelpCommand.get().execute(this.sender, this.getArgs(), chain);
MassiveCommandHelp.get().execute(this.sender, this.getArgs(), chain);
}
// -------------------------------------------- //

View File

@ -1,6 +1,6 @@
package com.massivecraft.massivecore.command;
public class DeprecatedCommand extends MassiveCommand
public class MassiveCommandDeprecated extends MassiveCommand
{
// -------------------------------------------- //
// FIELDS
@ -12,7 +12,7 @@ public class DeprecatedCommand extends MassiveCommand
// CONSTRUCT
// -------------------------------------------- //
public DeprecatedCommand(MassiveCommand target, String... aliases)
public MassiveCommandDeprecated(MassiveCommand target, String... aliases)
{
// Fields
this.target = target;

View File

@ -11,15 +11,15 @@ import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.Txt;
public class HelpCommand extends MassiveCommand
public class MassiveCommandHelp extends MassiveCommand
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
protected static HelpCommand i = new HelpCommand();
public static HelpCommand get() { return i; }
private HelpCommand()
protected static MassiveCommandHelp i = new MassiveCommandHelp();
public static MassiveCommandHelp get() { return i; }
private MassiveCommandHelp()
{
// Aliases
this.addAliases("?", "h", "help");

View File

@ -0,0 +1,83 @@
package com.massivecraft.massivecore.command;
import java.util.List;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.primitive.TypeBoolean;
import com.massivecraft.massivecore.util.Txt;
public abstract class MassiveCommandToggle extends MassiveCommand
{
// -------------------------------------------- //
// FIELD
// -------------------------------------------- //
protected String info = null;
public String getInfo() { return this.info; }
public void setInfo(String info) { this.info = info; }
public String getInfoSuffix()
{
String info = this.getInfo();
if (info == null) return "";
return " " + info;
}
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public MassiveCommandToggle()
{
// Parameters
this.addParameter(TypeBoolean.getOn(), "on|off", "toggle");
}
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
public abstract boolean get();
public abstract void set(boolean value);
// -------------------------------------------- //
// GET NAME
// -------------------------------------------- //
public String getName()
{
List<String> aliases = this.getAliases();
return Txt.upperCaseFirst(aliases.get(aliases.size() - 1));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform() throws MassiveException
{
// Args
boolean before = this.get();
boolean after = this.readArg( ! before);
String afterDesc = TypeBoolean.getOn().getVisual(after, sender);
// NoChange
if (after == before)
{
String message = Txt.parse("<h>%s<i> is already <h>%s<i>.", this.getName(), afterDesc);
if (after) message += this.getInfoSuffix();
message(message);
return;
}
// Apply
set(after);
// Inform
String message = Txt.parse("<h>%s<i> is now <h>%s<i>.", this.getName(), afterDesc);
if (after) message += this.getInfoSuffix();
message(message);
}
}

View File

@ -10,7 +10,7 @@ import org.bukkit.plugin.PluginDescriptionFile;
import com.massivecraft.massivecore.command.requirement.RequirementHasPerm;
import com.massivecraft.massivecore.util.Txt;
public class VersionCommand extends MassiveCommand
public class MassiveCommandVersion extends MassiveCommand
{
// -------------------------------------------- //
// CONSTANTS
@ -29,12 +29,12 @@ public class VersionCommand extends MassiveCommand
// CONSTRUCT
// -------------------------------------------- //
public VersionCommand(Plugin plugin, String permissionName, String... aliases)
public MassiveCommandVersion(Plugin plugin, String permissionName, String... aliases)
{
this(plugin, permissionName, Arrays.asList(aliases));
}
public VersionCommand(Plugin plugin, String permissionName, Collection<String> aliases)
public MassiveCommandVersion(Plugin plugin, String permissionName, Collection<String> aliases)
{
this.plugin = plugin;

View File

@ -6,7 +6,7 @@ import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.MassiveCorePerm;
import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.command.VersionCommand;
import com.massivecraft.massivecore.command.MassiveCommandVersion;
import com.massivecraft.massivecore.command.requirement.RequirementHasPerm;
public class CmdMassiveCore extends MassiveCommand
@ -29,7 +29,7 @@ public class CmdMassiveCore extends MassiveCommand
public CmdMassiveCoreHearsound cmdMassiveCoreHearsound = new CmdMassiveCoreHearsound();
public CmdMassiveCoreBuffer cmdMassiveCoreBuffer = new CmdMassiveCoreBuffer();
public CmdMassiveCoreCmdurl cmdMassiveCoreCmdurl = new CmdMassiveCoreCmdurl();
public VersionCommand cmdMassiveCoreVersion = new VersionCommand(MassiveCore.get(), MassiveCorePerm.VERSION.node, "v", "version");
public MassiveCommandVersion cmdMassiveCoreVersion = new MassiveCommandVersion(MassiveCore.get(), MassiveCorePerm.VERSION.node, "v", "version");
// -------------------------------------------- //
// CONSTRUCT

View File

@ -5,11 +5,16 @@ import java.io.Serializable;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.mixin.Mixin;
public abstract class RequirementAbstract implements Requirement, Serializable
{
private static final long serialVersionUID = 1L;
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(CommandSender sender)
{
@ -28,4 +33,26 @@ public abstract class RequirementAbstract implements Requirement, Serializable
return command.getDesc();
}
// -------------------------------------------- //
// BULK
// -------------------------------------------- //
public static boolean isRequirementsMet(Iterable<Requirement> requirements, CommandSender sender, MassiveCommand command, boolean verboose)
{
String error = getRequirementsError(requirements, sender, command, verboose);
if (error != null && verboose) Mixin.messageOne(sender, error);
return error == null;
}
public static String getRequirementsError(Iterable<Requirement> requirements, CommandSender sender, MassiveCommand command, boolean verboose)
{
for (Requirement requirement : requirements)
{
if (requirement.apply(sender, command)) continue;
if ( ! verboose) return "";
return requirement.createErrorMessage(sender, command);
}
return null;
}
}

View File

@ -0,0 +1,62 @@
package com.massivecraft.massivecore.command.type;
import java.util.Collection;
import java.util.Collections;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.TypeAbstract;
public abstract class TypeNameAbstract extends TypeAbstract<String>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final boolean strict;
public boolean isStrict() { return this.strict; }
public boolean isLenient() { return ! this.isStrict(); }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public TypeNameAbstract(boolean strict)
{
this.strict = strict;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String read(String arg, CommandSender sender) throws MassiveException
{
if (arg == null) throw new NullPointerException("arg");
// Allow changing capitalization of the current name if lenient.
String current = this.getCurrentName(sender);
if (current != null && current.equalsIgnoreCase(arg) && this.isLenient()) return arg;
if (this.isNameTaken(arg)) throw new MassiveException().addMsg("<b>The name \"<h>%s<b>\" is already in use.",arg);
return arg;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return Collections.emptyList();
}
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
public abstract String getCurrentName(CommandSender sender);
public abstract boolean isNameTaken(String name);
}

View File

@ -2,6 +2,9 @@ package com.massivecraft.massivecore.comparator;
import java.util.Comparator;
import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.Prioritized;
import com.massivecraft.massivecore.store.Entity;
import com.massivecraft.massivecore.util.MUtil;
public class ComparatorAbstract<T> implements Comparator<T>
@ -35,7 +38,6 @@ public class ComparatorAbstract<T> implements Comparator<T>
@Override
public int compare(T type1, T type2)
{
// Create
Integer ret;
// Null
@ -44,18 +46,48 @@ public class ComparatorAbstract<T> implements Comparator<T>
// Inner
ret = this.compareInner(type1, type2);
if (ret != null) return ret;
// Return
return ret;
// Prioritized
if (type1 instanceof Prioritized)
{
Prioritized prioritized1 = (Prioritized)type1;
Prioritized prioritized2 = (Prioritized)type2;
ret = Integer.compare(prioritized1.getPriority(), prioritized2.getPriority());
if (ret != null) return ret;
}
// Named
if (type1 instanceof Named)
{
Named named1 = (Named)type1;
Named named2 = (Named)type2;
ret = ComparatorNaturalOrder.get().compare(named1.getName(), named2.getName());
if (ret != null) return ret;
}
// EntityId
if (type1 instanceof Entity<?>)
{
Entity<?> entity1 = (Entity<?>)type1;
Entity<?> entity2 = (Entity<?>)type2;
ret = MUtil.compare(entity1.getId(), entity2.getId());
if (ret != null) return ret;
}
// Identity
return ComparatorIdentity.get().compare(type1, type2);
}
// -------------------------------------------- //
// INNER
// -------------------------------------------- //
public int compareInner(T type1, T type2)
public Integer compareInner(T type1, T type2)
{
throw new UnsupportedOperationException("not implemented");
return null;
}
}

View File

@ -18,7 +18,7 @@ public abstract class ComparatorAbstractTransformer<T, X> extends ComparatorAbst
// -------------------------------------------- //
@Override
public int compareInner(T type1, T type2)
public Integer compareInner(T type1, T type2)
{
X x1 = this.transform(type1);
X x2 = this.transform(type2);

View File

@ -14,7 +14,7 @@ public class ComparatorCaseInsensitive extends ComparatorAbstract<String>
// -------------------------------------------- //
@Override
public int compareInner(String string1, String string2)
public Integer compareInner(String string1, String string2)
{
return String.CASE_INSENSITIVE_ORDER.compare(string1, string2);
}

View File

@ -16,14 +16,9 @@ public class ComparatorHashCode extends ComparatorAbstract<Object>
// -------------------------------------------- //
@Override
public int compareInner(Object object1, Object object2)
public Integer compareInner(Object object1, Object object2)
{
int ret;
ret = Integer.compare(Objects.hashCode(object1), Objects.hashCode(object2));
if (ret != 0) return ret;
return ret;
return Integer.compare(Objects.hashCode(object1), Objects.hashCode(object2));
}
}

View File

@ -0,0 +1,23 @@
package com.massivecraft.massivecore.comparator;
public class ComparatorIdentity extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static transient ComparatorIdentity i = new ComparatorIdentity();
public static ComparatorIdentity get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Integer compareInner(Object object1, Object object2)
{
if (object1 == object2) return 0;
return Integer.compare(System.identityHashCode(object1), System.identityHashCode(object2));
}
}

View File

@ -1,27 +0,0 @@
package com.massivecraft.massivecore.comparator;
public class ComparatorIdentityHashCode extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static transient ComparatorIdentityHashCode i = new ComparatorIdentityHashCode();
public static ComparatorIdentityHashCode get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compareInner(Object object1, Object object2)
{
int ret;
ret = Integer.compare(System.identityHashCode(object1), System.identityHashCode(object2));
if (ret != 0) return ret;
return ret;
}
}

View File

@ -29,7 +29,7 @@ public class ComparatorLenient<T> extends ComparatorAbstractWrapper<T, T>
ret = ComparatorHashCode.get().compare(type1, type2);
if (ret != 0) return ret;
ret = ComparatorIdentityHashCode.get().compare(type1, type2);
ret = ComparatorIdentity.get().compare(type1, type2);
if (ret != 0) return ret;
return 1;

View File

@ -43,7 +43,7 @@ public class ComparatorNaturalOrder extends ComparatorAbstract<Object>
// -------------------------------------------- //
@Override
public int compareInner(Object object1, Object object2)
public Integer compareInner(Object object1, Object object2)
{
// Martin Pool
String a = object1.toString();

View File

@ -17,7 +17,7 @@ public class ComparatorPriority extends ComparatorAbstract<Prioritized>
// -------------------------------------------- //
@Override
public int compareInner(Prioritized prioritized1, Prioritized prioritized2)
public Integer compareInner(Prioritized prioritized1, Prioritized prioritized2)
{
// Equals
if (prioritized1.equals(prioritized2)) return 0;
@ -35,7 +35,7 @@ public class ComparatorPriority extends ComparatorAbstract<Prioritized>
}
// We should only return 0 if the items actually are equal.
return Integer.compare(prioritized1.hashCode(), prioritized2.hashCode());
return ComparatorIdentity.get().compare(prioritized1, prioritized2);
}
}

View File

@ -7,6 +7,7 @@ import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.Txt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
@ -46,6 +47,7 @@ public class Pager<T>
protected List<String> args = null;
public boolean hasArgs() { return this.args != null; }
public Pager<T> setArgs(List<String> args) { this.args = args; return this; }
public Pager<T> setArgs(String... args) { this.setArgs(Arrays.asList(args)); return this; }
public List<String> getArgs() { return this.args; }
public List<String> getArgsCalc()
{

View File

@ -14,9 +14,12 @@ import java.util.concurrent.ConcurrentSkipListMap;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.MassivePlugin;
import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.comparator.ComparatorNaturalOrder;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.predicate.Predicate;
import com.massivecraft.massivecore.predicate.PredicateEqualsIgnoreCase;
import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.massivecore.xlib.gson.Gson;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
@ -1019,4 +1022,32 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
return name2instance.containsKey(this.getName());
}
// -------------------------------------------- //
// NAME UTILITIES
// -------------------------------------------- //
public E getByName(String name)
{
if (name == null) throw new NullPointerException("name");
Predicate<String> predicate = PredicateEqualsIgnoreCase.get(name);
for (E entity : this.getAll())
{
if (entity == null) continue;
if ( ! (entity instanceof Named)) continue;
Named named = (Named)entity;
if (predicate.apply(named.getName())) return entity;
}
return null;
}
public boolean isNameTaken(String str)
{
return this.getByName(str) != null;
}
}

View File

@ -167,6 +167,7 @@ public class Txt
public static String parse(String string)
{
if (string == null) return null;
StringBuffer ret = new StringBuffer();
Matcher matcher = parsePattern.matcher(string);
while (matcher.find())
@ -198,6 +199,7 @@ public class Txt
public static ArrayList<String> wrap(final String string)
{
if (string == null) return null;
return new ArrayList<String>(Arrays.asList(PATTERN_NEWLINE.split(string)));
}