mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-24 17:47:38 +01:00
Added dynamic command handler from Dungeon Maze
This commit is contained in:
parent
3eb29da4a7
commit
1897b1f5e6
@ -0,0 +1,91 @@
|
||||
package fr.xephi.authme.commands.dynamic;
|
||||
|
||||
public class CommandArgumentDescription {
|
||||
|
||||
// TODO: Allow argument to consist of infinite parts. <label ...>
|
||||
|
||||
/** Argument label. */
|
||||
private String label;
|
||||
/** Argument description. */
|
||||
private String description;
|
||||
/** Defines whether the argument is optional. */
|
||||
private boolean optional = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param label The argument label.
|
||||
* @param description The argument description.
|
||||
*/
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public CommandArgumentDescription(String label, String description) {
|
||||
this(label, description, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param label The argument label.
|
||||
* @param description The argument description.
|
||||
* @param optional True if the argument is optional, false otherwise.
|
||||
*/
|
||||
public CommandArgumentDescription(String label, String description, boolean optional) {
|
||||
setLabel(label);
|
||||
setDescription(description);
|
||||
setOptional(optional);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the argument label.
|
||||
*
|
||||
* @return Argument label.
|
||||
*/
|
||||
public String getLabel() {
|
||||
return this.label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the argument label.
|
||||
*
|
||||
* @param label Argument label.
|
||||
*/
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the argument description.
|
||||
*
|
||||
* @return Argument description.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the argument description.
|
||||
*
|
||||
* @param description Argument description.
|
||||
*/
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the argument is optional.
|
||||
*
|
||||
* @return True if the argument is optional, false otherwise.
|
||||
*/
|
||||
public boolean isOptional() {
|
||||
return optional;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether the argument is optional.
|
||||
*
|
||||
* @param optional True if the argument is optional, false otherwise.
|
||||
*/
|
||||
public void setOptional(boolean optional) {
|
||||
this.optional = optional;
|
||||
}
|
||||
}
|
@ -0,0 +1,912 @@
|
||||
package fr.xephi.authme.commands.dynamic;
|
||||
|
||||
import com.timvisee.dungeonmaze.util.StringUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public class CommandDescription {
|
||||
|
||||
/** Defines the acceptable labels. */
|
||||
private List<String> labels = new ArrayList<>();
|
||||
/** Command description. */
|
||||
private String description = "";
|
||||
/** Detailed description. */
|
||||
private String detailedDescription = "";
|
||||
/** The executable command instance. */
|
||||
private ExecutableCommand executableCommand;
|
||||
/** The parent command. */
|
||||
private CommandDescription parent = null;
|
||||
/** The child labels. */
|
||||
private List<CommandDescription> childs = new ArrayList<>();
|
||||
/** The command arguments. */
|
||||
private List<CommandArgumentDescription> arguments = new ArrayList<>();
|
||||
/** Defines whether there is an argument maximum or not. */
|
||||
private boolean noArgumentMaximum = false;
|
||||
/** Defines the command permissions. */
|
||||
private CommandPermissions permissions = new CommandPermissions();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param label Command label.
|
||||
* @param description Command description.
|
||||
* @param detailedDescription Detailed comment description.
|
||||
* @param parent Parent command.
|
||||
*/
|
||||
public CommandDescription(ExecutableCommand executableCommand, String label, String description, String detailedDescription, CommandDescription parent) {
|
||||
this(executableCommand, label, description, parent, detailedDescription, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param labels List of command labels.
|
||||
* @param description Command description.
|
||||
* @param detailedDescription Detailed comment description.
|
||||
* @param parent Parent command.
|
||||
*/
|
||||
public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent) {
|
||||
this(executableCommand, labels, description, detailedDescription, parent, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param label Command label.
|
||||
* @param description Command description.
|
||||
* @param parent Parent command.
|
||||
* @param detailedDescription Detailed comment description.
|
||||
* @param arguments Command arguments.
|
||||
*/
|
||||
public CommandDescription(ExecutableCommand executableCommand, String label, String description, CommandDescription parent, String detailedDescription, List<CommandArgumentDescription> arguments) {
|
||||
setExecutableCommand(executableCommand);
|
||||
setLabel(label);
|
||||
setDescription(description);
|
||||
setDetailedDescription(detailedDescription);
|
||||
setParent(parent);
|
||||
setArguments(arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param labels List of command labels.
|
||||
* @param description Command description.
|
||||
* @param detailedDescription Detailed comment description.
|
||||
* @param parent Parent command.
|
||||
* @param arguments Command arguments.
|
||||
*/
|
||||
public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent, List<CommandArgumentDescription> arguments) {
|
||||
setExecutableCommand(executableCommand);
|
||||
setLabels(labels);
|
||||
setDescription(description);
|
||||
setDetailedDescription(detailedDescription);
|
||||
setParent(parent);
|
||||
setArguments(arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first relative command label.
|
||||
*
|
||||
* @return First relative command label.
|
||||
*/
|
||||
public String getLabel() {
|
||||
// Ensure there's any item in the command list
|
||||
if(this.labels.size() == 0)
|
||||
return "";
|
||||
|
||||
// Return the first command on the list
|
||||
return this.labels.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the label most similar to the reference. The first label will be returned if no reference was supplied.
|
||||
*
|
||||
* @param reference The command reference.
|
||||
*
|
||||
* @return The most similar label, or the first label. An empty label will be returned if no label was set.
|
||||
*/
|
||||
public String getLabel(CommandParts reference) {
|
||||
// Ensure there's any item in the command list
|
||||
if(this.labels.size() == 0)
|
||||
return "";
|
||||
|
||||
// Return the first label if we can't use the reference
|
||||
if(reference == null)
|
||||
return this.labels.get(0);
|
||||
|
||||
// Get the correct label from the reference
|
||||
String preferred = reference.get(getParentCount());
|
||||
|
||||
// Check whether the preferred label is in the label list
|
||||
double currentDifference = -1;
|
||||
String currentLabel = this.labels.get(0);
|
||||
for(String entry : this.labels) {
|
||||
double entryDifference = StringUtils.getDifference(entry, preferred);
|
||||
if(entryDifference < currentDifference || currentDifference < 0) {
|
||||
currentDifference = entryDifference;
|
||||
currentLabel = entry;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the most similar label
|
||||
return currentLabel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all relative command labels.
|
||||
*
|
||||
* @return All relative labels labels.
|
||||
*/
|
||||
public List<String> getLabels() {
|
||||
return this.labels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the list of command labels.
|
||||
*
|
||||
* @param labels New list of command labels. Null to clear the list of labels.
|
||||
*/
|
||||
public void setLabels(List<String> labels) {
|
||||
// Check whether the command label list should be cleared
|
||||
if(labels == null)
|
||||
this.labels.clear();
|
||||
|
||||
else
|
||||
this.labels = labels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command label, this will append the command label to already existing ones.
|
||||
*
|
||||
* @param commandLabel Command label to set or add.
|
||||
*/
|
||||
public void setLabel(String commandLabel) {
|
||||
setLabel(commandLabel, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command label.
|
||||
*
|
||||
* @param commandLabel Command label to set.
|
||||
* @param overwrite True to replace all old command labels, false to append this command label to the currently
|
||||
* existing labels.
|
||||
*
|
||||
* @return Trie if the command label is added, or if it was added already. False on failure.
|
||||
*/
|
||||
public boolean setLabel(String commandLabel, boolean overwrite) {
|
||||
// Check whether this new command should overwrite the previous ones
|
||||
if(!overwrite)
|
||||
return addLabel(commandLabel);
|
||||
|
||||
// Replace all labels with this new one
|
||||
this.labels.clear();
|
||||
return this.labels.add(commandLabel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a command label to the list.
|
||||
*
|
||||
* @param commandLabel Command label to add.
|
||||
*
|
||||
* @return True if the label was added, or if it was added already. False on error.
|
||||
*/
|
||||
public boolean addLabel(String commandLabel) {
|
||||
// Verify the label
|
||||
if(!isValidLabel(commandLabel))
|
||||
return false;
|
||||
|
||||
// Ensure this command isn't a duplicate
|
||||
if(hasLabel(commandLabel))
|
||||
return true;
|
||||
|
||||
// Add the command to the list
|
||||
return this.labels.add(commandLabel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a list of command labels.
|
||||
*
|
||||
* @param commandLabels List of command labels to add.
|
||||
*
|
||||
* @return True if succeed, false on failure.
|
||||
*/
|
||||
public boolean addLabels(List<String> commandLabels) {
|
||||
// Add each command label separately
|
||||
for(String cmd : commandLabels)
|
||||
if(!addLabel(cmd))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command description has a specific command.
|
||||
*
|
||||
* @param commandLabel Command to check for.
|
||||
*
|
||||
* @return True if this command label equals to the param command.
|
||||
*/
|
||||
public boolean hasLabel(String commandLabel) {
|
||||
// Check whether any command matches with the argument
|
||||
for(String entry : this.labels)
|
||||
if(commandLabelEquals(entry, commandLabel))
|
||||
return true;
|
||||
|
||||
// No match found, return false
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command description has a list of labels
|
||||
* @param commandLabels List of labels
|
||||
* @return True if all labels match, false otherwise
|
||||
*/
|
||||
public boolean hasLabels(List<String> commandLabels) {
|
||||
// Check if there's a match for every command
|
||||
for(String cmd : commandLabels)
|
||||
if(!hasLabel(cmd))
|
||||
return false;
|
||||
|
||||
// There seems to be a match for every command, return true
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command label is applicable with a command reference. This doesn't check if the parent
|
||||
* are suitable too.
|
||||
*
|
||||
* @param commandReference The command reference.
|
||||
*
|
||||
* @return True if the command reference is suitable to this command label, false otherwise.
|
||||
*/
|
||||
public boolean isSuitableLabel(CommandParts commandReference) {
|
||||
// Make sure the command reference is valid
|
||||
if(commandReference.getCount() <= 0)
|
||||
return false;
|
||||
|
||||
// Get the parent count
|
||||
String element = commandReference.get(getParentCount());
|
||||
|
||||
// Check whether this command description has this command label
|
||||
return hasLabel(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a label is valid to use.
|
||||
*
|
||||
* @param label The label to test.
|
||||
*
|
||||
* @return True if the label is valid to use, false otherwise.
|
||||
*/
|
||||
public static boolean isValidLabel(String label) {
|
||||
// Make sure the label isn't null
|
||||
if(label == null)
|
||||
return false;
|
||||
|
||||
// Trim the label
|
||||
label = label.trim();
|
||||
|
||||
// Make sure the label is at least one character long
|
||||
if(label.length() <= 0)
|
||||
return false;
|
||||
|
||||
// Make sure the label doesn't contain any spaces, return the result
|
||||
return !label.contains(" ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute command label, without a slash.
|
||||
*/
|
||||
public String getAbsoluteLabel() {
|
||||
return getAbsoluteLabel(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute command label.
|
||||
*
|
||||
* @return Absolute command label.
|
||||
*/
|
||||
public String getAbsoluteLabel(boolean includeSlash) {
|
||||
return getAbsoluteLabel(includeSlash, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute command label.
|
||||
*
|
||||
* @return Absolute command label.
|
||||
*/
|
||||
public String getAbsoluteLabel(boolean includeSlash, CommandParts reference) {
|
||||
// Get the command reference, and make sure it is valid
|
||||
CommandParts out = getCommandReference(reference);
|
||||
if(out == null)
|
||||
return "";
|
||||
|
||||
// Return the result
|
||||
return (includeSlash ? "/" : "") + out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command reference.
|
||||
*
|
||||
* @param reference The reference to use as template, which is used to choose the most similar reference.
|
||||
*
|
||||
* @return Command reference.
|
||||
*/
|
||||
public CommandParts getCommandReference(CommandParts reference) {
|
||||
// Build the reference
|
||||
List<String> referenceList = new ArrayList<>();
|
||||
|
||||
// Check whether this command has a parent, if so, add the absolute parent command
|
||||
if(getParent() != null)
|
||||
referenceList.addAll(getParent().getCommandReference(reference).getList());
|
||||
|
||||
// Get the current label
|
||||
referenceList.add(getLabel(reference));
|
||||
|
||||
// Return the reference
|
||||
return new CommandParts(referenceList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the difference between this command and another command reference.
|
||||
*
|
||||
* @param other The other command reference.
|
||||
*
|
||||
* @return The command difference. Zero if there's no difference. A negative number on error.
|
||||
*/
|
||||
public double getCommandDifference(CommandParts other) {
|
||||
return getCommandDifference(other, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the difference between this command and another command reference.
|
||||
*
|
||||
* @param other The other command reference.
|
||||
* @param fullCompare True to fully compare both command references.
|
||||
*
|
||||
* @return The command difference. Zero if there's no difference. A negative number on error.
|
||||
*/
|
||||
public double getCommandDifference(CommandParts other, boolean fullCompare) {
|
||||
// Make sure the reference is valid
|
||||
if(other == null)
|
||||
return -1;
|
||||
|
||||
// Get the command reference
|
||||
CommandParts reference = getCommandReference(other);
|
||||
|
||||
// Compare the two references, return the result
|
||||
return reference.getDifference(new CommandParts(other.getRange(0, reference.getCount())), fullCompare);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the executable command.
|
||||
*
|
||||
* @return The executable command.
|
||||
*/
|
||||
public ExecutableCommand getExecutableCommand() {
|
||||
return this.executableCommand;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the executable command.
|
||||
*
|
||||
* @param executableCommand The executable command.
|
||||
*/
|
||||
public void setExecutableCommand(ExecutableCommand executableCommand) {
|
||||
this.executableCommand = executableCommand;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command is executable, based on the assigned executable command.
|
||||
*
|
||||
* @return True if this command is executable.
|
||||
*/
|
||||
public boolean isExecutable() {
|
||||
return this.executableCommand != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the command, if possible.
|
||||
*
|
||||
* @param sender The command sender that triggered the execution of this command.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
public boolean execute(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Make sure the command is executable
|
||||
if(!isExecutable())
|
||||
return false;
|
||||
|
||||
// Execute the command, return the result
|
||||
return getExecutableCommand().executeCommand(sender, commandReference, commandArguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the parent command if this command description has a parent.
|
||||
*
|
||||
* @return Parent command, or null
|
||||
*/
|
||||
public CommandDescription getParent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of parent this description has.
|
||||
*
|
||||
* @return The number of parents.
|
||||
*/
|
||||
public int getParentCount() {
|
||||
// Check whether the this description has a parent
|
||||
if(!hasParent())
|
||||
return 0;
|
||||
|
||||
// Get the parent count of the parent, return the result
|
||||
return getParent().getParentCount() + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the parent command.
|
||||
*
|
||||
* @param parent Parent command.
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
public boolean setParent(CommandDescription parent) {
|
||||
// Make sure the parent is different
|
||||
if(this.parent == parent)
|
||||
return true;
|
||||
|
||||
// Set the parent
|
||||
this.parent = parent;
|
||||
|
||||
// Make sure the parent isn't null
|
||||
if(parent == null)
|
||||
return true;
|
||||
|
||||
// Add this description as a child to the parent
|
||||
return parent.addChild(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the plugin description has a parent command.
|
||||
*
|
||||
* @return True if the description has a parent command, false otherwise.
|
||||
*/
|
||||
public boolean hasParent() {
|
||||
return this.parent != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all command childs.
|
||||
*
|
||||
* @return Command childs.
|
||||
*/
|
||||
public List<CommandDescription> getChilds() {
|
||||
return this.childs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a child to the command description.
|
||||
*
|
||||
* @param commandDescription The child to add.
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
public boolean addChild(CommandDescription commandDescription) {
|
||||
// Make sure the description is valid
|
||||
if(commandDescription == null)
|
||||
return false;
|
||||
if(!commandDescription.isValid())
|
||||
return false;
|
||||
|
||||
// Make sure the child doesn't exist already
|
||||
if(isChild(commandDescription))
|
||||
return true;
|
||||
|
||||
// The command description to add as a child
|
||||
if(!this.childs.add(commandDescription))
|
||||
return false;
|
||||
|
||||
// Set this description as parent on the child
|
||||
return commandDescription.setParent(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the childs of this command.
|
||||
*
|
||||
* @param childs New command childs. Null to remove all childs.
|
||||
*/
|
||||
public void setChilds(List<CommandDescription> childs) {
|
||||
// Check whether the childs list should be cleared
|
||||
if(childs == null)
|
||||
this.childs.clear();
|
||||
|
||||
else
|
||||
this.childs = childs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command has any child labels.
|
||||
*
|
||||
* @return True if this command has any child labels.
|
||||
*/
|
||||
public boolean hasChilds() {
|
||||
return (this.childs.size() != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this command description has a specific child.
|
||||
*
|
||||
* @param commandDescription The command description to check for.
|
||||
*
|
||||
* @return True if this command description has the specific child, false otherwise.
|
||||
*/
|
||||
public boolean isChild(CommandDescription commandDescription) {
|
||||
// Make sure the description is valid
|
||||
if(commandDescription == null)
|
||||
return false;
|
||||
if(!commandDescription.isValid())
|
||||
return false;
|
||||
|
||||
// Check whether this child exists, return the result
|
||||
return this.childs.contains(commandDescription);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an argument.
|
||||
*
|
||||
* @param argument The argument to add.
|
||||
*
|
||||
* @return True if succeed, false if failed.
|
||||
*/
|
||||
public boolean addArgument(CommandArgumentDescription argument) {
|
||||
// Make sure the argument is valid
|
||||
if(argument == null)
|
||||
return false;
|
||||
|
||||
// Make sure the argument isn't added already
|
||||
if(hasArgument(argument))
|
||||
return true;
|
||||
|
||||
// Add the argument, return the result
|
||||
return this.arguments.add(argument);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all command arguments.
|
||||
*
|
||||
* @return Command arguments.
|
||||
*/
|
||||
public List<CommandArgumentDescription> getArguments() {
|
||||
return this.arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the arguments of this command.
|
||||
*
|
||||
* @param arguments New command arguments. Null to clear the list of arguments.
|
||||
*/
|
||||
public void setArguments(List<CommandArgumentDescription> arguments) {
|
||||
// Convert null into an empty argument list
|
||||
if(arguments == null)
|
||||
this.arguments.clear();
|
||||
|
||||
else
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether an argument exists.
|
||||
*
|
||||
* @param argument The argument to check for.
|
||||
*
|
||||
* @return True if this argument already exists, false otherwise.
|
||||
*/
|
||||
public boolean hasArgument(CommandArgumentDescription argument) {
|
||||
// Make sure the argument is valid
|
||||
if(argument == null)
|
||||
return false;
|
||||
|
||||
// Check whether the argument exists, return the result
|
||||
return this.arguments.contains(argument);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command has any arguments.
|
||||
*
|
||||
* @return True if this command has any arguments.
|
||||
*/
|
||||
public boolean hasArguments() {
|
||||
return (this.arguments.size() != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* The minimum number of arguments required for this command.
|
||||
*
|
||||
* @return The minimum number of required arguments.
|
||||
*/
|
||||
public int getMinimumArguments() {
|
||||
// Get the number of required and optional arguments
|
||||
int requiredArguments = 0;
|
||||
int optionalArgument = 0;
|
||||
|
||||
// Loop through each argument
|
||||
for(CommandArgumentDescription argument : this.arguments) {
|
||||
// Check whether the command is optional
|
||||
if(!argument.isOptional()) {
|
||||
requiredArguments += optionalArgument + 1;
|
||||
optionalArgument = 0;
|
||||
|
||||
} else
|
||||
optionalArgument++;
|
||||
}
|
||||
|
||||
// Return the number of required arguments
|
||||
return requiredArguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum number of arguments.
|
||||
*
|
||||
* @return The maximum number of arguments. A negative number will be returned if there's no maximum.
|
||||
*/
|
||||
public int getMaximumArguments() {
|
||||
// Check whether there is a maximum set
|
||||
if(this.noArgumentMaximum)
|
||||
return -1;
|
||||
|
||||
// Return the maximum based on the registered arguments
|
||||
return this.arguments.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether there is an argument maximum.
|
||||
*
|
||||
* @param maximumArguments True if there is an argument maximum, based on the number of registered arguments.
|
||||
*/
|
||||
public void setMaximumArguments(boolean maximumArguments) {
|
||||
this.noArgumentMaximum = !maximumArguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command description.
|
||||
*
|
||||
* @return Command description.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return hasDescription() ? this.description : this.detailedDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command description.
|
||||
*
|
||||
* @param description New command description. Null to reset the description.
|
||||
*/
|
||||
public void setDescription(String description) {
|
||||
if(description == null)
|
||||
this.description = "";
|
||||
|
||||
else
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command has any description.
|
||||
*
|
||||
* @return True if this command has any description.
|
||||
*/
|
||||
public boolean hasDescription() {
|
||||
return (this.description.trim().length() != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command detailed description.
|
||||
*
|
||||
* @return Command detailed description.
|
||||
*/
|
||||
public String getDetailedDescription() {
|
||||
return hasDetailedDescription() ? this.detailedDescription : this.description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command detailed description.
|
||||
*
|
||||
* @param detailedDescription New command description. Null to reset the description.
|
||||
*/
|
||||
public void setDetailedDescription(String detailedDescription) {
|
||||
if(detailedDescription == null)
|
||||
this.detailedDescription = "";
|
||||
|
||||
else
|
||||
this.detailedDescription = detailedDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command has any detailed description.
|
||||
*
|
||||
* @return True if this command has any detailed description.
|
||||
*/
|
||||
public boolean hasDetailedDescription() {
|
||||
return (this.detailedDescription.trim().length() != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the best suitable command for a query reference.
|
||||
*
|
||||
* @param queryReference The query reference to find a command for.
|
||||
*
|
||||
* @return The command found, or null.
|
||||
*/
|
||||
public FoundCommandResult findCommand(final CommandParts queryReference) {
|
||||
// Make sure the command reference is valid
|
||||
if(queryReference.getCount() <= 0)
|
||||
return null;
|
||||
|
||||
// Check whether this description is for the last element in the command reference, if so return the current command
|
||||
if(queryReference.getCount() <= getParentCount() + 1)
|
||||
return new FoundCommandResult(
|
||||
this,
|
||||
getCommandReference(queryReference),
|
||||
new CommandParts(),
|
||||
queryReference);
|
||||
|
||||
// Get the new command reference and arguments
|
||||
CommandParts newReference = new CommandParts(queryReference.getRange(0, getParentCount() + 1));
|
||||
CommandParts newArguments = new CommandParts(queryReference.getRange(getParentCount() + 1));
|
||||
|
||||
// Handle the child's, if this command has any
|
||||
if(getChilds().size() > 0) {
|
||||
// Get a new instance of the child's list, and sort them by their difference in comparison to the query reference
|
||||
List<CommandDescription> commandChilds = new ArrayList<>(getChilds());
|
||||
Collections.sort(commandChilds, (o1, o2) -> Double.compare(
|
||||
o1.getCommandDifference(queryReference),
|
||||
o2.getCommandDifference(queryReference)));
|
||||
|
||||
// Get the difference of the first child in the list
|
||||
double firstChildDifference = commandChilds.get(0).getCommandDifference(queryReference, true);
|
||||
|
||||
// Check if the reference perfectly suits the arguments of the current command if it doesn't perfectly suits a child command
|
||||
if(firstChildDifference > 0.0)
|
||||
if(getSuitableArgumentsDifference(queryReference) == 0)
|
||||
return new FoundCommandResult(this, newReference, newArguments, queryReference);
|
||||
|
||||
// Loop through each child
|
||||
for(CommandDescription child : commandChilds) {
|
||||
// Get the best suitable command
|
||||
FoundCommandResult result = child.findCommand(queryReference);
|
||||
if(result != null)
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the remaining command reference elements fit the arguments for this command
|
||||
if(getSuitableArgumentsDifference(queryReference) >= 0)
|
||||
return new FoundCommandResult(this, newReference, newArguments, queryReference);
|
||||
|
||||
// No command found, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether there's any command description that matches the specified command reference.
|
||||
*
|
||||
* @param commandReference The command reference.
|
||||
*
|
||||
* @return True if so, false otherwise.
|
||||
*/
|
||||
public boolean hasSuitableCommand(CommandParts commandReference) {
|
||||
return findCommand(commandReference) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the remaining command reference elements are suitable with arguments of the current command description.
|
||||
*
|
||||
* @param commandReference The command reference.
|
||||
*
|
||||
* @return True if the arguments are suitable, false otherwise.
|
||||
*/
|
||||
public boolean hasSuitableArguments(CommandParts commandReference) {
|
||||
return getSuitableArgumentsDifference(commandReference) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the remaining command reference elements are suitable with arguments of the current command description,
|
||||
* and get the difference in argument count.
|
||||
*
|
||||
* @param commandReference The command reference.
|
||||
*
|
||||
* @return The difference in argument count between the reference and the actual command.
|
||||
*/
|
||||
public int getSuitableArgumentsDifference(CommandParts commandReference) {
|
||||
// Make sure the command reference is valid
|
||||
if(commandReference.getCount() <= 0)
|
||||
return -1;
|
||||
|
||||
// Get the remaining command reference element count
|
||||
int remainingElementCount = commandReference.getCount() - getParentCount() - 1;
|
||||
|
||||
// Check if there are too less arguments
|
||||
if(getMinimumArguments() > remainingElementCount)
|
||||
return Math.abs(getMinimumArguments() - remainingElementCount);
|
||||
|
||||
// Check if there are too many arguments
|
||||
if(getMaximumArguments() < remainingElementCount && getMaximumArguments() >= 0)
|
||||
return Math.abs(remainingElementCount - getMaximumArguments());
|
||||
|
||||
// The arguments seem to be EQUALS, return the result
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command permissions.
|
||||
*
|
||||
* @return The command permissions.
|
||||
*/
|
||||
public CommandPermissions getCommandPermissions() {
|
||||
return this.permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command permissions.
|
||||
*
|
||||
* @param commandPermissions The command permissions.
|
||||
*/
|
||||
public void setCommandPermissions(CommandPermissions commandPermissions) {
|
||||
this.permissions = commandPermissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command permissions.
|
||||
*
|
||||
* @param permissionNode The permission node required.
|
||||
* @param defaultPermission The default permission.
|
||||
*/
|
||||
public void setCommandPermissions(String permissionNode, CommandPermissions.DefaultPermission defaultPermission) {
|
||||
this.permissions = new CommandPermissions(permissionNode, defaultPermission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether two labels equal to each other.
|
||||
*
|
||||
* @param commandLabel The first command label.
|
||||
* @param otherCommandLabel The other command label.
|
||||
*
|
||||
* @return True if the labels are equal to each other.
|
||||
*/
|
||||
private static boolean commandLabelEquals(String commandLabel, String otherCommandLabel) {
|
||||
// Trim the command labels from unwanted whitespaces
|
||||
commandLabel = commandLabel.trim();
|
||||
otherCommandLabel = otherCommandLabel.trim();
|
||||
|
||||
// Check whether the the two command labels are equal (case insensitive)
|
||||
return (commandLabel.equalsIgnoreCase(otherCommandLabel));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the command description has been set up properly.
|
||||
*
|
||||
* @return True if the command description is valid, false otherwise.
|
||||
*/
|
||||
public boolean isValid() {
|
||||
// Make sure any command label is set
|
||||
if(getLabels().size() == 0)
|
||||
return false;
|
||||
|
||||
// Make sure the permissions are set up properly
|
||||
if(this.permissions == null)
|
||||
return false;
|
||||
|
||||
// Everything seems to be correct, return the result
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,205 @@
|
||||
package fr.xephi.authme.commands.dynamic;
|
||||
|
||||
import com.timvisee.dungeonmaze.command.help.HelpProvider;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandHandler {
|
||||
|
||||
/** The command manager instance. */
|
||||
private CommandManager commandManager;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param init True to immediately initialize.
|
||||
*/
|
||||
public CommandHandler(boolean init) {
|
||||
// Initialize
|
||||
if(init)
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the command handler.
|
||||
*
|
||||
* @return True if succeed, false on failure. True will also be returned if the command handler was already
|
||||
* initialized.
|
||||
*/
|
||||
public boolean init() {
|
||||
// Make sure the handler isn't initialized already
|
||||
if(isInit())
|
||||
return true;
|
||||
|
||||
// Initialize the command manager
|
||||
this.commandManager = new CommandManager(false);
|
||||
this.commandManager.registerCommands();
|
||||
|
||||
// Return the result
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the command handler is initialized.
|
||||
*
|
||||
* @return True if the command handler is initialized.
|
||||
*/
|
||||
public boolean isInit() {
|
||||
return this.commandManager != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the command handler.
|
||||
*
|
||||
* @return True if the command handler was destroyed successfully, false otherwise. True will also be returned if
|
||||
* the command handler wasn't initialized.
|
||||
*/
|
||||
public boolean destroy() {
|
||||
// Make sure the command handler is initialized
|
||||
if(!isInit())
|
||||
return true;
|
||||
|
||||
// Unset the command manager
|
||||
this.commandManager = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command manager.
|
||||
*
|
||||
* @return Command manager instance.
|
||||
*/
|
||||
public CommandManager getCommandManager() {
|
||||
return this.commandManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a command.
|
||||
*
|
||||
* @param sender The command sender (Bukkit).
|
||||
* @param bukkitCommand The command (Bukkit).
|
||||
* @param bukkitCommandLabel The command label (Bukkit).
|
||||
* @param bukkitArgs The command arguments (Bukkit).
|
||||
*
|
||||
* @return True if the command was executed, false otherwise.
|
||||
*/
|
||||
public boolean onCommand(CommandSender sender, @SuppressWarnings("UnusedParameters") org.bukkit.command.Command bukkitCommand, String bukkitCommandLabel, String[] bukkitArgs) {
|
||||
// Process the arguments
|
||||
List<String> args = processArguments(bukkitArgs);
|
||||
|
||||
// Create a command reference, and make sure at least one command part is available
|
||||
CommandParts commandReference = new CommandParts(bukkitCommandLabel, args);
|
||||
if(commandReference.getCount() == 0)
|
||||
return false;
|
||||
|
||||
// Get a suitable command for this reference, and make sure it isn't null
|
||||
FoundCommandResult result = this.commandManager.findCommand(commandReference);
|
||||
if(result == null) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to parse Dungeon Maze command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the base command
|
||||
String baseCommand = commandReference.get(0);
|
||||
|
||||
// Make sure the difference between the command reference and the actual command isn't too big
|
||||
final double commandDifference = result.getDifference();
|
||||
if(commandDifference > 0.12) {
|
||||
// Show the unknown command warning
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Unknown command!");
|
||||
|
||||
// Show a command suggestion if available and the difference isn't too big
|
||||
if(commandDifference < 0.75)
|
||||
if(result.getCommandDescription() != null)
|
||||
sender.sendMessage(ChatColor.YELLOW + "Did you mean " + ChatColor.GOLD + "/" + result.getCommandDescription().getCommandReference(commandReference) + ChatColor.YELLOW + "?");
|
||||
|
||||
// Show the help command
|
||||
sender.sendMessage(ChatColor.YELLOW + "Use the command " + ChatColor.GOLD + "/" + baseCommand + " help" + ChatColor.YELLOW + " to view help.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Show a message when the command handler is assuming a command
|
||||
if(commandDifference > 0) {
|
||||
// Get the suggested command
|
||||
CommandParts suggestedCommandParts = new CommandParts(result.getCommandDescription().getCommandReference(commandReference));
|
||||
|
||||
// Show the suggested command
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Unknown command, assuming " + ChatColor.GOLD + "/" + suggestedCommandParts +
|
||||
ChatColor.DARK_RED + "!");
|
||||
}
|
||||
|
||||
// Make sure the command is executable
|
||||
if(!result.isExecutable()) {
|
||||
// Get the command reference
|
||||
CommandParts helpCommandReference = new CommandParts(result.getCommandReference().getRange(1));
|
||||
|
||||
// Show the unknown command warning
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Invalid command!");
|
||||
|
||||
// Show the help command
|
||||
sender.sendMessage(ChatColor.YELLOW + "Use the command " + ChatColor.GOLD + "/" + baseCommand + " help " + helpCommandReference + ChatColor.YELLOW + " to view help.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the command sender has permission
|
||||
if(!result.hasPermission(sender)) {
|
||||
// Show the no permissions warning
|
||||
sender.sendMessage(ChatColor.DARK_RED + "You don't have permission to use this command!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the command sender has permission
|
||||
if(!result.hasProperArguments()) {
|
||||
// Get the command and the suggested command reference
|
||||
CommandParts suggestedCommandReference = new CommandParts(result.getCommandDescription().getCommandReference(commandReference));
|
||||
CommandParts helpCommandReference = new CommandParts(suggestedCommandReference.getRange(1));
|
||||
|
||||
// Show the invalid arguments warning
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Incorrect command arguments!");
|
||||
|
||||
// Show the command argument help
|
||||
HelpProvider.showHelp(sender, commandReference, suggestedCommandReference, true, false, true, false, false, false);
|
||||
|
||||
// Show the command to use for detailed help
|
||||
sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/" + baseCommand + " help " + helpCommandReference);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Execute the command if it's suitable
|
||||
return result.executeCommand(sender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the command arguments, and return them as an array list.
|
||||
*
|
||||
* @param args The command arguments to process.
|
||||
*
|
||||
* @return The processed command arguments.
|
||||
*/
|
||||
private List<String> processArguments(String[] args) {
|
||||
// Convert the array into a list of arguments
|
||||
List<String> arguments = new ArrayList<>(Arrays.asList(args));
|
||||
|
||||
/// Remove all empty arguments
|
||||
for(int i = 0; i < arguments.size(); i++) {
|
||||
// Get the argument value
|
||||
final String arg = arguments.get(i);
|
||||
|
||||
// Check whether the argument value is empty
|
||||
if(arg.trim().length() == 0) {
|
||||
// Remove the current argument
|
||||
arguments.remove(i);
|
||||
|
||||
// Decrease the index by one, continue to the next argument
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the argument
|
||||
return arguments;
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package fr.xephi.authme.commands.dynamic;
|
||||
|
||||
import com.timvisee.dungeonmaze.service.Service;
|
||||
|
||||
public class CommandHandlerService extends Service {
|
||||
|
||||
/** Service name. */
|
||||
private static final String SERVICE_NAME = "Command Handler";
|
||||
|
||||
/** Command handler instance. */
|
||||
private CommandHandler commandHandler;
|
||||
|
||||
/**
|
||||
* Initialize the service.
|
||||
*
|
||||
* @return True on success, false on failure. True will also be returned if the service was initialized already.
|
||||
*/
|
||||
@Override
|
||||
public boolean init() {
|
||||
// Initialize the command handler
|
||||
this.commandHandler = new CommandHandler(false);
|
||||
|
||||
// Initialize the command handler, return the result
|
||||
return this.commandHandler.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the service is initialized.
|
||||
*
|
||||
* @return True if the service is initialized, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean isInit() {
|
||||
// Make sure the command handler is set
|
||||
if(this.commandHandler == null)
|
||||
return false;
|
||||
|
||||
// Check whether the command handler is initialized
|
||||
return this.commandHandler.isInit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the service. The destruction won't be forced.
|
||||
*
|
||||
* @param force True to force the destruction. This wil re-destroy the service even if it isn't initialized.
|
||||
* This will also force the initialization state to be set to false even if an error occurred while
|
||||
* destroying.
|
||||
*
|
||||
* @return True on success, false on failure. True will also be returned if the service wasn't initialized. False
|
||||
* might be returned if force is set to true, even though the initialization state is set to false.
|
||||
*/
|
||||
@Override
|
||||
public boolean destroy(boolean force) {
|
||||
// Make sure the command handler is initialized
|
||||
if(!this.isInit() && !force)
|
||||
return true;
|
||||
|
||||
// Destroy the command handler
|
||||
if(this.commandHandler != null) {
|
||||
if(!this.commandHandler.destroy()) {
|
||||
if(force)
|
||||
this.commandHandler = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the result
|
||||
this.commandHandler = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the service.
|
||||
*
|
||||
* @return Service name.
|
||||
*/
|
||||
@Override
|
||||
public String getName() {
|
||||
return SERVICE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command handler.
|
||||
*
|
||||
* @return Command handler instance.
|
||||
*/
|
||||
public CommandHandler getCommandHandler() {
|
||||
return this.commandHandler;
|
||||
}
|
||||
}
|
@ -0,0 +1,288 @@
|
||||
package fr.xephi.authme.commands.dynamic;
|
||||
|
||||
import com.timvisee.dungeonmaze.command.executable.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public class CommandManager {
|
||||
|
||||
/** The list of commandDescriptions. */
|
||||
private List<CommandDescription> commandDescriptions = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param registerCommands True to register the commands, false otherwise.
|
||||
*/
|
||||
public CommandManager(boolean registerCommands) {
|
||||
// Register the commands
|
||||
if(registerCommands)
|
||||
registerCommands();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all commands.
|
||||
*/
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
public void registerCommands() {
|
||||
// Register the base Dungeon Maze command
|
||||
CommandDescription dungeonMazeCommand = new CommandDescription(
|
||||
new DungeonMazeCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("dungeonmaze");
|
||||
add("dm");
|
||||
}},
|
||||
"Main command",
|
||||
"The main Dungeon Maze command. The root for all the other commands.", null);
|
||||
|
||||
// Register the help command
|
||||
CommandDescription helpCommand = new CommandDescription(
|
||||
new HelpCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("help");
|
||||
add("hlp");
|
||||
add("h");
|
||||
add("sos");
|
||||
add("?");
|
||||
}},
|
||||
"View help",
|
||||
"View detailed help pages about Dungeon Maze commands.",
|
||||
dungeonMazeCommand);
|
||||
helpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
|
||||
helpCommand.setMaximumArguments(false);
|
||||
|
||||
// Register the create command
|
||||
CommandDescription createWorldCommand = new CommandDescription(
|
||||
new CreateWorldCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("createworld");
|
||||
add("cw");
|
||||
}},
|
||||
"Create world",
|
||||
"Create a new Dungeon Maze world, the name of the world must be unique.",
|
||||
dungeonMazeCommand);
|
||||
createWorldCommand.addArgument(new CommandArgumentDescription("world", "The name of the world to create.", false));
|
||||
createWorldCommand.addArgument(new CommandArgumentDescription("preload", "True or False to preload the world on startup.", true));
|
||||
createWorldCommand.setCommandPermissions("dungeonmaze.command.createworld", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the teleport command
|
||||
CommandDescription teleportCommand = new CommandDescription(
|
||||
new TeleportCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("teleport");
|
||||
add("tp");
|
||||
add("warp");
|
||||
add("goto");
|
||||
add("move");
|
||||
}},
|
||||
"Teleport to world",
|
||||
"Teleports to any another world, such as a Dungeon Maze world." ,
|
||||
dungeonMazeCommand);
|
||||
teleportCommand.addArgument(new CommandArgumentDescription("world", "The name of the world to teleport to.", false));
|
||||
teleportCommand.setCommandPermissions("dungeonmaze.command.teleport", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the load world command
|
||||
CommandDescription loadWorldCommand = new CommandDescription(
|
||||
new LoadWorldCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("loadworld");
|
||||
add("load");
|
||||
}},
|
||||
"Load a world",
|
||||
"Load a world if it isn't loaded." ,
|
||||
dungeonMazeCommand);
|
||||
loadWorldCommand.addArgument(new CommandArgumentDescription("world", "The name of the world to load.", false));
|
||||
loadWorldCommand.setCommandPermissions("dungeonmaze.command.loadworld", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the unload world command
|
||||
CommandDescription unloadWorldCommand = new CommandDescription(
|
||||
new UnloadWorldCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("unloadworld");
|
||||
add("unload");
|
||||
}},
|
||||
"Unload a world",
|
||||
"Unload a loaded world." ,
|
||||
dungeonMazeCommand);
|
||||
unloadWorldCommand.addArgument(new CommandArgumentDescription("world", "The name of the world to unload.", false));
|
||||
unloadWorldCommand.setCommandPermissions("dungeonmaze.command.unloadworld", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the list world command
|
||||
CommandDescription listWorldCommand = new CommandDescription(
|
||||
new ListWorldCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("listworlds");
|
||||
add("listworld");
|
||||
add("list");
|
||||
add("worlds");
|
||||
add("lw");
|
||||
}},
|
||||
"List Dungeon Mazes",
|
||||
"Lists the available Dungeon Maze worlds and shows some additional information.",
|
||||
dungeonMazeCommand);
|
||||
listWorldCommand.setCommandPermissions("dungeonmaze.command.listworlds", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the reload command
|
||||
CommandDescription reloadCommand = new CommandDescription(
|
||||
new ReloadCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("reload");
|
||||
add("rld");
|
||||
add("r");
|
||||
}},
|
||||
"Reload Dungeon Maze",
|
||||
"Reload the Dungeon Maze plugin.",
|
||||
dungeonMazeCommand);
|
||||
reloadCommand.setCommandPermissions("dungeonmaze.command.reload", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
reloadCommand.addArgument(new CommandArgumentDescription("force", "True or False to force reload.", true));
|
||||
|
||||
// Register the reload permissions command
|
||||
CommandDescription reloadPermissionsCommand = new CommandDescription(
|
||||
new ReloadPermissionsCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("reloadpermissions");
|
||||
add("reloadpermission");
|
||||
add("reloadperms");
|
||||
add("rp");
|
||||
}},
|
||||
"Reload permissions",
|
||||
"Reload the permissions system and rehook the installed permissions system.",
|
||||
dungeonMazeCommand);
|
||||
reloadPermissionsCommand.setCommandPermissions("dungeonmaze.command.reloadpermissions", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the restart command
|
||||
CommandDescription restartCommand = new CommandDescription(
|
||||
new RestartCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("restart");
|
||||
add("rstrt");
|
||||
}},
|
||||
"Restart Dungeon Maze",
|
||||
"Restart the Dungeon Maze plugin.",
|
||||
dungeonMazeCommand);
|
||||
restartCommand.setCommandPermissions("dungeonmaze.command.restart", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
restartCommand.addArgument(new CommandArgumentDescription("force", "True or False to force restart.", true));
|
||||
|
||||
// Register the check updates command
|
||||
CommandDescription checkUpdatesCommand = new CommandDescription(
|
||||
new CheckUpdatesCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("checkupdates");
|
||||
add("checkupdate");
|
||||
add("check");
|
||||
add("updates");
|
||||
add("update");
|
||||
add("cu");
|
||||
}},
|
||||
"Check updates",
|
||||
"Check for available updates to install.",
|
||||
dungeonMazeCommand);
|
||||
checkUpdatesCommand.setCommandPermissions("dungeonmaze.command.checkupdates", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the install update command
|
||||
CommandDescription installUpdateCommand = new CommandDescription(
|
||||
new InstallUpdateCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("installupdates");
|
||||
add("installupdate");
|
||||
add("install");
|
||||
add("iu");
|
||||
}},
|
||||
"Install update",
|
||||
"Try to install any availble update.",
|
||||
dungeonMazeCommand);
|
||||
installUpdateCommand.setCommandPermissions("dungeonmaze.command.installupdate", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the status command
|
||||
CommandDescription statusCommand = new CommandDescription(
|
||||
new StatusCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("status");
|
||||
add("stats");
|
||||
add("s");
|
||||
}},
|
||||
"Status info",
|
||||
"Show detailed plugin status.",
|
||||
dungeonMazeCommand);
|
||||
statusCommand.setMaximumArguments(false);
|
||||
statusCommand.setCommandPermissions("dungeonmaze.command.status", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the status command
|
||||
CommandDescription serviceCommand = new CommandDescription(
|
||||
new ServiceCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("services");
|
||||
add("service");
|
||||
add("serv");
|
||||
}},
|
||||
"Services command",
|
||||
"Show detailed information about all the Dungeon Maze serivces.",
|
||||
dungeonMazeCommand);
|
||||
serviceCommand.setMaximumArguments(false);
|
||||
serviceCommand.setCommandPermissions("dungeonmaze.command.services", CommandPermissions.DefaultPermission.OP_ONLY);
|
||||
|
||||
// Register the version command
|
||||
CommandDescription versionCommand = new CommandDescription(
|
||||
new VersionCommand(),
|
||||
new ArrayList<String>() {{
|
||||
add("version");
|
||||
add("ver");
|
||||
add("v");
|
||||
add("about");
|
||||
add("info");
|
||||
}},
|
||||
"Version info",
|
||||
"Show detailed information about the installed Dungeon Maze version, and shows the developers, contributors, license and other information.",
|
||||
dungeonMazeCommand);
|
||||
versionCommand.setMaximumArguments(false);
|
||||
|
||||
// Add the base command to the commands array
|
||||
this.commandDescriptions.add(dungeonMazeCommand);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of command descriptions
|
||||
*
|
||||
* @return List of command descriptions.
|
||||
*/
|
||||
public List<CommandDescription> getCommandDescriptions() {
|
||||
return this.commandDescriptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of command description count.
|
||||
*
|
||||
* @return Command description count.
|
||||
*/
|
||||
public int getCommandDescriptionCount() {
|
||||
return this.getCommandDescriptions().size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the best suitable command for the specified reference.
|
||||
*
|
||||
* @param queryReference The query reference to find a command for.
|
||||
*
|
||||
* @return The command found, or null.
|
||||
*/
|
||||
public FoundCommandResult findCommand(CommandParts queryReference) {
|
||||
// Make sure the command reference is valid
|
||||
if(queryReference.getCount() <= 0)
|
||||
return null;
|
||||
|
||||
// Get the base command description
|
||||
for(CommandDescription commandDescription : this.commandDescriptions) {
|
||||
// Check whether there's a command description available for the current command
|
||||
if(!commandDescription.isSuitableLabel(queryReference))
|
||||
continue;
|
||||
|
||||
// Find the command reference, return the result
|
||||
return commandDescription.findCommand(queryReference);
|
||||
}
|
||||
|
||||
// No applicable command description found, return false
|
||||
return null;
|
||||
}
|
||||
}
|
204
src/main/java/fr/xephi/authme/commands/dynamic/CommandParts.java
Normal file
204
src/main/java/fr/xephi/authme/commands/dynamic/CommandParts.java
Normal file
@ -0,0 +1,204 @@
|
||||
package fr.xephi.authme.commands.dynamic;
|
||||
|
||||
import com.timvisee.dungeonmaze.util.ListUtils;
|
||||
import com.timvisee.dungeonmaze.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandParts {
|
||||
|
||||
/** The list of parts for this command. */
|
||||
private List<String> parts = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public CommandParts() { }
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param part The part to add.
|
||||
*/
|
||||
public CommandParts(String part) {
|
||||
this.parts.add(part);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param commandParts The command parts instance.
|
||||
*/
|
||||
public CommandParts(CommandParts commandParts) {
|
||||
this.parts.addAll(commandParts.getList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param parts The list of parts.
|
||||
*/
|
||||
public CommandParts(List<String> parts) {
|
||||
this.parts.addAll(parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param base The base part.
|
||||
* @param parts The list of additional parts.
|
||||
*/
|
||||
public CommandParts(String base, List<String> parts) {
|
||||
this.parts.add(base);
|
||||
this.parts.addAll(parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command parts.
|
||||
*
|
||||
* @return Command parts.
|
||||
*/
|
||||
public List<String> getList() {
|
||||
return this.parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a part.
|
||||
*
|
||||
* @param part The part to add.
|
||||
*
|
||||
* @return The result.
|
||||
*/
|
||||
public boolean add(String part) {
|
||||
return this.parts.add(part);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add some parts.
|
||||
*
|
||||
* @param parts The parts to add.
|
||||
*
|
||||
* @return The result.
|
||||
*/
|
||||
public boolean add(List<String> parts) {
|
||||
return this.parts.addAll(parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add some parts.
|
||||
*
|
||||
* @param parts The parts to add.
|
||||
*
|
||||
* @return The result.
|
||||
*/
|
||||
public boolean add(String[] parts) {
|
||||
for(String entry : parts)
|
||||
add(entry);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of parts.
|
||||
*
|
||||
* @return Part count.
|
||||
*/
|
||||
public int getCount() {
|
||||
return this.parts.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a part by it's index.
|
||||
*
|
||||
* @param i Part index.
|
||||
*
|
||||
* @return The part.
|
||||
*/
|
||||
public String get(int i) {
|
||||
// Make sure the index is in-bound
|
||||
if(i < 0 || i >= getCount())
|
||||
return null;
|
||||
|
||||
// Get and return the argument
|
||||
return this.parts.get(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a range of the parts starting at the specified index up to the end of the range.
|
||||
*
|
||||
* @param start The starting index.
|
||||
*
|
||||
* @return The parts range. Arguments that were out of bound are not included.
|
||||
*/
|
||||
public List<String> getRange(int start) {
|
||||
return getRange(start, getCount() - start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a range of the parts.
|
||||
*
|
||||
* @param start The starting index.
|
||||
* @param count The number of parts to get.
|
||||
*
|
||||
* @return The parts range. Parts that were out of bound are not included.
|
||||
*/
|
||||
public List<String> getRange(int start, int count) {
|
||||
// Create a new list to put the range into
|
||||
List<String> elements = new ArrayList<>();
|
||||
|
||||
// Get the range
|
||||
for(int i = start; i < start + count; i++) {
|
||||
// Get the part and add it if it's valid
|
||||
String element = get(i);
|
||||
if(element != null)
|
||||
elements.add(element);
|
||||
}
|
||||
|
||||
// Return the list of parts
|
||||
return elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the difference value between two references. This won't do a full compare, just the last reference parts instead.
|
||||
*
|
||||
* @param other The other reference.
|
||||
*
|
||||
* @return The result from zero to above. A negative number will be returned on error.
|
||||
*/
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public double getDifference(CommandParts other) {
|
||||
return getDifference(other, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the difference value between two references.
|
||||
*
|
||||
* @param other The other reference.
|
||||
* @param fullCompare True to compare the full references as far as the range reaches.
|
||||
*
|
||||
* @return The result from zero to above. A negative number will be returned on error.
|
||||
*/
|
||||
public double getDifference(CommandParts other, boolean fullCompare) {
|
||||
// Make sure the other reference is correct
|
||||
if(other == null)
|
||||
return -1;
|
||||
|
||||
// Get the range to use
|
||||
int range = Math.min(this.getCount(), other.getCount());
|
||||
|
||||
// Get and the difference
|
||||
if(fullCompare)
|
||||
return StringUtils.getDifference(this.toString(), other.toString());
|
||||
return StringUtils.getDifference(this.getRange(range - 1, 1).toString(), other.getRange(range - 1, 1).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the parts to a string.
|
||||
*
|
||||
* @return The part as a string.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return ListUtils.implode(this.parts, " ");
|
||||
}
|
||||
}
|
@ -0,0 +1,182 @@
|
||||
package fr.xephi.authme.commands.dynamic;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.permission.PermissionsManager;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public class CommandPermissions {
|
||||
|
||||
/** Defines the permission nodes required to have permission to execute this command. */
|
||||
private List<String> permissionNodes = new ArrayList<>();
|
||||
/** Defines the default permission if the permission nodes couldn't be used. */
|
||||
private DefaultPermission defaultPermission = DefaultPermission.NOT_ALLOWED;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public CommandPermissions() { }
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param permissionNode The permission node required to execute a command.
|
||||
* @param defaultPermission The default permission if the permission nodes couldn't be used.
|
||||
*/
|
||||
public CommandPermissions(String permissionNode, DefaultPermission defaultPermission) {
|
||||
this.permissionNodes.add(permissionNode);
|
||||
this.defaultPermission = defaultPermission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param permissionNodes The permission nodes required to execute a command.
|
||||
* @param defaultPermission The default permission if the permission nodes couldn't be used.
|
||||
*/
|
||||
public CommandPermissions(List<String> permissionNodes, DefaultPermission defaultPermission) {
|
||||
this.permissionNodes.addAll(permissionNodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a permission node required to execute this command.
|
||||
*
|
||||
* @param permissionNode The permission node to add.
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
public boolean addPermissionNode(String permissionNode) {
|
||||
// Trim the permission node
|
||||
permissionNode = permissionNode.trim();
|
||||
|
||||
// Make sure the permission node is valid
|
||||
if(permissionNode.length() == 0)
|
||||
return false;
|
||||
|
||||
// Make sure this permission node hasn't been added already
|
||||
if(hasPermissionNode(permissionNode))
|
||||
return true;
|
||||
|
||||
// Add the permission node, return the result
|
||||
return this.permissionNodes.add(permissionNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command requires a specified permission node to execute.
|
||||
*
|
||||
* @param permissionNode The permission node to check for.
|
||||
*
|
||||
* @return True if this permission node is required, false if not.
|
||||
*/
|
||||
public boolean hasPermissionNode(String permissionNode) {
|
||||
return this.permissionNodes.contains(permissionNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the permission nodes required to execute this command.
|
||||
*
|
||||
* @return The permission nodes required to execute this command.
|
||||
*/
|
||||
public List<String> getPermissionNodes() {
|
||||
return this.permissionNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of permission nodes set.
|
||||
*
|
||||
* @return Permission node count.
|
||||
*/
|
||||
public int getPermissionNodeCount() {
|
||||
return this.permissionNodes.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the permission nodes required to execute this command.
|
||||
*
|
||||
* @param permissionNodes The permission nodes required to execute this command.
|
||||
*/
|
||||
public void setPermissionNodes(List<String> permissionNodes) {
|
||||
this.permissionNodes = permissionNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command requires any permission to be executed. This is based on the getPermission() method.
|
||||
*
|
||||
* @return True if this command requires any permission to be executed by a player.
|
||||
*/
|
||||
public boolean hasPermission(CommandSender sender) {
|
||||
// Make sure any permission node is set
|
||||
if(getPermissionNodeCount() == 0)
|
||||
return true;
|
||||
|
||||
// Get the default permission
|
||||
final boolean defaultPermission = getDefaultPermissionCommandSender(sender);
|
||||
|
||||
// Make sure the command sender is a player, if not use the default
|
||||
if(!(sender instanceof Player))
|
||||
return defaultPermission;
|
||||
|
||||
// Get the player instance
|
||||
Player player = (Player) sender;
|
||||
|
||||
// Get the permissions manager, and make sure it's instance is valid
|
||||
PermissionsManager permissionsManager = Core.getPermissionsManager();
|
||||
if(permissionsManager == null)
|
||||
return false;
|
||||
|
||||
// Check whether the player has permission, return the result
|
||||
for(String node : this.permissionNodes)
|
||||
if(!permissionsManager.hasPermission(player, node, defaultPermission))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default permission if the permission nodes couldn't be used.
|
||||
*
|
||||
* @return The default permission.
|
||||
*/
|
||||
public DefaultPermission getDefaultPermission() {
|
||||
return this.defaultPermission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default permission used if the permission nodes couldn't be used.
|
||||
*
|
||||
* @param defaultPermission The default permission.
|
||||
*/
|
||||
public void setDefaultPermission(DefaultPermission defaultPermission) {
|
||||
this.defaultPermission = defaultPermission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default permission for a specified command sender.
|
||||
*
|
||||
* @param sender The command sender to get the default permission for.
|
||||
*
|
||||
* @return True if the command sender has permission by default, false otherwise.
|
||||
*/
|
||||
public boolean getDefaultPermissionCommandSender(CommandSender sender) {
|
||||
switch(getDefaultPermission()) {
|
||||
case ALLOWED:
|
||||
return true;
|
||||
|
||||
case OP_ONLY:
|
||||
return sender.isOp();
|
||||
|
||||
case NOT_ALLOWED:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public enum DefaultPermission {
|
||||
NOT_ALLOWED,
|
||||
OP_ONLY,
|
||||
ALLOWED
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package fr.xephi.authme.commands.dynamic;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public abstract class ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
public abstract boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments);
|
||||
}
|
@ -0,0 +1,151 @@
|
||||
package fr.xephi.authme.commands.dynamic;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class FoundCommandResult {
|
||||
|
||||
/** The command description instance. */
|
||||
private CommandDescription commandDescription;
|
||||
/** The command reference. */
|
||||
private CommandParts commandReference;
|
||||
/** The command arguments. */
|
||||
private CommandParts commandArguments;
|
||||
/** The original search query reference. */
|
||||
private CommandParts queryReference;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param commandDescription The command description.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
* @param queryReference The original query reference.
|
||||
*/
|
||||
public FoundCommandResult(CommandDescription commandDescription, CommandParts commandReference, CommandParts commandArguments, CommandParts queryReference) {
|
||||
this.commandDescription = commandDescription;
|
||||
this.commandReference = commandReference;
|
||||
this.commandArguments = commandArguments;
|
||||
this.queryReference = queryReference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the command was suitable.
|
||||
*
|
||||
* @return True if the command was suitable, false otherwise.
|
||||
*/
|
||||
public boolean hasProperArguments() {
|
||||
// Make sure the command description is set
|
||||
if(this.commandDescription == null)
|
||||
return false;
|
||||
|
||||
// Get and return the result
|
||||
return getCommandDescription().getSuitableArgumentsDifference(this.queryReference) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command description.
|
||||
*
|
||||
* @return Command description.
|
||||
*/
|
||||
public CommandDescription getCommandDescription() {
|
||||
return this.commandDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command description.
|
||||
*
|
||||
* @param commandDescription The command description.
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public void setCommandDescription(CommandDescription commandDescription) {
|
||||
this.commandDescription = commandDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the command is executable.
|
||||
*
|
||||
* @return True if the command is executable, false otherwise.
|
||||
*/
|
||||
public boolean isExecutable() {
|
||||
// Make sure the command description is valid
|
||||
if(this.commandDescription == null)
|
||||
return false;
|
||||
|
||||
// Check whether the command is executable, return the result
|
||||
return this.commandDescription.isExecutable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender that executed the command.
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
public boolean executeCommand(CommandSender sender) {
|
||||
// Make sure the command description is valid
|
||||
if(this.commandDescription == null)
|
||||
return false;
|
||||
|
||||
// Execute the command
|
||||
return this.commandDescription.execute(sender, this.commandReference, this.commandArguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a command sender has permission to execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
*
|
||||
* @return True if the command sender has permission, false otherwise.
|
||||
*/
|
||||
public boolean hasPermission(CommandSender sender) {
|
||||
// Make sure the command description is valid
|
||||
if(this.commandDescription == null)
|
||||
return false;
|
||||
|
||||
// Get and return the permission
|
||||
return this.commandDescription.getCommandPermissions().hasPermission(sender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command reference.
|
||||
*
|
||||
* @return The command reference.
|
||||
*/
|
||||
public CommandParts getCommandReference() {
|
||||
return this.commandReference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command arguments.
|
||||
*
|
||||
* @return The command arguments.
|
||||
*/
|
||||
public CommandParts getCommandArguments() {
|
||||
return this.commandArguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the original query reference.
|
||||
*
|
||||
* @return Original query reference.
|
||||
*/
|
||||
public CommandParts getQueryReference() {
|
||||
return this.queryReference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the difference value between the original query and the result reference.
|
||||
*
|
||||
* @return The difference value.
|
||||
*/
|
||||
public double getDifference() {
|
||||
// Get the difference through the command found
|
||||
if(this.commandDescription != null)
|
||||
return this.commandDescription.getCommandDifference(this.queryReference);
|
||||
|
||||
// Get the difference from the query reference
|
||||
return this.queryReference.getDifference(commandReference, true);
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.update.UpdateCheckerService;
|
||||
import com.timvisee.dungeonmaze.update.bukkit.Updater;
|
||||
import com.timvisee.dungeonmaze.util.Profiler;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class CheckUpdatesCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Profile the process
|
||||
Profiler p = new Profiler(true);
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Checking for Dungeon Maze updates...");
|
||||
|
||||
// Get the update checker service, shut it down and start it again to force an update check
|
||||
UpdateCheckerService service = Core.getUpdateCheckerService();
|
||||
service.shutdownUpdateChecker();
|
||||
service.setupUpdateChecker();
|
||||
|
||||
// Get the update checker instance
|
||||
Updater uc = service.getUpdateChecker();
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Update checking succeed, took " + p.getTimeFormatted() + "!");
|
||||
|
||||
// Get the version number of the new update
|
||||
String newVer = uc.getLatestName();
|
||||
|
||||
// Make sure any update is available
|
||||
if(uc.getResult() == Updater.UpdateResult.UPDATE_AVAILABLE) {
|
||||
sender.sendMessage(ChatColor.GREEN + "New Dungeon Maze version available: " + String.valueOf(newVer));
|
||||
return true;
|
||||
|
||||
} else if(uc.getResult() == Updater.UpdateResult.NO_UPDATE) {
|
||||
sender.sendMessage(ChatColor.GREEN + "You are running the latest Dungeon Maze version!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the new version is compatible with the current bukkit version
|
||||
if(uc.getResult() == Updater.UpdateResult.FAIL_NOVERSION) {
|
||||
// Show a message
|
||||
sender.sendMessage(ChatColor.GREEN + "New Dungeon Maze version available: " + String.valueOf(newVer));
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The new version is not compatible with your Bukkit version!");
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Please update your Bukkit to " + uc.getLatestGameVersion() + " or higher!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check whether the update was installed or not
|
||||
if(uc.getResult() == Updater.UpdateResult.SUCCESS)
|
||||
sender.sendMessage(ChatColor.GREEN + "New version installed (" + String.valueOf(newVer) + "). Server reboot required!");
|
||||
|
||||
else {
|
||||
sender.sendMessage(ChatColor.GREEN + "New version found: " + String.valueOf(newVer));
|
||||
//noinspection SpellCheckingInspection
|
||||
sender.sendMessage(ChatColor.GREEN + "Use " + ChatColor.GOLD + "/dm installupdate" +
|
||||
ChatColor.GREEN + " to automatically install the new version!");
|
||||
}
|
||||
|
||||
// Return the result
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.DungeonMaze;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.util.Profiler;
|
||||
import com.timvisee.dungeonmaze.world.WorldManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.WorldCreator;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class CreateWorldCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Get and trim the preferred world name
|
||||
String worldName = commandArguments.get(0).trim();
|
||||
|
||||
// Validate the world name
|
||||
if(!WorldManager.isValidWorldName(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + worldName);
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The world name contains invalid characters!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Set whether the world should be preloaded
|
||||
boolean preloadWorld = true;
|
||||
|
||||
// Get whether the world should be preloaded based on the arguments
|
||||
if(commandArguments.getCount() >= 2) {
|
||||
String arg = commandArguments.get(1);
|
||||
|
||||
// Check whether the argument equals 'force'
|
||||
if(arg.equalsIgnoreCase("preload"))
|
||||
preloadWorld = true;
|
||||
|
||||
else if(arg.equalsIgnoreCase("true") || arg.equalsIgnoreCase("t") || arg.equalsIgnoreCase("yes") || arg.equalsIgnoreCase("y"))
|
||||
preloadWorld = true;
|
||||
|
||||
else if(arg.equalsIgnoreCase("false") || arg.equalsIgnoreCase("f") || arg.equalsIgnoreCase("no") || arg.equalsIgnoreCase("n"))
|
||||
preloadWorld = false;
|
||||
|
||||
else {
|
||||
sender.sendMessage(ChatColor.DARK_RED + arg);
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Invalid argument!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the world manager, and make sure it's valid
|
||||
WorldManager worldManager = Core.getWorldManager();
|
||||
boolean showWorldManagerError = false;
|
||||
if(worldManager == null)
|
||||
showWorldManagerError = true;
|
||||
else if(!worldManager.isInit())
|
||||
showWorldManagerError = true;
|
||||
if(showWorldManagerError) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to create the world, world manager not available!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the world doesn't exist
|
||||
if(worldManager.isWorld(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The world " + ChatColor.GOLD + worldName + ChatColor.DARK_RED + " already exists!");
|
||||
sender.sendMessage(ChatColor.YELLOW + "Use the command " + ChatColor.GOLD + "/" + commandReference.get(0) + " listworlds" + ChatColor.YELLOW + " to list all worlds.");
|
||||
sender.sendMessage(ChatColor.YELLOW + "Use the command " + ChatColor.GOLD + "/" + commandReference.get(0) + " loadworld " + worldName + ChatColor.YELLOW + " to load the world.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Preparing the server...");
|
||||
|
||||
// Prepare the server for the new world
|
||||
if(!worldManager.prepareDungeonMazeWorld(worldName, preloadWorld)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to prepare the server!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Generating the " + DungeonMaze.PLUGIN_NAME + " " + ChatColor.GOLD + worldName + ChatColor.YELLOW + "...");
|
||||
Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "Generating a new world, expecting lag for a while...");
|
||||
|
||||
// Profile the world generation
|
||||
Profiler p = new Profiler(true);
|
||||
|
||||
// Create the world
|
||||
// TODO: Put this in a separate function!
|
||||
WorldCreator newWorld = new WorldCreator(worldName);
|
||||
newWorld.generator(DungeonMaze.instance.getDungeonMazeGenerator());
|
||||
World world = newWorld.createWorld();
|
||||
|
||||
// Force-save the level.dat file for the world, profile the process
|
||||
Profiler pWorldSave = new Profiler(true);
|
||||
try {
|
||||
// Force-save the world, and show some status messages
|
||||
Core.getLogger().info("Force saving the level.dat file for '" + world.getName() + "'...");
|
||||
world.save();
|
||||
Core.getLogger().info("World saved successfully, took " + pWorldSave.getTimeFormatted() + "!");
|
||||
|
||||
} catch(Exception ex) {
|
||||
Core.getLogger().error("Failed to save the world after " + pWorldSave.getTimeFormatted() + "!");
|
||||
}
|
||||
|
||||
// Make sure the world instance is valid
|
||||
if(world == null) {
|
||||
Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "World generation failed after " + p.getTimeFormatted() + "!");
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The " + DungeonMaze.PLUGIN_NAME + " " + ChatColor.GOLD + worldName + ChatColor.GREEN +
|
||||
" failed to generate after " + p.getTimeFormatted() + "!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Show a status message
|
||||
Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "World generation finished, took " + p.getTimeFormatted() + "!");
|
||||
sender.sendMessage(ChatColor.GREEN + "The " + DungeonMaze.PLUGIN_NAME + " " + ChatColor.GOLD + worldName + ChatColor.GREEN +
|
||||
" has successfully been generated, took " + p.getTimeFormatted() + "!");
|
||||
|
||||
// If the command was executed by a player, teleport the player
|
||||
if(sender instanceof Player) {
|
||||
// Teleport the player
|
||||
((Player) sender).teleport(world.getSpawnLocation());
|
||||
sender.sendMessage(ChatColor.GREEN + "You have been teleported to " + ChatColor.GOLD + worldName + ChatColor.GREEN + "!");
|
||||
}
|
||||
|
||||
// Return the result
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.DungeonMaze;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class DungeonMazeCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Show some version info
|
||||
sender.sendMessage(ChatColor.GREEN + "This server is running " + DungeonMaze.PLUGIN_NAME + " v" + DungeonMaze.getVersionName() + "! " + ChatColor.RED + "<3");
|
||||
sender.sendMessage(ChatColor.YELLOW + "Use the command " + ChatColor.GOLD + "/" + commandReference.get(0) + " help" + ChatColor.YELLOW + " to view help.");
|
||||
sender.sendMessage(ChatColor.YELLOW + "Use the command " + ChatColor.GOLD + "/" + commandReference.get(0) + " about" + ChatColor.YELLOW + " to view about.");
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.command.help.HelpProvider;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class HelpCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Check whether quick help should be shown
|
||||
boolean quickHelp = commandArguments.getCount() == 0;
|
||||
|
||||
// Set the proper command arguments for the quick help
|
||||
if(quickHelp)
|
||||
commandArguments = new CommandParts(commandReference.get(0));
|
||||
|
||||
// Show the new help
|
||||
if(quickHelp)
|
||||
HelpProvider.showHelp(sender, commandReference, commandArguments, false, false, false, false, false, true);
|
||||
else
|
||||
HelpProvider.showHelp(sender, commandReference, commandArguments);
|
||||
|
||||
// Return the result
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.update.UpdateCheckerService;
|
||||
import com.timvisee.dungeonmaze.update.bukkit.Updater;
|
||||
import com.timvisee.dungeonmaze.util.Profiler;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class InstallUpdateCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Profile the process
|
||||
Profiler p = new Profiler(true);
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Checking for Dungeon Maze updates...");
|
||||
|
||||
// Get the update checker service, shut it down and start it again to force an update check
|
||||
UpdateCheckerService service = Core.getUpdateCheckerService();
|
||||
service.shutdownUpdateChecker();
|
||||
service.setupUpdateChecker();
|
||||
|
||||
// Get the update checker instance
|
||||
Updater uc = service.getUpdateChecker();
|
||||
|
||||
// TODO: Automatically install the actual update!
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Update checking succeed, took " + p.getTimeFormatted() + "!");
|
||||
|
||||
// Get the version number of the new update
|
||||
String newVer = uc.getLatestName();
|
||||
|
||||
// Make sure any update is available
|
||||
if(uc.getResult() == Updater.UpdateResult.UPDATE_AVAILABLE) {
|
||||
sender.sendMessage(ChatColor.GREEN + "New Dungeon Maze version available: " + String.valueOf(newVer));
|
||||
return true;
|
||||
|
||||
} else if(uc.getResult() == Updater.UpdateResult.NO_UPDATE) {
|
||||
sender.sendMessage(ChatColor.GREEN + "You are running the latest Dungeon Maze version!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the new version is compatible with the current bukkit version
|
||||
if(uc.getResult() == Updater.UpdateResult.FAIL_NOVERSION) {
|
||||
// Show a message
|
||||
sender.sendMessage(ChatColor.GREEN + "New Dungeon Maze version available: " + String.valueOf(newVer));
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The new version is not compatible with your Bukkit version!");
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Please update your Bukkit to " + uc.getLatestGameVersion() + " or higher!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check whether the update was installed or not
|
||||
if(uc.getResult() == Updater.UpdateResult.SUCCESS)
|
||||
sender.sendMessage(ChatColor.GREEN + "New version installed (" + String.valueOf(newVer) + "). Server reboot required!");
|
||||
else
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Automatic installation failed, please update manually!");
|
||||
|
||||
// Return the result
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.DungeonMaze;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.world.WorldManager;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ListWorldCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Get the world manager and make sure it's valid
|
||||
WorldManager worldManager = Core.getWorldManager();
|
||||
if(worldManager == null) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Error, failed to list the worlds!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get the list of Dungeon Maze worlds and other worlds
|
||||
List<String> dungeonMazeWorlds = worldManager.getDungeonMazeWorlds();
|
||||
List<String> otherWorlds = worldManager.getWorlds(true);
|
||||
|
||||
// Show the list of Dungeon Maze worlds
|
||||
sender.sendMessage(ChatColor.GOLD + "==========[ \" + DungeonMaze.PLUGIN_NAME.toUpperCase() + \" WORLDS ]==========");
|
||||
sender.sendMessage(ChatColor.GOLD + DungeonMaze.PLUGIN_NAME + " worlds:");
|
||||
if(dungeonMazeWorlds.size() > 0) {
|
||||
for(String worldName : dungeonMazeWorlds) {
|
||||
if(worldManager.isDungeonMazeWorldLoaded(worldName))
|
||||
sender.sendMessage(ChatColor.WHITE + " " + worldName + ChatColor.GREEN + ChatColor.ITALIC + " (Loaded)");
|
||||
else
|
||||
sender.sendMessage(ChatColor.WHITE + " " + worldName + ChatColor.GRAY + ChatColor.ITALIC + " (Not Loaded)");
|
||||
}
|
||||
} else
|
||||
// No Dungeon Maze world available, show a message
|
||||
sender.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + " No Dungeon Maze worlds available!");
|
||||
|
||||
// Show the list of other worlds
|
||||
sender.sendMessage(ChatColor.GOLD + "Other worlds:");
|
||||
if(otherWorlds.size() > 0) {
|
||||
for(String worldName : otherWorlds) {
|
||||
if(worldManager.isWorldLoaded(worldName))
|
||||
sender.sendMessage(ChatColor.WHITE + " " + worldName + ChatColor.GREEN + ChatColor.ITALIC + " (Loaded)");
|
||||
else
|
||||
sender.sendMessage(ChatColor.WHITE + " " + worldName + ChatColor.GRAY + ChatColor.ITALIC + " (Not Loaded)");
|
||||
}
|
||||
} else
|
||||
// No other world available, show a message
|
||||
sender.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + " No other worlds available!");
|
||||
|
||||
// Return the result
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.util.Profiler;
|
||||
import com.timvisee.dungeonmaze.world.WorldManager;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class LoadWorldCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Get and trim the preferred world name
|
||||
String worldName = commandArguments.get(0).trim();
|
||||
|
||||
// Profile the world loading
|
||||
Profiler p = new Profiler(true);
|
||||
|
||||
// Validate the world name
|
||||
if(!WorldManager.isValidWorldName(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + worldName);
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The world name contains invalid characters!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get the world manager, and make sure it's valid
|
||||
WorldManager worldManager = Core.getWorldManager();
|
||||
boolean showWorldManagerError = false;
|
||||
if(worldManager == null)
|
||||
showWorldManagerError = true;
|
||||
else if(!worldManager.isInit())
|
||||
showWorldManagerError = true;
|
||||
if(showWorldManagerError) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to load the world, world manager not available!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the world exists
|
||||
if(!worldManager.isWorld(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The world " + ChatColor.GOLD + worldName + ChatColor.DARK_RED + " doesn't exist!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the world isn't loaded already
|
||||
if(worldManager.isWorldLoaded(worldName)) {
|
||||
sender.sendMessage(ChatColor.GREEN + "The world " + ChatColor.GOLD + worldName + ChatColor.GREEN + " is already loaded!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Load the world, store the instance
|
||||
World world = worldManager.loadWorld(worldName);
|
||||
|
||||
// Make sure the world was loaded
|
||||
if(world == null) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to load the world!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Show a status message, return the result
|
||||
sender.sendMessage(ChatColor.GREEN + "The world " + ChatColor.GOLD + worldName + ChatColor.GREEN + " has been loaded, took " + p.getTimeFormatted() + "!");
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.config.ConfigHandler;
|
||||
import com.timvisee.dungeonmaze.util.Profiler;
|
||||
import com.timvisee.dungeonmaze.world.WorldManager;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class ReloadCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Profile the reload process
|
||||
Profiler p = new Profiler(true);
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Reloading Dungeon Maze...");
|
||||
|
||||
/* // Set whether the reload is forced
|
||||
boolean force = false;
|
||||
|
||||
// Get whether the reload should be forced from the command arguments
|
||||
if(commandArguments.getCount() >= 1) {
|
||||
String arg = commandArguments.get(0);
|
||||
|
||||
// Check whether the argument equals 'force'
|
||||
if(arg.equalsIgnoreCase("force") || arg.equalsIgnoreCase("forced"))
|
||||
force = true;
|
||||
|
||||
else if(arg.equalsIgnoreCase("true") || arg.equalsIgnoreCase("t") || arg.equalsIgnoreCase("yes") || arg.equalsIgnoreCase("y"))
|
||||
force = true;
|
||||
|
||||
else if(arg.equalsIgnoreCase("false") || arg.equalsIgnoreCase("f") || arg.equalsIgnoreCase("no") || arg.equalsIgnoreCase("n"))
|
||||
force = false;
|
||||
|
||||
else {
|
||||
sender.sendMessage(ChatColor.DARK_RED + arg);
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Invalid argument!");
|
||||
return true;
|
||||
}
|
||||
}*/
|
||||
|
||||
// Reload the configuration
|
||||
ConfigHandler configHandler = Core.getConfigHandler();
|
||||
if(configHandler != null) {
|
||||
configHandler.load();
|
||||
sender.sendMessage(ChatColor.YELLOW + "Reloaded the configuration!");
|
||||
} else
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to reload the configuration!");
|
||||
|
||||
// Get the world manager to reload the world list, and make sure it's valid
|
||||
WorldManager worldManager = Core.getWorldManager();
|
||||
if(worldManager != null) {
|
||||
worldManager.refresh();
|
||||
sender.sendMessage(ChatColor.YELLOW + "Reloaded the worlds!");
|
||||
} else
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to reload the worlds!");
|
||||
|
||||
// Dungeon Maze reloaded, show a status message
|
||||
sender.sendMessage(ChatColor.GREEN + "Dungeon Maze has been reloaded successfully, took " + p.getTimeFormatted() + "!");
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.permission.PermissionsManager;
|
||||
import com.timvisee.dungeonmaze.util.Profiler;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class ReloadPermissionsCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Profile the permissions reload process
|
||||
Profiler p = new Profiler(true);
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Reloading permissions...");
|
||||
Core.getLogger().info("Reloading permissions...");
|
||||
|
||||
// Get the permissions manager and make sure it's valid
|
||||
PermissionsManager permissionsManager = Core.getPermissionsManager();
|
||||
if(permissionsManager == null) {
|
||||
Core.getLogger().info("Failed to access the permissions manager after " + p.getTimeFormatted() + "!");
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to access the permissions manager after " + p.getTimeFormatted() + "!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Reload the permissions service, show an error on failure
|
||||
if(!permissionsManager.reload()) {
|
||||
Core.getLogger().info("Failed to reload permissions after " + p.getTimeFormatted() + "!");
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to reload permissions after " + p.getTimeFormatted() + "!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Show a success message
|
||||
Core.getLogger().info("Permissions reloaded successfully, took " + p.getTimeFormatted() + "!");
|
||||
sender.sendMessage(ChatColor.GREEN + "Permissions reloaded successfully, took " + p.getTimeFormatted() + "!");
|
||||
|
||||
// Get and show the permissions system being used
|
||||
String permissionsSystem = ChatColor.GOLD + permissionsManager.getUsedPermissionsSystemType().getName();
|
||||
Core.getLogger().info("Used permissions system: " + permissionsSystem);
|
||||
sender.sendMessage(ChatColor.GREEN + "Used permissions system: " + permissionsSystem);
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.DungeonMaze;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.util.Profiler;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class RestartCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Profile the restart process
|
||||
Profiler p = new Profiler(true);
|
||||
|
||||
// Set whether the restart is forced
|
||||
boolean force = false;
|
||||
|
||||
// Get whether the restart should be forced from the command arguments
|
||||
if(commandArguments.getCount() >= 1) {
|
||||
String arg = commandArguments.get(0);
|
||||
|
||||
// Check whether the argument equals 'force'
|
||||
if(arg.equalsIgnoreCase("force") || arg.equalsIgnoreCase("forced"))
|
||||
force = true;
|
||||
|
||||
else if(arg.equalsIgnoreCase("true") || arg.equalsIgnoreCase("t") || arg.equalsIgnoreCase("yes") || arg.equalsIgnoreCase("y"))
|
||||
force = true;
|
||||
|
||||
else if(arg.equalsIgnoreCase("false") || arg.equalsIgnoreCase("f") || arg.equalsIgnoreCase("no") || arg.equalsIgnoreCase("n"))
|
||||
force = false;
|
||||
|
||||
else {
|
||||
sender.sendMessage(ChatColor.DARK_RED + arg);
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Invalid argument!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Show a restart warning
|
||||
if(force) {
|
||||
sender.sendMessage(ChatColor.YELLOW + "Force restarting Dungeon Maze...");
|
||||
Core.getLogger().info("Force restarting Dungeon Maze...");
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.YELLOW + "Restarting Dungeon Maze...");
|
||||
Core.getLogger().info("Restarting Dungeon Maze...");
|
||||
}
|
||||
|
||||
// Profile the Dungeon Maze Core destruction
|
||||
Profiler stopCoreProfiler = new Profiler(true);
|
||||
|
||||
// Destroy the Dungeon Maze core
|
||||
if(!DungeonMaze.instance.destroyCore(force)) {
|
||||
// Failed to destroy the core, show a status message
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to stop the Dungeon Maze Core after " + stopCoreProfiler.getTimeFormatted() + "!");
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Please use " + ChatColor.GOLD + "/reload" + ChatColor.DARK_RED + " for plugin instability reasons!");
|
||||
Core.getLogger().error("Failed to stop the core, after " + stopCoreProfiler.getTimeFormatted() + "!");
|
||||
|
||||
// Return if the restart isn't force
|
||||
if(!force)
|
||||
return true;
|
||||
}
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Dungeon Maze Core stopped, took " + stopCoreProfiler.getTimeFormatted() + "!");
|
||||
|
||||
// Profile the core starting
|
||||
Profiler startCoreProfiler = new Profiler(true);
|
||||
|
||||
// Initialize the core, show the result status
|
||||
if(!DungeonMaze.instance.initCore()) {
|
||||
// Core failed to initialize, show a status message
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to start the Dungeon Maze Core after " + startCoreProfiler.getTimeFormatted() + "!");
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Please use " + ChatColor.GOLD + "/reload" + ChatColor.DARK_RED + " for plugin instability reasons!");
|
||||
Core.getLogger().error("Failed to start the core, after " + startCoreProfiler.getTimeFormatted() + "!");
|
||||
|
||||
// Return if the restart isn't forced
|
||||
if(!force)
|
||||
return true;
|
||||
}
|
||||
|
||||
// Core initialized, show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Dungeon Maze Core started, took " + startCoreProfiler.getTimeFormatted() + "!");
|
||||
|
||||
// Show a status message of the running services
|
||||
final int runningServices = Core.instance.getServiceManager().getServiceCount(true);
|
||||
final int totalServices = Core.instance.getServiceManager().getServiceCount();
|
||||
sender.sendMessage(ChatColor.YELLOW + "Started " + ChatColor.GOLD + runningServices + ChatColor.YELLOW + " out of " + ChatColor.GOLD + totalServices + ChatColor.YELLOW + " Dungeon Maze services!");
|
||||
|
||||
// Dungeon Maze restarted, show a status message
|
||||
sender.sendMessage(ChatColor.GREEN + "Dungeon Maze has been restarted successfully, took " + p.getTimeFormatted() + "!");
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.DungeonMaze;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.service.Service;
|
||||
import com.timvisee.dungeonmaze.service.ServiceManager;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ServiceCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Print the status info header
|
||||
sender.sendMessage(ChatColor.GOLD + "==========[ " + DungeonMaze.PLUGIN_NAME.toUpperCase() + " SERVICES ]==========");
|
||||
|
||||
// Get the service manager and make sure it's valid
|
||||
ServiceManager serviceManager = Core.instance.getServiceManager();
|
||||
if(serviceManager == null) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Error, failed to retrieve the services information!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Print the service count and the list of services
|
||||
sender.sendMessage(ChatColor.GOLD + "Running Services: " + ChatColor.WHITE + serviceManager.getServiceCount(true) + ChatColor.GRAY + " / " + Core.instance.getServiceManager().getServiceCount());
|
||||
printServices(sender);
|
||||
|
||||
// Return the result
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print all services.
|
||||
*
|
||||
* @param sender The command sender to print the services to.
|
||||
*/
|
||||
public void printServices(CommandSender sender) {
|
||||
// Get the service manager and make sure it's valid
|
||||
ServiceManager serviceManager = Core.instance.getServiceManager();
|
||||
if(serviceManager == null) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Error, failed to retrieve the services information!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all the services
|
||||
List<Service> services = serviceManager.getServices();
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Services:");
|
||||
|
||||
// Print all the services
|
||||
for(Service service : services) {
|
||||
// Check whether the service is initialized
|
||||
if(service.isInit())
|
||||
sender.sendMessage(ChatColor.WHITE + " " + service.getName() + " service " + ChatColor.GREEN + ChatColor.ITALIC + "(Loaded)");
|
||||
else
|
||||
sender.sendMessage(ChatColor.WHITE + " " + service.getName() + " service " + ChatColor.DARK_RED + ChatColor.ITALIC + "(Not loaded)");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,180 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.DungeonMaze;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.permission.PermissionsManager;
|
||||
import com.timvisee.dungeonmaze.util.MinecraftUtils;
|
||||
import com.timvisee.dungeonmaze.util.SystemUtils;
|
||||
import com.timvisee.dungeonmaze.world.WorldManager;
|
||||
import com.timvisee.dungeonmaze.world.dungeon.chunk.grid.DungeonChunkGridManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class StatusCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Print the status info header
|
||||
sender.sendMessage(ChatColor.GOLD + "==========[ " + DungeonMaze.PLUGIN_NAME.toUpperCase() + " STATUS ]==========");
|
||||
|
||||
// Get the world manager
|
||||
WorldManager worldManager = Core.getWorldManager();
|
||||
|
||||
// Print the number of Dungeon Maze worlds
|
||||
if(worldManager != null)
|
||||
sender.sendMessage(ChatColor.GOLD + DungeonMaze.PLUGIN_NAME + " worlds: " + ChatColor.WHITE + worldManager.getDungeonMazeWorlds().size());
|
||||
else
|
||||
sender.sendMessage(ChatColor.GOLD + DungeonMaze.PLUGIN_NAME + " worlds: " + ChatColor.DARK_RED + ChatColor.ITALIC + "Unknown!");
|
||||
|
||||
// Print the Dungeon Maze player count
|
||||
int playerCount = Bukkit.getOnlinePlayers().size();
|
||||
int dungeonMazePlayerCount = 0;
|
||||
if(worldManager != null) {
|
||||
for(Player player : Bukkit.getOnlinePlayers())
|
||||
if(worldManager.isDungeonMazeWorld(player.getWorld().getName()))
|
||||
dungeonMazePlayerCount++;
|
||||
|
||||
sender.sendMessage(ChatColor.GOLD + DungeonMaze.PLUGIN_NAME + " players: " + ChatColor.WHITE + dungeonMazePlayerCount + ChatColor.GRAY + " / " + playerCount);
|
||||
|
||||
} else
|
||||
sender.sendMessage(ChatColor.GOLD + DungeonMaze.PLUGIN_NAME + " players: " + ChatColor.DARK_RED + ChatColor.ITALIC + "Unknown!");
|
||||
|
||||
// Get the permissions manager
|
||||
PermissionsManager permissionsManager = Core.getPermissionsManager();
|
||||
|
||||
// Print the permissions manager status
|
||||
if(permissionsManager != null) {
|
||||
// Get the used permissions system
|
||||
PermissionsManager.PermissionsSystemType type = permissionsManager.getUsedPermissionsSystemType();
|
||||
|
||||
if(!type.equals(PermissionsManager.PermissionsSystemType.NONE))
|
||||
sender.sendMessage(ChatColor.GOLD + "Permissions System: " + ChatColor.GREEN + permissionsManager.getUsedPermissionsSystemType().getName());
|
||||
else
|
||||
sender.sendMessage(ChatColor.GOLD + "Permissions System: " + ChatColor.GRAY + ChatColor.ITALIC + permissionsManager.getUsedPermissionsSystemType().getName());
|
||||
} else
|
||||
sender.sendMessage(ChatColor.GOLD + "Permissions System: " + ChatColor.DARK_RED + ChatColor.ITALIC + "Unknown!");
|
||||
|
||||
// Get the dungeon chunk grid manager
|
||||
DungeonChunkGridManager dungeonChunkGridManager = Core.getDungeonChunkGridManager();
|
||||
if(dungeonChunkGridManager != null) {
|
||||
int loadedChunks = dungeonChunkGridManager.getLoadedChunksCount();
|
||||
int loadedGrids = dungeonChunkGridManager.getLoadedGridsCount();
|
||||
sender.sendMessage(ChatColor.GOLD + "Loaded Dungeon Chunks: " + ChatColor.WHITE + loadedChunks + ChatColor.GRAY + " in " + ChatColor.WHITE + loadedGrids + ChatColor.GRAY + " grid" + (loadedGrids != 1 ? "s" : ""));
|
||||
} else
|
||||
sender.sendMessage(ChatColor.GOLD + "Loaded Dungeon Chunks: " + ChatColor.DARK_RED + ChatColor.ITALIC + "Unknown!");
|
||||
|
||||
// Print the service count
|
||||
sender.sendMessage(ChatColor.GOLD + "Running Services: " + ChatColor.WHITE + Core.instance.getServiceManager().getServiceCount(true) + ChatColor.GRAY + " / " + Core.instance.getServiceManager().getServiceCount());
|
||||
|
||||
// Print the plugin runtime
|
||||
printPluginRuntime(sender);
|
||||
|
||||
// Show the version status
|
||||
sender.sendMessage(ChatColor.GOLD + "Version: " + ChatColor.WHITE + "Dungeon Maze v" + DungeonMaze.getVersionName() + ChatColor.GRAY + " (code: " + DungeonMaze.getVersionCode() + ")");
|
||||
|
||||
// Print the server status
|
||||
printServerStatus(sender);
|
||||
|
||||
// Print the machine status
|
||||
printMachineStatus(sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the plugin runtime.
|
||||
*
|
||||
* @param sender Command sender to print the runtime to.
|
||||
*/
|
||||
public void printPluginRuntime(CommandSender sender) {
|
||||
// Get the runtime
|
||||
long runtime = new Date().getTime() - Core.getInitializationTime().getTime();
|
||||
|
||||
// Calculate the timings
|
||||
int millis = (int) (runtime % 1000);
|
||||
runtime/=1000;
|
||||
int seconds = (int) (runtime % 60);
|
||||
runtime/=60;
|
||||
int minutes = (int) (runtime % 60);
|
||||
runtime/=60;
|
||||
int hours = (int) runtime;
|
||||
|
||||
// Create a double and triple digit formatter
|
||||
DecimalFormat doubleDigit = new DecimalFormat("######00");
|
||||
DecimalFormat tripleDigit = new DecimalFormat("000");
|
||||
|
||||
// Generate the timing string
|
||||
StringBuilder runtimeStr = new StringBuilder(ChatColor.WHITE + doubleDigit.format(seconds) + ChatColor.GRAY + "." + ChatColor.WHITE + tripleDigit.format(millis));
|
||||
String measurement = "Seconds";
|
||||
if(minutes > 0 || hours > 0) {
|
||||
runtimeStr.insert(0, ChatColor.WHITE + doubleDigit.format(minutes) + ChatColor.GRAY + ":");
|
||||
measurement = "Minutes";
|
||||
if(hours > 0) {
|
||||
runtimeStr.insert(0, ChatColor.WHITE + doubleDigit.format(hours) + ChatColor.GRAY + ":");
|
||||
measurement = "Hours";
|
||||
}
|
||||
}
|
||||
|
||||
// Print the runtime
|
||||
sender.sendMessage(ChatColor.GOLD + "Runtime: " + ChatColor.WHITE + runtimeStr + " " + ChatColor.GRAY + measurement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the server status.
|
||||
*
|
||||
* @param sender The command sender to print the status to.
|
||||
*/
|
||||
public void printServerStatus(CommandSender sender) {
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "Server Status:");
|
||||
|
||||
// Print the server status
|
||||
sender.sendMessage(ChatColor.GOLD + "Detected Minecraft Version: " + ChatColor.WHITE + MinecraftUtils.getMinecraftVersion());
|
||||
sender.sendMessage(ChatColor.GOLD + "Detected Minecraft Server: " + ChatColor.WHITE + MinecraftUtils.getServerType().getName());
|
||||
sender.sendMessage(ChatColor.GOLD + "Server Version: " + ChatColor.WHITE + Bukkit.getVersion());
|
||||
sender.sendMessage(ChatColor.GOLD + "Bukkit Version: " + ChatColor.WHITE + Bukkit.getBukkitVersion());
|
||||
sender.sendMessage(ChatColor.GOLD + "Running Plugins: " + ChatColor.WHITE + Bukkit.getPluginManager().getPlugins().length);
|
||||
|
||||
// Get the world manager
|
||||
WorldManager worldManager = Core.getWorldManager();
|
||||
if(worldManager != null)
|
||||
sender.sendMessage(ChatColor.GOLD + "Loaded Worlds: " + ChatColor.WHITE + Bukkit.getWorlds().size() + ChatColor.GRAY + " / " + worldManager.getWorlds().size());
|
||||
|
||||
// Print the server time
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
sender.sendMessage(ChatColor.GOLD + "Server Time: " + ChatColor.WHITE + dateFormat.format(new Date()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the machine status.
|
||||
*
|
||||
* @param sender The command sender to print the status to.
|
||||
*/
|
||||
public void printMachineStatus(CommandSender sender) {
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "Machine Status:");
|
||||
|
||||
// Return the machine status
|
||||
sender.sendMessage(ChatColor.GOLD + "OS Name: " + ChatColor.WHITE + System.getProperty("os.name"));
|
||||
sender.sendMessage(ChatColor.GOLD + "OS Architecture: " + ChatColor.WHITE + SystemUtils.getSystemArchNumber() + "-bit" + ChatColor.GRAY + " (" + SystemUtils.getSystemArchFull() + ")");
|
||||
sender.sendMessage(ChatColor.GOLD + "OS Version: " + ChatColor.WHITE + System.getProperty("os.version"));
|
||||
sender.sendMessage(ChatColor.GOLD + "Java Version: " + ChatColor.WHITE + System.getProperty("java.version") + ChatColor.GRAY + " (" + System.getProperty("sun.arch.data.model") + "-bit)");
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.world.WorldManager;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class TeleportCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Make sure the command is executed by an in-game player
|
||||
if(!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "You need to be in-game to use this command!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get the player and the world name to teleport to
|
||||
Player player = (Player) sender;
|
||||
String worldName = commandArguments.get(0);
|
||||
|
||||
// Get the world manager, and make sure it's valid
|
||||
WorldManager worldManager = Core.getWorldManager();
|
||||
boolean showWorldManagerError = false;
|
||||
if(worldManager == null)
|
||||
showWorldManagerError = true;
|
||||
else if(!worldManager.isInit())
|
||||
showWorldManagerError = true;
|
||||
if(showWorldManagerError) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to teleport, world manager not available!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the world exists
|
||||
if(!worldManager.isWorld(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + worldName);
|
||||
sender.sendMessage(ChatColor.DARK_RED + "This world doesn't exists!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try to load the world
|
||||
World world = worldManager.loadWorld(worldName);
|
||||
|
||||
// Make sure the world was loaded successfully
|
||||
if(world == null) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to teleport, unable to load the world!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get the spawn location to teleport the player to
|
||||
Location spawn = world.getSpawnLocation();
|
||||
|
||||
// Force-set the location on Dungeon Maze worlds
|
||||
// TODO: Fix this!
|
||||
if(worldManager.isDungeonMazeWorld(worldName)) {
|
||||
spawn.setX(4);
|
||||
spawn.setY(68);
|
||||
spawn.setZ(4);
|
||||
}
|
||||
|
||||
// Teleport the player, show a status message and return true
|
||||
player.teleport(spawn);
|
||||
player.sendMessage(ChatColor.GREEN + "You have been teleported to " + ChatColor.GOLD + worldName + ChatColor.GREEN + "!");
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import com.timvisee.dungeonmaze.util.Profiler;
|
||||
import com.timvisee.dungeonmaze.world.WorldManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class UnloadWorldCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Get and trim the preferred world name
|
||||
String worldName = commandArguments.get(0).trim();
|
||||
|
||||
// Profile the world unloading
|
||||
Profiler p = new Profiler(true);
|
||||
|
||||
// Validate the world name
|
||||
if(!WorldManager.isValidWorldName(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + worldName);
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The world name contains invalid characters!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get the world manager, and make sure it's valid
|
||||
WorldManager worldManager = Core.getWorldManager();
|
||||
boolean showWorldManagerError = false;
|
||||
if(worldManager == null)
|
||||
showWorldManagerError = true;
|
||||
else if(!worldManager.isInit())
|
||||
showWorldManagerError = true;
|
||||
if(showWorldManagerError) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to unload the world, world manager not available!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the world exists
|
||||
if(!worldManager.isWorld(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The world " + ChatColor.GOLD + worldName + ChatColor.DARK_RED + " doesn't exist!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the world is loaded
|
||||
if(!worldManager.isWorldLoaded(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The world " + ChatColor.GOLD + worldName + ChatColor.DARK_RED + " isn't loaded!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make sure the main world isn't unloaded
|
||||
if(worldManager.isMainWorld(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "The main world can't be unloaded!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get all players in the world
|
||||
List<Player> players = Bukkit.getOnlinePlayers().stream().filter(player -> player.getWorld().getName().equals(worldName)).collect(Collectors.toList());
|
||||
int playerCount = players.size();
|
||||
|
||||
// Teleport all players away
|
||||
if(playerCount > 0) {
|
||||
// Get the main world
|
||||
World mainWorld = worldManager.getMainWorld();
|
||||
Location mainWorldSpawn = mainWorld.getSpawnLocation();
|
||||
|
||||
// Teleport all players
|
||||
for(Player player : players) {
|
||||
// Teleport the player to the spawn of the main world
|
||||
player.teleport(mainWorldSpawn);
|
||||
|
||||
// Show a message to the player
|
||||
player.sendMessage(ChatColor.YELLOW + "The current world is being unloaded, you've been teleported!");
|
||||
}
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage(ChatColor.YELLOW + "Teleported " + ChatColor.GOLD + playerCount + ChatColor.YELLOW + " player" + (playerCount != 1 ? "s" : "") + " away!");
|
||||
}
|
||||
|
||||
// Force the world to be loaded if it isn't already loaded
|
||||
if(!worldManager.unloadWorld(worldName)) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to unload the world!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Show a status message, return the result
|
||||
sender.sendMessage(ChatColor.GREEN + "The world " + ChatColor.GOLD + worldName + ChatColor.GREEN + " has been unloaded, took " + p.getTimeFormatted() + "!");
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package fr.xephi.authme.commands.dynamic.executable;
|
||||
|
||||
import com.timvisee.dungeonmaze.DungeonMaze;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.ExecutableCommand;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class VersionCommand extends ExecutableCommand {
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
* @return True if the command was executed successfully, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Show some version info
|
||||
sender.sendMessage(ChatColor.GOLD + "==========[ " + DungeonMaze.PLUGIN_NAME.toUpperCase() + " ABOUT ]==========");
|
||||
sender.sendMessage(ChatColor.GOLD + "Version: " + ChatColor.WHITE + DungeonMaze.PLUGIN_NAME + " v" + DungeonMaze.getVersionName() + ChatColor.GRAY + " (code: " + DungeonMaze.getVersionCode() + ")");
|
||||
sender.sendMessage(ChatColor.GOLD + "Developers:");
|
||||
printDeveloper(sender, "Tim Visee", "timvisee", "Lead Developer");
|
||||
printDeveloper(sender, "Xephi", "xephi", "Code Contributor");
|
||||
printDeveloper(sender, "sgdc3", "sgdc3", "Code Contributor");
|
||||
printDeveloper(sender, "Metonymia", "Metonymia", "Design Contributor");
|
||||
sender.sendMessage(ChatColor.GOLD + "Website: " + ChatColor.WHITE + "http://timvisee.com/projects/bukkit/dungeon-maze/");
|
||||
sender.sendMessage(ChatColor.GOLD + "License: " + ChatColor.WHITE + "GNU GPL v3.0" + ChatColor.GRAY + ChatColor.ITALIC + " (See LICENSE file)");
|
||||
sender.sendMessage(ChatColor.GOLD + "Copyright: " + ChatColor.WHITE + "Copyright (c) Tim Visee 2015. All rights reserved.");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a developer with proper styling.
|
||||
*
|
||||
* @param sender The command sender.
|
||||
* @param name The display name of the developer.
|
||||
* @param minecraftName The Minecraft username of the developer, if available.
|
||||
* @param function The function of the developer.
|
||||
*/
|
||||
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
|
||||
private void printDeveloper(CommandSender sender, String name, String minecraftName, String function) {
|
||||
// Print the name
|
||||
StringBuilder msg = new StringBuilder();
|
||||
msg.append(" " + ChatColor.WHITE);
|
||||
msg.append(name);
|
||||
|
||||
// Append the Minecraft name, if available
|
||||
if(minecraftName.length() != 0)
|
||||
msg.append(ChatColor.GRAY + " // " + ChatColor.WHITE + minecraftName);
|
||||
msg.append(ChatColor.GRAY + "" + ChatColor.ITALIC + " (" + function + ")");
|
||||
|
||||
// Show the online status
|
||||
if(minecraftName.length() != 0)
|
||||
if(isPlayerOnline(minecraftName))
|
||||
msg.append(ChatColor.GREEN + "" + ChatColor.ITALIC + " (In-Game)");
|
||||
|
||||
// Print the message
|
||||
sender.sendMessage(msg.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a player is online.
|
||||
*
|
||||
* @param minecraftName The Minecraft player name.
|
||||
*
|
||||
* @return True if the player is online, false otherwise.
|
||||
*/
|
||||
private boolean isPlayerOnline(String minecraftName) {
|
||||
for(Player player : Bukkit.getOnlinePlayers())
|
||||
if(player.getName().equalsIgnoreCase(minecraftName))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,189 @@
|
||||
package fr.xephi.authme.commands.dynamic.help;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.command.CommandArgumentDescription;
|
||||
import com.timvisee.dungeonmaze.command.CommandDescription;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.command.CommandPermissions;
|
||||
import com.timvisee.dungeonmaze.util.StringUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class HelpPrinter {
|
||||
|
||||
/**
|
||||
* Print the command help information.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print.
|
||||
* @param commandReference The command reference used.
|
||||
*/
|
||||
public static void printCommand(CommandSender sender, CommandDescription command, CommandParts commandReference) {
|
||||
// Print the proper command syntax
|
||||
sender.sendMessage(ChatColor.GOLD + "Command: " + HelpSyntaxHelper.getCommandSyntax(command, commandReference, null, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help description information. This will print both the short, as the detailed description if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print the description help for.
|
||||
*/
|
||||
public static void printCommandDescription(CommandSender sender, CommandDescription command) {
|
||||
// Print the regular description, if available
|
||||
if(command.hasDescription())
|
||||
sender.sendMessage(ChatColor.GOLD + "Short Description: " + ChatColor.WHITE + command.getDescription());
|
||||
|
||||
// Print the detailed description, if available
|
||||
if(command.hasDetailedDescription()) {
|
||||
sender.sendMessage(ChatColor.GOLD + "Detailed Description:");
|
||||
sender.sendMessage(ChatColor.WHITE + " " + command.getDetailedDescription());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help arguments information if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print the argument help for.
|
||||
*/
|
||||
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
|
||||
public static void printArguments(CommandSender sender, CommandDescription command) {
|
||||
// Make sure there are any commands to print
|
||||
if(!command.hasArguments() && command.getMaximumArguments() >= 0)
|
||||
return;
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Arguments:");
|
||||
|
||||
// Print each argument
|
||||
for(CommandArgumentDescription arg : command.getArguments()) {
|
||||
// Create a string builder to build the syntax in
|
||||
StringBuilder argString = new StringBuilder();
|
||||
argString.append(" " + ChatColor.YELLOW + ChatColor.ITALIC + arg.getLabel() + " : " + ChatColor.WHITE + arg.getDescription());
|
||||
|
||||
// Suffix a note if the command is optional
|
||||
if(arg.isOptional())
|
||||
argString.append(ChatColor.GRAY + "" + ChatColor.ITALIC + " (Optional)");
|
||||
|
||||
// Print the syntax
|
||||
sender.sendMessage(argString.toString());
|
||||
}
|
||||
|
||||
// Show the unlimited arguments argument
|
||||
if(command.getMaximumArguments() < 0)
|
||||
sender.sendMessage(" " + ChatColor.YELLOW + ChatColor.ITALIC + "... : " + ChatColor.WHITE + "Any additional arguments." + ChatColor.GRAY + ChatColor.ITALIC + " (Optional)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help permissions information if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print the permissions help for.
|
||||
*/
|
||||
public static void printPermissions(CommandSender sender, CommandDescription command) {
|
||||
// Get the permissions and make sure it isn't null
|
||||
CommandPermissions permissions = command.getCommandPermissions();
|
||||
if(permissions == null)
|
||||
return;
|
||||
|
||||
// Make sure any permission node is set
|
||||
if(permissions.getPermissionNodeCount() <= 0)
|
||||
return;
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Permissions:");
|
||||
|
||||
// Print each node
|
||||
for(String node : permissions.getPermissionNodes()) {
|
||||
boolean nodePermission = true;
|
||||
if(sender instanceof Player)
|
||||
nodePermission = Core.getPermissionsManager().hasPermission((Player) sender, node, false);
|
||||
final String nodePermsString = ChatColor.GRAY + (nodePermission ? ChatColor.ITALIC + " (Permission!)" : ChatColor.ITALIC + " (No Permission!)");
|
||||
sender.sendMessage(" " + ChatColor.YELLOW + ChatColor.ITALIC + node + nodePermsString);
|
||||
}
|
||||
|
||||
// Print the default permission
|
||||
switch(permissions.getDefaultPermission()) {
|
||||
case ALLOWED:
|
||||
sender.sendMessage(ChatColor.GOLD + " Default: " + ChatColor.GRAY + ChatColor.ITALIC + "Permission!");
|
||||
break;
|
||||
|
||||
case OP_ONLY:
|
||||
final String defaultPermsString = ChatColor.GRAY + (permissions.getDefaultPermissionCommandSender(sender) ? ChatColor.ITALIC + " (Permission!)" : ChatColor.ITALIC + " (No Permission!)");
|
||||
sender.sendMessage(ChatColor.GOLD + " Default: " + ChatColor.YELLOW + ChatColor.ITALIC + "OP's Only!" + defaultPermsString);
|
||||
break;
|
||||
|
||||
case NOT_ALLOWED:
|
||||
default:
|
||||
sender.sendMessage(ChatColor.GOLD + " Default: " + ChatColor.GRAY + ChatColor.ITALIC + "No Permission!");
|
||||
break;
|
||||
}
|
||||
|
||||
// Print the permission result
|
||||
if(permissions.hasPermission(sender))
|
||||
sender.sendMessage(ChatColor.GOLD + " Result: " + ChatColor.GREEN + ChatColor.ITALIC + "Permission!");
|
||||
else
|
||||
sender.sendMessage(ChatColor.GOLD + " Result: " + ChatColor.DARK_RED + ChatColor.ITALIC + "No Permission!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help alternatives information if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command used.
|
||||
* @param commandReference The original command reference used for this command.
|
||||
*/
|
||||
public static void printAlternatives(CommandSender sender, CommandDescription command, CommandParts commandReference) {
|
||||
// Make sure there are any alternatives
|
||||
if(command.getLabels().size() <= 1)
|
||||
return;
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Alternatives:");
|
||||
|
||||
// Get the label used
|
||||
final String usedLabel = commandReference.get(command.getParentCount());
|
||||
|
||||
// Create a list of alternatives
|
||||
List<String> alternatives = new ArrayList<>();
|
||||
for(String entry : command.getLabels()) {
|
||||
// Exclude the proper argument
|
||||
if(entry.equalsIgnoreCase(usedLabel))
|
||||
continue;
|
||||
alternatives.add(entry);
|
||||
}
|
||||
|
||||
// Sort the alternatives
|
||||
Collections.sort(alternatives, (o1, o2) -> Double.compare(StringUtils.getDifference(usedLabel, o1), StringUtils.getDifference(usedLabel, o2)));
|
||||
|
||||
// Print each alternative with proper syntax
|
||||
for(String alternative : alternatives)
|
||||
sender.sendMessage(" " + HelpSyntaxHelper.getCommandSyntax(command, commandReference, alternative, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help child's information if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print the help for.
|
||||
* @param commandReference The original command reference used for this command.
|
||||
*/
|
||||
public static void printChildren(CommandSender sender, CommandDescription command, CommandParts commandReference) {
|
||||
// Make sure there are child's
|
||||
if(command.getChilds().size() <= 0)
|
||||
return;
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Commands:");
|
||||
|
||||
// Loop through each child
|
||||
for(CommandDescription child : command.getChilds())
|
||||
sender.sendMessage(" " + HelpSyntaxHelper.getCommandSyntax(child, commandReference, null, false) + ChatColor.GRAY + ChatColor.ITALIC + " : " + child.getDescription());
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
package fr.xephi.authme.commands.dynamic.help;
|
||||
|
||||
import com.timvisee.dungeonmaze.Core;
|
||||
import com.timvisee.dungeonmaze.command.*;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class HelpProvider {
|
||||
|
||||
/**
|
||||
* Show help for a specific command.
|
||||
*
|
||||
* @param sender The command sender the help needs to be shown to.
|
||||
* @param reference The command reference to the help command.
|
||||
* @param helpQuery The query to show help for.
|
||||
*/
|
||||
public static void showHelp(CommandSender sender, CommandParts reference, CommandParts helpQuery) {
|
||||
showHelp(sender, reference, helpQuery, true, true, true, true, true, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show help for a specific command.
|
||||
*
|
||||
* @param sender The command sender the help needs to be shown to.
|
||||
* @param reference The command reference to the help command.
|
||||
* @param helpQuery The query to show help for.
|
||||
* @param showCommand True to show the command.
|
||||
* @param showDescription True to show the command description, both the short and detailed description.
|
||||
* @param showArguments True to show the command argument help.
|
||||
* @param showPermissions True to show the command permission help.
|
||||
* @param showAlternatives True to show the command alternatives.
|
||||
* @param showCommands True to show the child commands.
|
||||
*/
|
||||
public static void showHelp(CommandSender sender, CommandParts reference, CommandParts helpQuery, boolean showCommand, boolean showDescription, boolean showArguments, boolean showPermissions, boolean showAlternatives, boolean showCommands) {
|
||||
// Find the command for this help query, one with and one without a prefixed base command
|
||||
FoundCommandResult result = Core.getCommandHandler().getCommandManager().findCommand(new CommandParts(helpQuery.getList()));
|
||||
CommandParts commandReferenceOther = new CommandParts(reference.get(0), helpQuery.getList());
|
||||
FoundCommandResult resultOther = Core.getCommandHandler().getCommandManager().findCommand(commandReferenceOther);
|
||||
if(resultOther != null) {
|
||||
if(result == null)
|
||||
result = resultOther;
|
||||
|
||||
else if(result.getDifference() > resultOther.getDifference())
|
||||
result = resultOther;
|
||||
}
|
||||
|
||||
// Make sure a result was found
|
||||
if(result == null) {
|
||||
// Show a warning message
|
||||
sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.ITALIC + helpQuery);
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Couldn't show any help information for this help query.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the command description, and make sure it's valid
|
||||
CommandDescription command = result.getCommandDescription();
|
||||
if(command == null) {
|
||||
// Show a warning message
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Failed to retrieve any help information!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the proper command reference to use for the help page
|
||||
CommandParts commandReference = command.getCommandReference(result.getQueryReference());
|
||||
|
||||
// Get the base command
|
||||
String baseCommand = commandReference.get(0);
|
||||
|
||||
// Make sure the difference between the command reference and the actual command isn't too big
|
||||
final double commandDifference = result.getDifference();
|
||||
if(commandDifference > 0.20) {
|
||||
// Show the unknown command warning
|
||||
sender.sendMessage(ChatColor.DARK_RED + "No help found for '" + helpQuery + "'!");
|
||||
|
||||
// Get the suggested command
|
||||
CommandParts suggestedCommandParts = new CommandParts(result.getCommandDescription().getCommandReference(commandReference).getRange(1));
|
||||
|
||||
// Show a command suggestion if available and the difference isn't too big
|
||||
if(commandDifference < 0.75)
|
||||
if(result.getCommandDescription() != null)
|
||||
sender.sendMessage(ChatColor.YELLOW + "Did you mean " + ChatColor.GOLD + "/" + baseCommand + " help " + suggestedCommandParts + ChatColor.YELLOW + "?");
|
||||
|
||||
// Show the help command
|
||||
sender.sendMessage(ChatColor.YELLOW + "Use the command " + ChatColor.GOLD + "/" + baseCommand + " help" + ChatColor.YELLOW + " to view help.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Show a message when the command handler is assuming a command
|
||||
if(commandDifference > 0) {
|
||||
// Get the suggested command
|
||||
CommandParts suggestedCommandParts = new CommandParts(result.getCommandDescription().getCommandReference(commandReference).getRange(1));
|
||||
|
||||
// Show the suggested command
|
||||
sender.sendMessage(ChatColor.DARK_RED + "No help found, assuming '" + ChatColor.GOLD + suggestedCommandParts + ChatColor.DARK_RED + "'!");
|
||||
}
|
||||
|
||||
// Print the help header
|
||||
sender.sendMessage(ChatColor.GOLD + "==========[ DUNGEON MAZE HELP ]==========");
|
||||
|
||||
// Print the command help information
|
||||
if(showCommand)
|
||||
HelpPrinter.printCommand(sender, command, commandReference);
|
||||
if(showDescription)
|
||||
HelpPrinter.printCommandDescription(sender, command);
|
||||
if(showArguments)
|
||||
HelpPrinter.printArguments(sender, command);
|
||||
if(showPermissions)
|
||||
HelpPrinter.printPermissions(sender, command);
|
||||
if(showAlternatives)
|
||||
HelpPrinter.printAlternatives(sender, command, commandReference);
|
||||
if(showCommands)
|
||||
HelpPrinter.printChildren(sender, command, commandReference);
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package fr.xephi.authme.commands.dynamic.help;
|
||||
|
||||
import com.timvisee.dungeonmaze.command.CommandArgumentDescription;
|
||||
import com.timvisee.dungeonmaze.command.CommandDescription;
|
||||
import com.timvisee.dungeonmaze.command.CommandParts;
|
||||
import com.timvisee.dungeonmaze.util.ListUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
public class HelpSyntaxHelper {
|
||||
|
||||
/**
|
||||
* Get the proper syntax for a command.
|
||||
*
|
||||
* @param commandDescription The command to get the syntax for.
|
||||
* @param commandReference The reference of the command.
|
||||
* @param alternativeLabel The alternative label to use for this command syntax.
|
||||
* @param highlight True to highlight the important parts of this command.
|
||||
*
|
||||
* @return The command with proper syntax.
|
||||
*/
|
||||
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
|
||||
public static String getCommandSyntax(CommandDescription commandDescription, CommandParts commandReference, String alternativeLabel, boolean highlight) {
|
||||
// Create a string builder to build the command
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// Set the color and prefix a slash
|
||||
sb.append(ChatColor.WHITE + "/");
|
||||
|
||||
// Get the help command reference, and the command label
|
||||
CommandParts helpCommandReference = commandDescription.getCommandReference(commandReference);
|
||||
final String parentCommand = (new CommandParts(helpCommandReference.getRange(0, helpCommandReference.getCount() - 1))).toString();
|
||||
String commandLabel = helpCommandReference.get(helpCommandReference.getCount() - 1);
|
||||
|
||||
// Check whether the alternative label should be used
|
||||
if(alternativeLabel != null)
|
||||
if(alternativeLabel.trim().length() > 0)
|
||||
commandLabel = alternativeLabel;
|
||||
|
||||
// Show the important bit of the command, highlight this part if required
|
||||
sb.append(ListUtils.implode(parentCommand, (highlight ? ChatColor.YELLOW + "" + ChatColor.BOLD : "") + commandLabel, " "));
|
||||
if(highlight)
|
||||
sb.append(ChatColor.YELLOW);
|
||||
|
||||
// Add each command arguments
|
||||
for(CommandArgumentDescription arg : commandDescription.getArguments()) {
|
||||
// Add the argument as optional or non-optional argument
|
||||
if(!arg.isOptional())
|
||||
sb.append(ChatColor.ITALIC + " <" + arg.getLabel() + ">");
|
||||
else
|
||||
sb.append(ChatColor.ITALIC + " [" + arg.getLabel() + "]");
|
||||
}
|
||||
|
||||
// Add some dots if the command allows unlimited arguments
|
||||
if(commandDescription.getMaximumArguments() < 0)
|
||||
sb.append(ChatColor.ITALIC + " ...");
|
||||
|
||||
// Return the build command syntax
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user