Adding wait times

This commit is contained in:
Ne0n x3r0 2013-04-09 19:49:06 -05:00
parent 404f460b4c
commit e221ccb7f3
5 changed files with 558 additions and 382 deletions

View File

@ -30,6 +30,10 @@ hand:
handid: handid:
0: /give !name !handItemID 0: /give !name !handItemID
# Toggle the current player's game mode from survival to creative
gm:
0: /gamemode !oppositeGameMode !name
# Send everything with a command # Send everything with a command
sendAll: sendAll:
"*": /say !* "*": /say !*
@ -70,4 +74,28 @@ makeop:
- console /op !1 - console /op !1
- /me has opped !1 - /me has opped !1
2: console /say testing single line 2: console /say testing single line
3: reply testing single line 3: reply testing single line
# Wait example
wait:
# Quotes are required because 0 uses a colon (:)
0:
- wait 80 /me thinks this message will come third
- wait 40 /me thinks this message will come second
- /me thinks this message will come first
# wait then reply example
waitreply:
# Quotes are required because 0 uses a colon (:)
0:
- wait 80 reply this came third!
- wait 40 reply this came second!
- reply this came first!
# Wait then console example
waitconsole:
# Quotes are required because 0 uses a colon (:)
0:
- wait 40 console /say this message was delayed!
- wait 80 console /say this message was delayed longer!
- console /say this message came first!

View File

@ -1,13 +1,19 @@
package com.ne0nx3r0.betteralias.alias; package com.ne0nx3r0.betteralias.alias;
public class AliasCommand public class AliasCommand
{ {
final String command; final String command;
final AliasCommandTypes type; final AliasCommandTypes type;
int waitTime;
public AliasCommand(String command,AliasCommandTypes type)
{ public AliasCommand(String command,AliasCommandTypes type,int waitTime)
this.command = command; {
this.type = type; this.command = command;
} this.type = type;
}
if(waitTime > 0)
{
this.waitTime = waitTime;
}
}
}

View File

@ -1,10 +1,13 @@
package com.ne0nx3r0.betteralias.alias; package com.ne0nx3r0.betteralias.alias;
public enum AliasCommandTypes public enum AliasCommandTypes
{ {
PLAYER, PLAYER,
CONSOLE, CONSOLE,
REPLY_MESSAGE; REPLY_MESSAGE,
WAIT,
public int id; WAIT_THEN_CONSOLE,
} WAIT_THEN_REPLY;
public int id;
}

View File

