add priority option
This commit is contained in:
parent
66b1381cd2
commit
a804a58e60
|
@ -3,14 +3,17 @@ package com.ne0nx3r0.betteralias.alias;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.event.EventPriority;
|
||||
|
||||
public class Alias
|
||||
{
|
||||
public final String command;
|
||||
public final boolean caseSensitive;
|
||||
private final String permission;
|
||||
private final EventPriority priority;
|
||||
private final HashMap<Integer, List<AliasCommand>> parameters;
|
||||
|
||||
public Alias(String commandName,boolean caseSensitive,String permissionNode)
|
||||
|
||||
public Alias(String commandName, boolean caseSensitive, String permissionNode, String priority)
|
||||
{
|
||||
this.caseSensitive = caseSensitive;
|
||||
|
||||
|
@ -22,12 +25,42 @@ public class Alias
|
|||
{
|
||||
this.command = commandName.toLowerCase();
|
||||
}
|
||||
|
||||
if (priority != null) {
|
||||
switch (priority.toLowerCase()) {
|
||||
case "lowest":
|
||||
this.priority = EventPriority.LOWEST;
|
||||
break;
|
||||
case "low":
|
||||
this.priority = EventPriority.LOW;
|
||||
break;
|
||||
case "normal":
|
||||
this.priority = EventPriority.NORMAL;
|
||||
break;
|
||||
case "high":
|
||||
this.priority = EventPriority.HIGH;
|
||||
break;
|
||||
case "highest":
|
||||
this.priority = EventPriority.HIGHEST;
|
||||
break;
|
||||
|
||||
default:
|
||||
this.priority = EventPriority.LOWEST;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
this.priority = EventPriority.LOWEST;
|
||||
}
|
||||
|
||||
this.permission = permissionNode;
|
||||
|
||||
this.parameters = new HashMap<Integer,List<AliasCommand>>();
|
||||
}
|
||||
|
||||
public EventPriority getPriority() {
|
||||
return this.priority;
|
||||
}
|
||||
|
||||
public boolean hasCommandFor(int length)
|
||||
{
|
||||
return this.parameters.containsKey(length) || this.parameters.containsKey(-1);
|
||||
|
|
|
@ -6,7 +6,7 @@ public class AliasCommand
|
|||
final AliasCommandTypes type;
|
||||
int waitTime;
|
||||
|
||||
public AliasCommand(String command,AliasCommandTypes type,int waitTime)
|
||||
public AliasCommand(String command, AliasCommandTypes type, int waitTime)
|
||||
{
|
||||
this.command = command;
|
||||
this.type = type;
|
||||
|
|
|
@ -1,21 +1,27 @@
|
|||
package com.ne0nx3r0.betteralias.alias;
|
||||
|
||||
import com.ne0nx3r0.betteralias.BetterAlias;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventPriority;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import com.ne0nx3r0.betteralias.BetterAlias;
|
||||
|
||||
|
||||
// Helper methods
|
||||
|
@ -53,17 +59,21 @@ public class AliasManager {
|
|||
Alias alias = new Alias(
|
||||
sAlias,
|
||||
yml.getBoolean(sAlias + ".caseSensitive", false),
|
||||
yml.getString(sAlias + ".permission", null));
|
||||
yml.getString(sAlias + ".permission", null),
|
||||
yml.getString(sAlias + ".priority", null));
|
||||
|
||||
for (String sArg : yml.getConfigurationSection(sAlias).getKeys(false)) {
|
||||
List<AliasCommand> commandsList = new ArrayList<AliasCommand>();
|
||||
|
||||
if (!sArg.equalsIgnoreCase("permission") && !sArg.equalsIgnoreCase("caseSensitive")) {
|
||||
if (!sArg.equalsIgnoreCase("permission")
|
||||
&& !sArg.equalsIgnoreCase("caseSensitive")
|
||||
&& !sArg.equalsIgnoreCase("priority")) {
|
||||
int iArg;
|
||||
|
||||
if (sArg.equals("*")) {
|
||||
iArg = -1;
|
||||
} else {
|
||||
// TODO This raise error sometime on unknown configuration parameter
|
||||
iArg = Integer.parseInt(sArg);
|
||||
}
|
||||
|
||||
|
@ -333,18 +343,21 @@ public class AliasManager {
|
|||
return false;
|
||||
}
|
||||
|
||||
public Collection<Alias> getAliasMatches(String sCommand) {
|
||||
public Collection<Alias> getAliasMatches(String sCommand, EventPriority priority) {
|
||||
String sCommandLower = sCommand.toLowerCase()+" ";
|
||||
|
||||
ArrayList<Alias> aliasMatches = new ArrayList<Alias>();
|
||||
|
||||
for (Alias alias : this.aliases.values()) {
|
||||
if (priority != alias.getPriority()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (alias.caseSensitive) {
|
||||
if (sCommand.startsWith(alias.command+" ")) {
|
||||
aliasMatches.add(alias);
|
||||
}
|
||||
}
|
||||
else if (sCommandLower.startsWith(alias.command+" ")) {
|
||||
} else if (sCommandLower.startsWith(alias.command+" ")) {
|
||||
aliasMatches.add(alias);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package com.ne0nx3r0.betteralias.command;
|
||||
|
||||
import com.ne0nx3r0.betteralias.BetterAlias;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.ne0nx3r0.betteralias.BetterAlias;
|
||||
|
||||
public class BetterAliasCommandExecutor implements CommandExecutor {
|
||||
private final BetterAlias plugin;
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.ne0nx3r0.betteralias.listener;
|
||||
|
||||
import com.ne0nx3r0.betteralias.BetterAlias;
|
||||
import com.ne0nx3r0.betteralias.alias.Alias;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -11,8 +9,8 @@ import org.bukkit.event.Listener;
|
|||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.server.ServerCommandEvent;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import com.ne0nx3r0.betteralias.BetterAlias;
|
||||
import com.ne0nx3r0.betteralias.alias.Alias;
|
||||
|
||||
public class BetterAliasCommandListener implements Listener {
|
||||
private final BetterAlias plugin;
|
||||
|
@ -22,17 +20,41 @@ public class BetterAliasCommandListener implements Listener {
|
|||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
|
||||
public void onPlayerCommandPreprocessLowest(PlayerCommandPreprocessEvent e) {
|
||||
onPlayerCommandPreprocess(e, EventPriority.LOWEST);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onPlayerCommandPreprocessLow(PlayerCommandPreprocessEvent e) {
|
||||
onPlayerCommandPreprocess(e, EventPriority.LOW);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
public void onPlayerCommandPreprocessNormal(PlayerCommandPreprocessEvent e) {
|
||||
onPlayerCommandPreprocess(e, EventPriority.NORMAL);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onPlayerCommandPreprocessHigh(PlayerCommandPreprocessEvent e) {
|
||||
onPlayerCommandPreprocess(e, EventPriority.HIGH);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPlayerCommandPreprocessHighest(PlayerCommandPreprocessEvent e) {
|
||||
onPlayerCommandPreprocess(e, EventPriority.HIGHEST);
|
||||
}
|
||||
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e, EventPriority priority) {
|
||||
|
||||
String sCommand = e.getMessage().substring(1);
|
||||
|
||||
for(Alias alias : plugin.aliasManager.getAliasMatches(sCommand)){
|
||||
for (Alias alias : plugin.aliasManager.getAliasMatches(sCommand, priority)) {
|
||||
String sArgs = sCommand.substring(alias.command.length());
|
||||
|
||||
Player player = e.getPlayer();
|
||||
String sNode = "betteralias." + alias.getPermissionNode();
|
||||
|
||||
if (alias.hasPermission()
|
||||
&& !player.hasPermission(sNode)) {
|
||||
if (alias.hasPermission() && !player.hasPermission(sNode)) {
|
||||
player.sendMessage(ChatColor.RED + "You do not have permission to use this alias.");
|
||||
player.sendMessage(ChatColor.GRAY + "Node: " + sNode);
|
||||
|
||||
|
@ -46,10 +68,35 @@ public class BetterAliasCommandListener implements Listener {
|
|||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onConsoleCommand(ServerCommandEvent e) {
|
||||
public void onConsoleCommandLowest(ServerCommandEvent e) {
|
||||
onConsoleCommand(e, EventPriority.LOWEST);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onConsoleCommandLow(ServerCommandEvent e) {
|
||||
onConsoleCommand(e, EventPriority.LOW);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
public void onConsoleCommandNormal(ServerCommandEvent e) {
|
||||
onConsoleCommand(e, EventPriority.NORMAL);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onConsoleCommandHigh(ServerCommandEvent e) {
|
||||
onConsoleCommand(e, EventPriority.HIGH);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onConsoleCommandHighest(ServerCommandEvent e) {
|
||||
onConsoleCommand(e, EventPriority.HIGHEST);
|
||||
}
|
||||
|
||||
public void onConsoleCommand(ServerCommandEvent e, EventPriority priority) {
|
||||
|
||||
String sCommand = e.getCommand();
|
||||
|
||||
for(Alias alias : plugin.aliasManager.getAliasMatches(sCommand)){
|
||||
for (Alias alias : plugin.aliasManager.getAliasMatches(sCommand, priority)) {
|
||||
String sArgs = sCommand.substring(alias.command.length());
|
||||
|
||||
if (plugin.aliasManager.sendAliasCommands(alias, e.getSender(), sArgs)) {
|
||||
|
|
Loading…
Reference in New Issue