mirror of
https://github.com/Ne0nx3r0/BetterAlias.git
synced 2025-01-22 21:42:44 +01:00
Updating to v2
This commit is contained in:
parent
06be8e7218
commit
29cbaaf456
94
aliases.yml
94
aliases.yml
@ -1,37 +1,57 @@
|
||||
#'dumb' parameter, will just use what you put in
|
||||
banhammer:
|
||||
1: /ban !1
|
||||
#name parameter is replaced with the command users name
|
||||
gmon:
|
||||
0: /gamemode !name 1
|
||||
1: /gamemode !1 1
|
||||
gmoff:
|
||||
0: /gamemode !name 0
|
||||
1: /gamemode !1 0
|
||||
# putting a P after the parameter specifies this is a player, and BA should try to guess which player
|
||||
# useful for things like "/to e0n" instead of "/to Ne0nx3r0"
|
||||
# This will ONLY look for players that are online
|
||||
to:
|
||||
1: /tp !name !1p
|
||||
2: /tp !1p !2p
|
||||
bring:
|
||||
1: /tp !1p !name
|
||||
# multiple command example
|
||||
tpthenkick:
|
||||
1:
|
||||
- /tp !1p !name
|
||||
- /kick !1p
|
||||
# special command for toggling GM mode
|
||||
gm:
|
||||
0: /gamemode !oppositeGameMode !name
|
||||
# hand all parameters in order
|
||||
s:
|
||||
"*": /say I want to say "!*", there I said it!
|
||||
|
||||
# replaced with the item name in the player's hand
|
||||
hand:
|
||||
0: I have a !handItemName in my hand right now!
|
||||
|
||||
# replace with the item ID in the player's hand
|
||||
handid:
|
||||
0: /give !name !handItemID
|
||||
#'dumb' parameter, will just use what you put in
|
||||
banhammer:
|
||||
1: /ban !1
|
||||
#name parameter is replaced with the command users name
|
||||
gmon:
|
||||
0: /gamemode !name 1
|
||||
1: /gamemode !1 1
|
||||
gmoff:
|
||||
0: /gamemode !name 0
|
||||
1: /gamemode !1 0
|
||||
# putting a P after the parameter specifies this is a player, and BA should try to guess which player
|
||||
# useful for things like "/to e0n" instead of "/to Ne0nx3r0"
|
||||
# This will ONLY look for players that are online
|
||||
to:
|
||||
1: /tp !name !1p
|
||||
2: /tp !1p !2p
|
||||
bring:
|
||||
1: /tp !1p !name
|
||||
# multiple command example
|
||||
tpthenkick:
|
||||
1:
|
||||
- /tp !1p !name
|
||||
- /kick !1p
|
||||
|
||||
# replaced with the item name in the player's hand
|
||||
hand:
|
||||
0: I have a !handItemName in my hand right now!
|
||||
|
||||
# replace with the item ID in the player's hand
|
||||
handid:
|
||||
0: /give !name !handItemID
|
||||
|
||||
# Color example
|
||||
# ----------------
|
||||
# Available colors:
|
||||
# BLACK, DARK_BLUE, DARK_GREEN, DARK_AQUA, DARK_RED, DARK_PURPLE, GOLD, GRAY,
|
||||
# DARK_GRAY, BLUE, GREEN, AQUA, RED, LIGHT_PURPLE, YELLOW, WHITE, MAGIC, BOLD,
|
||||
# STRIKETHROUGH, UNDERLINE, ITALIC, RESET
|
||||
#
|
||||
# (Also shows how to use the reply modifier to only reply to the player)
|
||||
redText:
|
||||
0: reply Look at my &REDcolored &DARK_GREENtext!
|
||||
|
||||
# Console example
|
||||
# If this seems dangerous, that's because it is!
|
||||
#
|
||||
# Required permission will be betteralias.your.node
|
||||
makeop:
|
||||
permission: your.node
|
||||
# Quotes are required because 0 uses a colon (:)
|
||||
0: "reply Usage: /makeop <username>"
|
||||
1:
|
||||
- console /say !1 has been opped!
|
||||
- console /op !1
|
||||
- /me has opped !1
|
||||
2: console /say testing single line
|
||||
3: reply testing single line
|
@ -1,20 +0,0 @@
|
||||
package com.gmail.Ne0nx3r0.AliasManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class Alias{
|
||||
//mapping number of parameters to string(s) to be executed
|
||||
private Map<String,String[]> params = new HashMap<String,String[]>();
|
||||
|
||||
public Alias(Map<String,String[]> p){
|
||||
this.params = p;
|
||||
}
|
||||
|
||||
public String[] getCommands(int p){
|
||||
if(this.params.containsKey(Integer.toString(p))){
|
||||
return this.params.get(Integer.toString(p));
|
||||
}
|
||||
return this.params.get("*");//or null if it doesn't exist
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
package com.gmail.Ne0nx3r0.AliasManager;
|
||||
|
||||
import com.gmail.Ne0nx3r0.BetterAlias;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class AliasManager{
|
||||
private static Map<String,Alias> aliases;
|
||||
private static BetterAlias plugin;
|
||||
|
||||
public AliasManager(BetterAlias ba){
|
||||
plugin = ba;
|
||||
|
||||
loadAliases();
|
||||
}
|
||||
|
||||
private void loadAliases(){
|
||||
aliases = new HashMap<String,Alias>();
|
||||
|
||||
File configFile = new File(plugin.getDataFolder(), "aliases.yml");
|
||||
|
||||
if(!configFile.exists()){
|
||||
configFile.getParentFile().mkdirs();
|
||||
copy(plugin.getResource("aliases.yml"), configFile);
|
||||
}
|
||||
|
||||
FileConfiguration yml = YamlConfiguration.loadConfiguration(configFile);
|
||||
|
||||
Set<String> aliasList = yml.getKeys(false);
|
||||
|
||||
if(aliasList.isEmpty()){
|
||||
plugin.log("No aliases found in aliases.yml");
|
||||
return;
|
||||
}
|
||||
|
||||
for(String sAlias : aliasList){
|
||||
Map<String,String[]> shareMap = new HashMap<String,String[]>();
|
||||
|
||||
for(int i=0;i<10;i++){
|
||||
|
||||
if(yml.isList(sAlias+"."+i)){
|
||||
|
||||
shareMap.put(Integer.toString(i),yml.getStringList(sAlias+"."+i).toArray(new String[]{}));
|
||||
|
||||
}else if(yml.isString(sAlias+"."+i)){
|
||||
|
||||
shareMap.put(Integer.toString(i),new String[]{yml.getString(sAlias+"."+i)});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(yml.isList(sAlias+".*")){
|
||||
shareMap.put("*",yml.getStringList(sAlias+".*").toArray(new String[]{}));
|
||||
|
||||
}else if(yml.isString(sAlias+".*")){
|
||||
shareMap.put("*",new String[]{yml.getString(sAlias+".*")});
|
||||
}
|
||||
|
||||
aliases.put(sAlias,new Alias(shareMap));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean isAliased(String alias){
|
||||
return aliases.containsKey(alias);
|
||||
}
|
||||
|
||||
public String[] getAliasCommands(String alias,int paramsCount){
|
||||
return aliases.get(alias).getCommands(paramsCount);
|
||||
}
|
||||
|
||||
private void copy(InputStream in, File file) {
|
||||
try {
|
||||
OutputStream out = new FileOutputStream(file);
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while((len=in.read(buf))>0){
|
||||
out.write(buf,0,len);
|
||||
}
|
||||
out.close();
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package com.gmail.Ne0nx3r0;
|
||||
|
||||
import com.gmail.Ne0nx3r0.AliasManager.AliasManager;
|
||||
import java.util.logging.Level;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class BetterAlias extends JavaPlugin{
|
||||
public static AliasManager aliasManager;
|
||||
public static BetterAlias self;
|
||||
|
||||
@Override
|
||||
public void onEnable()
|
||||
{
|
||||
this.self = this;
|
||||
//Create an alias manager (also loads aliases)
|
||||
aliasManager = new AliasManager(this);
|
||||
|
||||
//Register listeners
|
||||
PluginManager pm = getServer().getPluginManager();
|
||||
|
||||
pm.registerEvents(new BetterAliasPlayerListener(this,aliasManager), this);
|
||||
|
||||
//Register commands
|
||||
getCommand("bareload").setExecutor(new BetterAliasCommandExecutor(this));
|
||||
|
||||
log("[BetterAlias] Enabled");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
log("[BetterAlias] Disabled");
|
||||
}
|
||||
|
||||
//Generic wrappers for console messages
|
||||
public void log(Level level,String sMessage){
|
||||
if(!sMessage.equals(""))
|
||||
getLogger().log(level,sMessage);
|
||||
}
|
||||
public void log(String sMessage){
|
||||
log(Level.INFO,sMessage);
|
||||
}
|
||||
public void error(String sMessage){
|
||||
log(Level.WARNING,sMessage);
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package com.gmail.Ne0nx3r0;
|
||||
|
||||
import com.gmail.Ne0nx3r0.AliasManager.AliasManager;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
class BetterAliasCommandExecutor implements CommandExecutor {
|
||||
private final BetterAlias plugin;
|
||||
|
||||
public BetterAliasCommandExecutor(BetterAlias plugin)
|
||||
{
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings)
|
||||
{
|
||||
if(cs.isOp() || cs.hasPermission("BetterAlias.reload"))
|
||||
{
|
||||
BetterAlias.aliasManager = new AliasManager(BetterAlias.self);
|
||||
|
||||
cs.sendMessage(ChatColor.GOLD+"BetterAlias reloaded!");
|
||||
}
|
||||
else
|
||||
{
|
||||
cs.sendMessage(ChatColor.RED+"You do not have permission to use /bareload (node: BetterAlias.reload)");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
package com.gmail.Ne0nx3r0;
|
||||
|
||||
import com.gmail.Ne0nx3r0.AliasManager.AliasManager;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
class BetterAliasPlayerListener implements Listener{
|
||||
private final BetterAlias plugin;
|
||||
private final AliasManager aliasManager;
|
||||
|
||||
BetterAliasPlayerListener(BetterAlias p, AliasManager am) {
|
||||
this.plugin = p;
|
||||
this.aliasManager = am;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e)
|
||||
{
|
||||
String[] cmd = e.getMessage().substring(1).split(" ");
|
||||
|
||||
if(aliasManager.isAliased(cmd[0])){
|
||||
String[] commandsToExecute = aliasManager.getAliasCommands(cmd[0],cmd.length-1);
|
||||
|
||||
//in case they specified an invalid number of parameters
|
||||
if(commandsToExecute != null){
|
||||
Player player = e.getPlayer();
|
||||
|
||||
for(String commandToExecute : commandsToExecute){
|
||||
//regex time!
|
||||
Pattern patt = Pattern.compile("!([0-9a-zA-Z~*]+)");
|
||||
Matcher m = patt.matcher(commandToExecute);
|
||||
StringBuffer sb = new StringBuffer(commandToExecute.length());
|
||||
|
||||
while(m.find()){
|
||||
String text = m.group(0).substring(1);
|
||||
|
||||
if(text.equalsIgnoreCase("*")){
|
||||
text = e.getMessage().replace("/"+cmd[0]+" ","");
|
||||
}else if(text.equalsIgnoreCase("name")){
|
||||
text = player.getName();
|
||||
}else if(text.equalsIgnoreCase("handItemName")){
|
||||
text = player.getItemInHand().getType().name();
|
||||
}else if(text.equalsIgnoreCase("handItemID")){
|
||||
text = new Integer(player.getItemInHand().getTypeId()).toString();
|
||||
}else if(text.equalsIgnoreCase("oppositeGameMode")){
|
||||
text = (player.getGameMode() == GameMode.SURVIVAL ? "creative" : "survival");
|
||||
|
||||
}else if(text.length() >= 2 && text.substring(1,2).equalsIgnoreCase("p")){
|
||||
int iParam = -1;
|
||||
|
||||
try{
|
||||
iParam = Integer.parseInt(text.substring(0,1));
|
||||
}catch(Exception ex){return; }
|
||||
|
||||
if(iParam > -1 && cmd.length >= iParam){
|
||||
String sPlayerName = cmd[iParam].toLowerCase();
|
||||
|
||||
text = "notFound";
|
||||
|
||||
for(Player p : plugin.getServer().getOnlinePlayers()){
|
||||
if(p.getName().toLowerCase().contains(sPlayerName)){
|
||||
text = p.getName();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
int iParam = -1;
|
||||
|
||||
try{
|
||||
iParam = Integer.parseInt(text);
|
||||
} catch(Exception ex){}
|
||||
|
||||
if(iParam > -1 && cmd.length-1 >= iParam){
|
||||
text = cmd[iParam];
|
||||
}else{
|
||||
text = "";
|
||||
}
|
||||
}
|
||||
|
||||
m.appendReplacement(sb, Matcher.quoteReplacement(text));
|
||||
}
|
||||
m.appendTail(sb);
|
||||
|
||||
//debug: player.sendMessage(sb.toString());
|
||||
player.chat(sb.toString());
|
||||
}
|
||||
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
27
com/ne0nx3r0/betteralias/BetterAlias.java
Normal file
27
com/ne0nx3r0/betteralias/BetterAlias.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.ne0nx3r0.betteralias;
|
||||
|
||||
import com.ne0nx3r0.betteralias.alias.AliasManager;
|
||||
import com.ne0nx3r0.betteralias.listener.BetterAliasPlayerListener;
|
||||
import com.ne0nx3r0.betteralias.listener.command.BetterAliasCommandExecutor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class BetterAlias extends JavaPlugin
|
||||
{
|
||||
public AliasManager aliasManager;
|
||||
|
||||
@Override
|
||||
public void onEnable()
|
||||
{
|
||||
this.aliasManager = new AliasManager(this);
|
||||
|
||||
this.getServer().getPluginManager().registerEvents(new BetterAliasPlayerListener(this), this);
|
||||
|
||||
|
||||
BetterAliasCommandExecutor betterAliasCommandExecutor = new BetterAliasCommandExecutor(this);
|
||||
|
||||
this.getCommand("bareload").setExecutor(betterAliasCommandExecutor);
|
||||
|
||||
//Filler command to null things to since console commands are not cancellable
|
||||
this.getCommand("badonothing").setExecutor(betterAliasCommandExecutor);
|
||||
}
|
||||
}
|
52
com/ne0nx3r0/betteralias/alias/Alias.java
Normal file
52
com/ne0nx3r0/betteralias/alias/Alias.java
Normal file
@ -0,0 +1,52 @@
|
||||
package com.ne0nx3r0.betteralias.alias;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class Alias
|
||||
{
|
||||
private final String command;
|
||||
private final String permission;
|
||||
private final HashMap<Integer, List<AliasCommand>> parameters;
|
||||
|
||||
public Alias(String commandName)
|
||||
{
|
||||
this.command = commandName;
|
||||
this.permission = null;
|
||||
|
||||
this.parameters = new HashMap<Integer,List<AliasCommand>>();
|
||||
}
|
||||
|
||||
public Alias(String commandName,String permissionNode)
|
||||
{
|
||||
this.command = commandName;
|
||||
this.permission = permissionNode;
|
||||
|
||||
this.parameters = new HashMap<Integer,List<AliasCommand>>();
|
||||
}
|
||||
|
||||
public boolean hasCommandFor(int length)
|
||||
{
|
||||
return this.parameters.containsKey(length);
|
||||
}
|
||||
|
||||
public String getPermissionNode()
|
||||
{
|
||||
return this.permission;
|
||||
}
|
||||
|
||||
public boolean hasPermission()
|
||||
{
|
||||
return this.permission != null;
|
||||
}
|
||||
|
||||
Iterable<AliasCommand> getCommands(int length)
|
||||
{
|
||||
return this.parameters.get(length);
|
||||
}
|
||||
|
||||
void setCommandsFor(int length,List<AliasCommand> commandsList)
|
||||
{
|
||||
this.parameters.put(length, commandsList);
|
||||
}
|
||||
}
|
13
com/ne0nx3r0/betteralias/alias/AliasCommand.java
Normal file
13
com/ne0nx3r0/betteralias/alias/AliasCommand.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.ne0nx3r0.betteralias.alias;
|
||||
|
||||
public class AliasCommand
|
||||
{
|
||||
final String command;
|
||||
final AliasCommandTypes type;
|
||||
|
||||
public AliasCommand(String command,AliasCommandTypes type)
|
||||
{
|
||||
this.command = command;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
10
com/ne0nx3r0/betteralias/alias/AliasCommandTypes.java
Normal file
10
com/ne0nx3r0/betteralias/alias/AliasCommandTypes.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.ne0nx3r0.betteralias.alias;
|
||||
|
||||
public enum AliasCommandTypes
|
||||
{
|
||||
PLAYER,
|
||||
CONSOLE,
|
||||
REPLY_MESSAGE;
|
||||
|
||||
public int id;
|
||||
}
|
319
com/ne0nx3r0/betteralias/alias/AliasManager.java
Normal file
319
com/ne0nx3r0/betteralias/alias/AliasManager.java
Normal file
@ -0,0 +1,319 @@
|
||||
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.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.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class AliasManager
|
||||
{
|
||||
private final BetterAlias plugin;
|
||||
private HashMap<String, Alias> aliases;
|
||||
|
||||
public AliasManager(BetterAlias plugin)
|
||||
{
|
||||
this.plugin = plugin;
|
||||
|
||||
this.loadAliases();
|
||||
}
|
||||
|
||||
public final boolean loadAliases()
|
||||
{
|
||||
this.aliases = new HashMap<String,Alias>();
|
||||
|
||||
File configFile = new File(plugin.getDataFolder(), "aliases.yml");
|
||||
|
||||
if(!configFile.exists())
|
||||
{
|
||||
configFile.getParentFile().mkdirs();
|
||||
copy(plugin.getResource("aliases.yml"), configFile);
|
||||
}
|
||||
|
||||
FileConfiguration yml = YamlConfiguration.loadConfiguration(configFile);
|
||||
|
||||
Set<String> aliasList = yml.getKeys(false);
|
||||
|
||||
if(aliasList.isEmpty())
|
||||
{
|
||||
plugin.getLogger().log(Level.WARNING, "No aliases found in aliases.yml");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
for(String sAlias : aliasList)
|
||||
{
|
||||
Alias alias;
|
||||
|
||||
if(yml.isString(sAlias+".permission"))
|
||||
{
|
||||
alias = new Alias(sAlias,yml.getString("permission"));
|
||||
}
|
||||
else
|
||||
{
|
||||
alias = new Alias(sAlias);
|
||||
}
|
||||
|
||||
for(String sArg : yml.getConfigurationSection(sAlias).getKeys(false))
|
||||
{
|
||||
List<AliasCommand> commandsList = new ArrayList<AliasCommand>();
|
||||
|
||||
if(!sArg.equalsIgnoreCase("permission"))
|
||||
{
|
||||
int iArg = Integer.parseInt(sArg);
|
||||
|
||||
List<String> sArgLines = new ArrayList<String>();
|
||||
|
||||
if(yml.isList(sAlias+"."+sArg))
|
||||
{
|
||||
sArgLines = yml.getStringList(sAlias+"."+sArg);
|
||||
}
|
||||
else
|
||||
{
|
||||
sArgLines.add(yml.getString(sAlias+"."+sArg));
|
||||
}
|
||||
|
||||
for(String sArgLine : sArgLines)
|
||||
{
|
||||
String sType = sArgLine.substring(0,sArgLine.indexOf(" "));
|
||||
AliasCommandTypes type = AliasCommandTypes.PLAYER;
|
||||
|
||||
if(sType.equalsIgnoreCase("console"))
|
||||
{
|
||||
type = AliasCommandTypes.CONSOLE;
|
||||
|
||||
sArgLine = sArgLine.substring(sArgLine.indexOf(" ")+1);
|
||||
}
|
||||
else if(sType.equalsIgnoreCase("reply"))
|
||||
{
|
||||
type = AliasCommandTypes.REPLY_MESSAGE;
|
||||
|
||||
sArgLine = sArgLine.substring(sArgLine.indexOf(" ")+1);
|
||||
}
|
||||
|
||||
sArgLine = this.replaceColorCodes(sArgLine);
|
||||
|
||||
commandsList.add(new AliasCommand(sArgLine,type));
|
||||
}
|
||||
|
||||
alias.setCommandsFor(iArg,commandsList);
|
||||
}
|
||||
}
|
||||
|
||||
this.aliases.put(sAlias, alias);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Alias getAlias(String sCommand)
|
||||
{
|
||||
return this.aliases.get(sCommand);
|
||||
}
|
||||
|
||||
public boolean sendAliasCommands(Alias alias,CommandSender cs, String commandString)
|
||||
{
|
||||
Player player = null;
|
||||
|
||||
if(cs instanceof Player)
|
||||
{
|
||||
player = (Player) cs;
|
||||
}
|
||||
|
||||
List<String> args = new ArrayList<String>();
|
||||
|
||||
Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'");
|
||||
|
||||
Matcher regexMatcher = regex.matcher(commandString);
|
||||
|
||||
while(regexMatcher.find())
|
||||
{
|
||||
if(regexMatcher.group(1) != null)
|
||||
{
|
||||
// Add double-quoted string without the quotes
|
||||
args.add(regexMatcher.group(1));
|
||||
}
|
||||
else if (regexMatcher.group(2) != null)
|
||||
{
|
||||
// Add single-quoted string without the quotes
|
||||
args.add(regexMatcher.group(2));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add unquoted word
|
||||
args.add(regexMatcher.group());
|
||||
}
|
||||
}
|
||||
|
||||
if(alias.hasCommandFor(args.size()))
|
||||
{
|
||||
for(AliasCommand ac : alias.getCommands(args.size()))
|
||||
{
|
||||
String sAliasCommand = ac.command;
|
||||
|
||||
Matcher m = Pattern.compile("!([0-9a-zA-Z~*]+)").matcher(sAliasCommand);
|
||||
|
||||
StringBuffer sb = new StringBuffer(sAliasCommand.length());
|
||||
|
||||
while(m.find())
|
||||
{
|
||||
String text = m.group(0).substring(1);
|
||||
|
||||
if(text.equalsIgnoreCase("name"))
|
||||
{
|
||||
if(player != null)
|
||||
{
|
||||
text = player.getName();
|
||||
}
|
||||
else
|
||||
{
|
||||
cs.sendMessage(ChatColor.RED+"[BA] A parameter of this alias requires a player.");
|
||||
}
|
||||
}
|
||||
else if(text.equalsIgnoreCase("handItemName"))
|
||||
{
|
||||
if(player != null)
|
||||
{
|
||||
text = player.getItemInHand().getType().name();
|
||||
}
|
||||
else
|
||||
{
|
||||
cs.sendMessage(ChatColor.RED+"[BA] A parameter of this alias requires a player.");
|
||||
}
|
||||
}
|
||||
else if(text.equalsIgnoreCase("handItemID"))
|
||||
{
|
||||
if(player != null)
|
||||
{
|
||||
text = new Integer(player.getItemInHand().getTypeId()).toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
cs.sendMessage(ChatColor.RED+"[BA] A parameter of this alias requires a player.");
|
||||
}
|
||||
}
|
||||
else if(text.length() >= 2 && text.substring(1,2).equalsIgnoreCase("p"))
|
||||
{
|
||||
int iParam = -1;
|
||||
|
||||
try
|
||||
{
|
||||
iParam = Integer.parseInt(text.substring(0,1));
|
||||
}
|
||||
catch(Exception ex){}
|
||||
|
||||
if(iParam > -1 && args.size() >= iParam)
|
||||
{
|
||||
String sPlayerName = args.get(iParam-1).toLowerCase();
|
||||
|
||||
text = "BetterAliasPlayerNotFound";
|
||||
|
||||
for(Player p : plugin.getServer().getOnlinePlayers())
|
||||
{
|
||||
if(p.getName().toLowerCase().contains(sPlayerName))
|
||||
{
|
||||
text = p.getName();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int iParam = -1;
|
||||
|
||||
try
|
||||
{
|
||||
iParam = Integer.parseInt(text);
|
||||
}
|
||||
catch(Exception ex){}
|
||||
|
||||
if(iParam > -1 && args.size() >= iParam)
|
||||
{
|
||||
text = args.get(iParam-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
text = "";
|
||||
}
|
||||
}
|
||||
|
||||
m.appendReplacement(sb, Matcher.quoteReplacement(text));
|
||||
}
|
||||
|
||||
m.appendTail(sb);
|
||||
|
||||
String sNewCommand = sb.toString();
|
||||
if(ac.type.equals(AliasCommandTypes.REPLY_MESSAGE))
|
||||
{
|
||||
cs.sendMessage(sNewCommand);
|
||||
}
|
||||
else if(ac.type.equals(AliasCommandTypes.CONSOLE)
|
||||
|| player == null)
|
||||
{
|
||||
if(player != null)
|
||||
{
|
||||
plugin.getLogger().log(Level.INFO,"[BA] Running console command for "+player.getName()+": "+sNewCommand);
|
||||
}
|
||||
else
|
||||
{
|
||||
cs.sendMessage(ChatColor.AQUA+"[BA] Running: "+sNewCommand);
|
||||
}
|
||||
|
||||
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), sNewCommand);
|
||||
}
|
||||
else
|
||||
{
|
||||
player.chat(sNewCommand);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Helper method
|
||||
public void copy(InputStream in, File file)
|
||||
{
|
||||
try
|
||||
{
|
||||
OutputStream out = new FileOutputStream(file);
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while((len=in.read(buf))>0)
|
||||
{
|
||||
out.write(buf,0,len);
|
||||
}
|
||||
out.close();
|
||||
in.close();
|
||||
} catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private String replaceColorCodes(String str)
|
||||
{
|
||||
for(ChatColor cc : ChatColor.values())
|
||||
{
|
||||
str = str.replace("&"+cc.name(), cc.toString());
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
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;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.server.ServerCommandEvent;
|
||||
|
||||
public class BetterAliasPlayerListener implements Listener
|
||||
{
|
||||
private final BetterAlias plugin;
|
||||
|
||||
public BetterAliasPlayerListener(BetterAlias plugin)
|
||||
{
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e)
|
||||
{
|
||||
String sCommand = e.getMessage().substring(0,e.getMessage().indexOf(" "));
|
||||
Alias alias = plugin.aliasManager.getAlias(sCommand);
|
||||
|
||||
if(alias != null)
|
||||
{
|
||||
Player player = e.getPlayer();
|
||||
|
||||
if(alias.hasPermission()
|
||||
&& (!player.hasPermission("betteralias."+alias.getPermissionNode())
|
||||
|| player.isOp()))
|
||||
{
|
||||
player.sendMessage(ChatColor.RED+"You do not have permission to use this command.");
|
||||
}
|
||||
else
|
||||
{
|
||||
if(plugin.aliasManager.sendAliasCommands(
|
||||
alias,
|
||||
(CommandSender) e.getPlayer(),
|
||||
e.getMessage().substring(e.getMessage().indexOf(" ")+1)))
|
||||
{
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onConsoleCommand(ServerCommandEvent e)
|
||||
{
|
||||
Alias alias;
|
||||
String sCommand = null;
|
||||
|
||||
if(e.getCommand().contains(" "))
|
||||
{
|
||||
sCommand = e.getCommand().substring(e.getCommand().indexOf(" ")+1);
|
||||
|
||||
alias = plugin.aliasManager.getAlias(e.getCommand().substring(0,e.getCommand().indexOf(" ")));
|
||||
}
|
||||
else
|
||||
{
|
||||
alias = plugin.aliasManager.getAlias(e.getCommand());
|
||||
|
||||
sCommand = "";
|
||||
}
|
||||
|
||||
if(alias != null)
|
||||
{
|
||||
if(plugin.aliasManager.sendAliasCommands(alias,e.getSender(),sCommand))
|
||||
{
|
||||
e.setCommand("badonothing");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.ne0nx3r0.betteralias.listener.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;
|
||||
|
||||
public class BetterAliasCommandExecutor implements CommandExecutor
|
||||
{
|
||||
private final BetterAlias plugin;
|
||||
|
||||
public BetterAliasCommandExecutor(BetterAlias plugin)
|
||||
{
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender cs, Command cmnd, String alias, String[] args)
|
||||
{
|
||||
if(cmnd.getName().equalsIgnoreCase("badonothing"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if(cs.hasPermission("BetterAlias.reload") || cs.isOp())
|
||||
{
|
||||
cs.sendMessage(ChatColor.GOLD+"Reloading aliases...");
|
||||
|
||||
if(plugin.aliasManager.loadAliases())
|
||||
{
|
||||
cs.sendMessage(ChatColor.GOLD+"Aliases reloaded!");
|
||||
}
|
||||
else
|
||||
{
|
||||
cs.sendMessage(ChatColor.RED+"An error occurred while reloading aliases!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cs.sendMessage(ChatColor.RED+"You do not have permission to use /bareload (node: BetterAlias.reload)");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +1,13 @@
|
||||
name: BetterAlias
|
||||
main: com.gmail.Ne0nx3r0.BetterAlias
|
||||
version: '0.5'
|
||||
main: com.ne0nx3r0.betteralias.BetterAlias
|
||||
version: '2.0'
|
||||
database: false
|
||||
description: Command alias system
|
||||
commands:
|
||||
bareload:
|
||||
description: Reloads the aliases.yml file
|
||||
description: Reloads the aliases.yml file
|
||||
badonothing:
|
||||
description: Filler command, since console commands are not cancellable
|
||||
permissions:
|
||||
BetterAlias.reload:
|
||||
description: Allows reloading BetterAlias
|
Loading…
Reference in New Issue
Block a user