@ -1,348 +1,487 @@
package com.ne0nx3r0.betteralias.alias; package com.ne0nx3r0.betteralias.alias;
import com.ne0nx3r0.betteralias.BetterAlias; import com.ne0nx3r0.betteralias.BetterAlias;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.GameMode;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
public class AliasManager
{
private final BetterAlias plugin; // Helper methods
private HashMap<String, Alias> aliases; public class AliasManager
{
public AliasManager(BetterAlias plugin) private static BetterAlias plugin;
{ private HashMap<String, Alias> aliases;
this.plugin = plugin;
public AliasManager(BetterAlias plugin)
this.loadAliases(); {
} AliasManager.plugin = plugin;
public final boolean loadAliases() this.loadAliases();
{ }
this.aliases = new HashMap<String,Alias>();
public final boolean loadAliases()
File configFile = new File(plugin.getDataFolder(), "aliases.yml"); {
this.aliases = new HashMap<String,Alias>();
if(!configFile.exists())
{ File configFile = new File(plugin.getDataFolder(), "aliases.yml");
configFile.getParentFile().mkdirs();
copy(plugin.getResource("aliases.yml"), configFile); if(!configFile.exists())
} {
configFile.getParentFile().mkdirs();
FileConfiguration yml = YamlConfiguration.loadConfiguration(configFile); copy(plugin.getResource("aliases.yml"), configFile);
}
Set<String> aliasList = yml.getKeys(false);
FileConfiguration yml = YamlConfiguration.loadConfiguration(configFile);
if(aliasList.isEmpty())
{ Set<String> aliasList = yml.getKeys(false);
plugin.getLogger().log(Level.WARNING, "No aliases found in aliases.yml");
if(aliasList.isEmpty())
return false; {
} plugin.getLogger().log(Level.WARNING, "No aliases found in aliases.yml");
for(String sAlias : aliasList) return false;
{ }
Alias alias;
for(String sAlias : aliasList)
if(yml.getString(sAlias+".permission") != null) {
{ Alias alias;
alias = new Alias(sAlias,yml.getString(sAlias+".permission"));
} if(yml.getString(sAlias+".permission") != null)
else {
{ alias = new Alias(sAlias,yml.getString(sAlias+".permission"));
alias = new Alias(sAlias); }
} else
{
for(String sArg : yml.getConfigurationSection(sAlias).getKeys(false)) alias = new Alias(sAlias);
{ }
List<AliasCommand> commandsList = new ArrayList<AliasCommand>();
for(String sArg : yml.getConfigurationSection(sAlias).getKeys(false))
if(!sArg.equalsIgnoreCase("permission")) {
{ List<AliasCommand> commandsList = new ArrayList<AliasCommand>();
int iArg;
if(!sArg.equalsIgnoreCase("permission"))
if(sArg.equals("*")) {
{ int iArg;
iArg = -1;
} if(sArg.equals("*"))
else {
{ iArg = -1;
iArg = Integer.parseInt(sArg); }
} else
{
List<String> sArgLines = new ArrayList<String>(); iArg = Integer.parseInt(sArg);
}
if(yml.isList(sAlias+"."+sArg))
{ List<String> sArgLines = new ArrayList<String>();
sArgLines = yml.getStringList(sAlias+"."+sArg);
} if(yml.isList(sAlias+"."+sArg))
else {
{ sArgLines = yml.getStringList(sAlias+"."+sArg);
sArgLines.add(yml.getString(sAlias+"."+sArg)); }
} else
{
for(String sArgLine : sArgLines) sArgLines.add(yml.getString(sAlias+"."+sArg));
{ }
AliasCommandTypes type = AliasCommandTypes.PLAYER;
for(String sArgLine : sArgLines)
if(sArgLine.contains(" ")) {
{ AliasCommandTypes type = AliasCommandTypes.PLAYER;
String sType = sArgLine.substring(0,sArgLine.indexOf(" "));
int waitTime = 0;
if(sType.equalsIgnoreCase("console"))
{ if(sArgLine.contains(" "))
type = AliasCommandTypes.CONSOLE; {
String sType = sArgLine.substring(0,sArgLine.indexOf(" "));
sArgLine = sArgLine.substring(sArgLine.indexOf(" ")+1);
} if(sType.equalsIgnoreCase("console"))
else if(sType.equalsIgnoreCase("reply")) {
{ type = AliasCommandTypes.CONSOLE;
type = AliasCommandTypes.REPLY_MESSAGE;
sArgLine = sArgLine.substring(sArgLine.indexOf(" ")+1);
sArgLine = sArgLine.substring(sArgLine.indexOf(" ")+1); }
} else if(sType.equalsIgnoreCase("reply"))
} {
type = AliasCommandTypes.REPLY_MESSAGE;
sArgLine = this.replaceColorCodes(sArgLine);
sArgLine = sArgLine.substring(sArgLine.indexOf(" ")+1);
commandsList.add(new AliasCommand(sArgLine,type)); }
} else if(sType.equalsIgnoreCase("wait"))
{
alias.setCommandsFor(iArg,commandsList); String[] sArgLineParams = sArgLine.split(" ");
}
} try
{
this.aliases.put(sAlias, alias); waitTime = Integer.parseInt(sArgLineParams[1]);
} }
catch(Exception e)
return true; {
} plugin.getLogger().log(Level.WARNING, "Invalid wait time for command {0} in alias {1}, skipping line",
new Object[]{sArgLine, sAlias});
public Alias getAlias(String sCommand)
{ continue;
return this.aliases.get(sCommand); }
}
if(sArgLineParams[2].equalsIgnoreCase("reply"))
public boolean sendAliasCommands(Alias alias,CommandSender cs, String commandString) {
{ type = AliasCommandTypes.WAIT_THEN_REPLY;
Player player = null;
sArgLine = sArgLine.replace(sArgLineParams[0]+" "+sArgLineParams[1]+" "+sArgLineParams[2]+" ", "");
if(cs instanceof Player) }
{ else if(sArgLineParams[2].equalsIgnoreCase("console"))
player = (Player) cs; {
} type = AliasCommandTypes.WAIT_THEN_CONSOLE;
List<String> args = new ArrayList<String>(); sArgLine = sArgLine.replace(sArgLineParams[0]+" "+sArgLineParams[1]+" "+sArgLineParams[2]+" ", "");
}
Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'"); else
{
Matcher regexMatcher = regex.matcher(commandString); type = AliasCommandTypes.WAIT;
while(regexMatcher.find()) sArgLine = sArgLine.replace(sArgLineParams[0]+" "+sArgLineParams[1]+" ", "");
{ }
if(regexMatcher.group(1) != null) }
{ }
// Add double-quoted string without the quotes
args.add(regexMatcher.group(1)); sArgLine = this.replaceColorCodes(sArgLine);
}
else if (regexMatcher.group(2) != null) commandsList.add(new AliasCommand(sArgLine,type,waitTime));
{ }
// Add single-quoted string without the quotes
args.add(regexMatcher.group(2)); alias.setCommandsFor(iArg,commandsList);
} }
else }
{
// Add unquoted word this.aliases.put(sAlias, alias);
args.add(regexMatcher.group()); }
}
} return true;
}
if(alias.hasCommandFor(args.size()))
{ public Alias getAlias(String sCommand)
for(AliasCommand ac : alias.getCommands(args.size())) {
{ return this.aliases.get(sCommand);
String sAliasCommand = ac.command; }
Matcher m = Pattern.compile("!([0-9a-zA-Z~*]+)").matcher(sAliasCommand); public boolean sendAliasCommands(Alias alias,CommandSender cs, String commandString)
{
StringBuffer sb = new StringBuffer(sAliasCommand.length()); Player player = null;
while(m.find()) if(cs instanceof Player)
{ {
String text = m.group(0).substring(1); player = (Player) cs;
}
if(text.equalsIgnoreCase("name"))
{ List<String> args = new ArrayList<String>();
if(player != null)
{ Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'");
text = player.getName();
} Matcher regexMatcher = regex.matcher(commandString);
else
{ while(regexMatcher.find())
cs.sendMessage("[BetterAlias] "+ChatColor.RED+"A parameter of this alias requires a player."); {
if(regexMatcher.group(1) != null)
cs.sendMessage("[BetterAlias] Line: "+ac.command); {
// Add double-quoted string without the quotes
return true; args.add(regexMatcher.group(1));
} }
} else if (regexMatcher.group(2) != null)
else if(text.equalsIgnoreCase("handItemName")) {
{ // Add single-quoted string without the quotes
if(player != null) args.add(regexMatcher.group(2));
{ }
text = player.getItemInHand().getType().name(); else
} {
else // Add unquoted word
{ args.add(regexMatcher.group());
cs.sendMessage("[BetterAlias] "+ChatColor.RED+"A parameter of this alias requires a player."); }
}
cs.sendMessage("[BetterAlias] Line: "+ac.command);
if(alias.hasCommandFor(args.size()))
return true; {
} for(AliasCommand ac : alias.getCommands(args.size()))
} {
else if(text.equalsIgnoreCase("handItemID")) String sAliasCommand = ac.command;
{
if(player != null) Matcher m = Pattern.compile("!([0-9a-zA-Z~*]+)").matcher(sAliasCommand);
{
text = new Integer(player.getItemInHand().getTypeId()).toString(); StringBuffer sb = new StringBuffer(sAliasCommand.length());
}
else while(m.find())
{ {
cs.sendMessage("[BetterAlias] "+ChatColor.RED+"A parameter of this alias requires a player."); String text = m.group(0).substring(1);
cs.sendMessage("[BetterAlias] Line: "+ac.command); if(text.equalsIgnoreCase("name"))
{
return true; if(player != null)
} {
} text = player.getName();
else if(text.equalsIgnoreCase("*")) }
{ else
text = commandString; {
} cs.sendMessage("[BetterAlias] "+ChatColor.RED+"A parameter of this alias requires a player.");
else if(text.length() >= 2 && text.substring(1,2).equalsIgnoreCase("p"))
{ cs.sendMessage("[BetterAlias] Line: "+ac.command);
int iParam = -1;
return true;
try }
{ }
iParam = Integer.parseInt(text.substring(0,1)); else if(text.equalsIgnoreCase("handItemName"))
} {
catch(Exception ex){} if(player != null)
{
if(iParam > -1 && args.size() >= iParam) text = player.getItemInHand().getType().name();
{ }
String sPlayerName = args.get(iParam-1).toLowerCase(); else
{
text = "BetterAliasPlayerNotFound"; cs.sendMessage("[BetterAlias] "+ChatColor.RED+"A parameter of this alias requires a player.");
for(Player p : plugin.getServer().getOnlinePlayers()) cs.sendMessage("[BetterAlias] Line: "+ac.command);
{
if(p.getName().toLowerCase().contains(sPlayerName)) return true;
{ }
text = p.getName(); }
else if(text.equalsIgnoreCase("handItemID"))
break; {
} if(player != null)
} {
} text = new Integer(player.getItemInHand().getTypeId()).toString();
} }
else else
{ {
int iParam = -1; cs.sendMessage("[BetterAlias] "+ChatColor.RED+"A parameter of this alias requires a player.");
try cs.sendMessage("[BetterAlias] Line: "+ac.command);
{
iParam = Integer.parseInt(text); return true;
} }
catch(Exception ex){} }
else if(text.equalsIgnoreCase("oppositeGameMode"))
if(iParam > -1 && args.size() >= iParam) {
{ if(player != null)
text = args.get(iParam-1); {
} text = player.getGameMode().equals(GameMode.SURVIVAL)?"creative":"survival";
else }
{ else
text = ""; {
} cs.sendMessage("[BetterAlias] "+ChatColor.RED+"A parameter of this alias requires a player.");
}
cs.sendMessage("[BetterAlias] Line: "+ac.command);
m.appendReplacement(sb, Matcher.quoteReplacement(text));
} return true;
}
m.appendTail(sb); }
else if(text.equalsIgnoreCase("*"))
String sNewCommand = sb.toString(); {
if(ac.type.equals(AliasCommandTypes.REPLY_MESSAGE)) text = commandString;
{ }
cs.sendMessage(sNewCommand); else if(text.length() >= 2 && text.substring(1,2).equalsIgnoreCase("p"))
} {
else if(ac.type.equals(AliasCommandTypes.CONSOLE) int iParam = -1;
|| player == null)
{ try
if(player != null) {
{ iParam = Integer.parseInt(text.substring(0,1));
plugin.getLogger().log(Level.INFO,"[BetterAlias] "+ChatColor.AQUA+"Running console command for "+player.getName()+": "+sNewCommand); }
} catch(Exception ex){}
else
{ if(iParam > -1 && args.size() >= iParam)
cs.sendMessage("[BetterAlias] "+ChatColor.AQUA+"Running: "+sNewCommand); {
} String sPlayerName = args.get(iParam-1).toLowerCase();
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), sNewCommand.substring(1)); text = "BetterAliasPlayerNotFound";
}
else for(Player p : plugin.getServer().getOnlinePlayers())
{ {
player.chat(sNewCommand); if(p.getName().toLowerCase().contains(sPlayerName))
} {
} text = p.getName();
return true; break;
} }
}
return false; }
} }
else
// Helper method {
public void copy(InputStream in, File file) int iParam = -1;
{
try try
{ {
OutputStream out = new FileOutputStream(file); iParam = Integer.parseInt(text);
byte[] buf = new byte[1024]; }
int len; catch(Exception ex){}
while((len=in.read(buf))>0)
{ if(iParam > -1 && args.size() >= iParam)
out.write(buf,0,len); {
} text = args.get(iParam-1);
out.close(); }
in.close(); else
} catch (Exception e) {
{ text = "";
e.printStackTrace(); }
} }
}
m.appendReplacement(sb, Matcher.quoteReplacement(text));
private String replaceColorCodes(String str) }
{
for(ChatColor cc : ChatColor.values()) m.appendTail(sb);
{
str = str.replace("&"+cc.name(), cc.toString()); String sNewCommand = sb.toString();
}
if(ac.type.equals(AliasCommandTypes.REPLY_MESSAGE))
return str; {
} cs.sendMessage(sNewCommand);
} }
else if(ac.type.equals(AliasCommandTypes.WAIT_THEN_REPLY))
{
final CommandSender csWait = cs;
final String message = sNewCommand;
plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable()
{
@Override
public void run()
{
csWait.sendMessage(message);
}
}, ac.waitTime);
}
else if(ac.type.equals(AliasCommandTypes.WAIT_THEN_CONSOLE))
{
if(player != null)
{
plugin.getServer().getScheduler().runTaskLater(plugin,new waitConsoleCommand(sNewCommand.substring(1),
"[BetterAlias] "+ChatColor.AQUA+"Running console command for "+player.getName()+": "+sNewCommand), ac.waitTime);
}
else
{
plugin.getServer().getScheduler().runTaskLater(plugin, new waitConsoleCommand(sNewCommand.substring(1),
"[BetterAlias] "+ChatColor.AQUA+"Running: "+sNewCommand), ac.waitTime);
}
}
else if(ac.type.equals(AliasCommandTypes.WAIT))
{
if(player != null)
{
plugin.getServer().getScheduler().runTaskLater(plugin, new waitPlayerCommand(sNewCommand,player.getName()), ac.waitTime);
}
else
{
plugin.getServer().getScheduler().runTaskLater(plugin, new waitConsoleCommand(sNewCommand.substring(1),
"[BetterAlias] "+ChatColor.AQUA+"Running: "+sNewCommand), ac.waitTime);
}
}
else if(ac.type.equals(AliasCommandTypes.CONSOLE)
|| player == null)
{
if(player != null)
{
plugin.getLogger().log(Level.INFO,
"[BetterAlias] {0}Running console command for {1}: {2}",
new Object[]{ChatColor.AQUA, player.getName(), sNewCommand});
}
else
{
cs.sendMessage("[BetterAlias] "+ChatColor.AQUA+"Running: "+sNewCommand);
}
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), sNewCommand.substring(1));
}
else
{
player.chat(sNewCommand);
}
}
return true;
}
return false;
}
// Delayed tasks
private static class waitConsoleCommand implements Runnable
{
private final String message;
private final String command;
public waitConsoleCommand(String command,String message)
{
this.message = message;
this.command = command;
}
@Override
public void run()
{
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), command);
}
}
private static class waitPlayerCommand implements Runnable
{
private final String playerName;
private final String command;
public waitPlayerCommand(String command,String playerName)
{
this.playerName = playerName;
this.command = command;
}
@Override
public void run()
{
Player p = plugin.getServer().getPlayer(playerName);
if(p != null)
{
p.chat(command);
}
}
}
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;
}
}

View File

@ -1,11 +1,11 @@
name: BetterAlias name: BetterAlias
main: com.ne0nx3r0.betteralias.BetterAlias main: com.ne0nx3r0.betteralias.BetterAlias
version: '1.1.0' version: '1.2'
database: false database: false
description: Command alias system description: Command alias system
commands: commands:
bareload: bareload:
description: Reloads the aliases.yml file description: Reloads the aliases.yml file
permissions: permissions:
BetterAlias.reload: BetterAlias.reload:
description: Allows reloading BetterAlias description: Allows reloading BetterAlias