This commit is contained in:
Jan Holcak 2016-08-16 21:21:10 +00:00 committed by GitHub
commit 4e36ebf9e8
17 changed files with 631 additions and 293 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Ignore everything
/*
# But not these files...
!/.gitignore
!/pom.xml
!/src
!/src/**

21
README.md Normal file
View File

@ -0,0 +1,21 @@
# BetterAlias
"Smart" alias system for Bukkit commands
Links:
======
- [Bukkit](https://dev.bukkit.org/bukkit-plugins/betteralias/)
Build
=====
1. git clone https://github.com/Ne0nx3r0/BetterAlias.git
2. cd BetterAlias
3. mvn install
4. cp target/BetterAlias*.jar {$server_dir}/plugins/BetterAlias.jar
5. run server
Installation
============
1. Add BetterAlias.jar to your plugins directory
2. Once loaded an aliases.yml file will be created
3. Customize the file and use /bareload to enable the changes.
4. Use the examples inside aliases.yml to setup aliases for everyone to use! (or add a custom permission node for the command)

View File

@ -1,62 +0,0 @@
package com.ne0nx3r0.betteralias.alias;
import java.util.HashMap;
import java.util.List;
public class Alias
{
public final String command;
public final boolean caseSensitive;
private final String permission;
private final HashMap<Integer, List<AliasCommand>> parameters;
public Alias(String commandName,boolean caseSensitive,String permissionNode)
{
this.caseSensitive = caseSensitive;
if(this.caseSensitive)
{
this.command = commandName;
}
else
{
this.command = commandName.toLowerCase();
}
this.permission = permissionNode;
this.parameters = new HashMap<Integer,List<AliasCommand>>();
}
public boolean hasCommandFor(int length)
{
return this.parameters.containsKey(length) || this.parameters.containsKey(-1);
}
public String getPermissionNode()
{
return this.permission;
}
public boolean hasPermission()
{
return this.permission != null;
}
Iterable<AliasCommand> getCommands(int length)
{
List<AliasCommand> commands = this.parameters.get(length);
if(commands != null)
{
return commands;
}
return this.parameters.get(-1);
}
void setCommandsFor(int length,List<AliasCommand> commandsList)
{
this.parameters.put(length, commandsList);
}
}

View File

@ -1,60 +0,0 @@
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;
import java.util.Collection;
import java.util.List;
public class BetterAliasCommandListener implements Listener {
private final BetterAlias plugin;
public BetterAliasCommandListener(BetterAlias plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
String sCommand = e.getMessage().substring(1);
for(Alias alias : plugin.aliasManager.getAliasMatches(sCommand)){
String sArgs = sCommand.substring(alias.command.length());
Player player = e.getPlayer();
String sNode = "betteralias." + alias.getPermissionNode();
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);
e.setCancelled(true);
} else {
if (plugin.aliasManager.sendAliasCommands(alias, (CommandSender) e.getPlayer(), sArgs)) {
e.setCancelled(true);
}
}
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onConsoleCommand(ServerCommandEvent e) {
String sCommand = e.getCommand();
for(Alias alias : plugin.aliasManager.getAliasMatches(sCommand)){
String sArgs = sCommand.substring(alias.command.length());
if (plugin.aliasManager.sendAliasCommands(alias, e.getSender(), sArgs)) {
e.setCommand("bareload donothing");
}
}
}
}

48
pom.xml Normal file
View File

@ -0,0 +1,48 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ne0nx3r0.betteralias</groupId>
<artifactId>BetterAlias</artifactId>
<version>1.5.4</version>
<name>BetterAlias</name>
<description>Command alias system</description>
<url>https://dev.bukkit.org/bukkit-plugins/betteralias/</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<version>3.5.1</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<!--Spigot API-->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.10.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--Bukkit API-->
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.10.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>

View File

@ -1,23 +1,42 @@
package com.ne0nx3r0.betteralias;
import java.util.logging.Level;
import org.bukkit.plugin.java.JavaPlugin;
import com.ne0nx3r0.betteralias.alias.AliasManager;
import com.ne0nx3r0.betteralias.command.BetterAliasCommandExecutor;
import com.ne0nx3r0.betteralias.config.PluginConfig;
import com.ne0nx3r0.betteralias.listener.BetterAliasCommandListener;
import org.bukkit.plugin.java.JavaPlugin;
public class BetterAlias extends JavaPlugin
{
public class BetterAlias extends JavaPlugin {
public AliasManager aliasManager;
public PluginConfig config;
@Override
public void onEnable()
{
public void onEnable() {
this.config = new PluginConfig(this);
if (this.config.isDebuggingAllowed() == true) {
String version = this.getDescription().getVersion();
this.getLogger().log(Level.INFO, "Debugging enabled by config on version: " + version);
}
this.aliasManager = new AliasManager(this);
this.getServer().getPluginManager().registerEvents(new BetterAliasCommandListener(this), this);
BetterAliasCommandExecutor betterAliasCommandExecutor = new BetterAliasCommandExecutor(this);
this.getCommand("bareload").setExecutor(betterAliasCommandExecutor);
}
public boolean reload() {
if (aliasManager.loadAliases() && this.config.reload()) {
return true;
}
return false;
}
}

View File

@ -0,0 +1,86 @@
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, String priority) {
this.caseSensitive = caseSensitive;
if (this.caseSensitive) {
this.command = commandName;
} else {
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;
}
// get commands where length is number of arguments
public boolean hasCommandFor(int length) {
return this.parameters.containsKey(length) || this.parameters.containsKey(-1);
}
public String getPermissionNode() {
return this.permission;
}
public boolean hasPermission() {
return this.permission != null;
}
Iterable<AliasCommand> getCommands(int length) {
List<AliasCommand> commands = this.parameters.get(length);
if (commands != null) {
return commands;
}
return this.parameters.get(-1);
}
// organize aliases by number of arguments
public void setCommandsFor(int length,List<AliasCommand> commandsList) {
this.parameters.put(length, commandsList);
}
}

View File

@ -6,13 +6,11 @@ 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;
if(waitTime > 0)
{
if (waitTime > 0) {
this.waitTime = waitTime;
}
}

View File

@ -1,7 +1,6 @@
package com.ne0nx3r0.betteralias.alias;
public enum AliasCommandTypes
{
public enum AliasCommandTypes {
PLAYER,
PLAYER_AS_OP,
CONSOLE,

View File

@ -1,144 +1,56 @@
package com.ne0nx3r0.betteralias.alias;
import com.ne0nx3r0.betteralias.BetterAlias;
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 java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
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.entity.Player;
import org.bukkit.event.EventPriority;
import com.ne0nx3r0.betteralias.BetterAlias;
import com.ne0nx3r0.betteralias.config.AliasConfig;
// Helper methods
public class AliasManager {
private static BetterAlias plugin;
private HashMap<String, Alias> aliases;
private AliasConfig config;
public AliasManager(BetterAlias plugin) {
AliasManager.plugin = plugin;
this.loadAliases();
this.config = new AliasConfig(plugin);
// TODO resolve error when no aliases loaded
this.aliases = this.config.loadAliases();
}
public final boolean loadAliases() {
this.aliases = new HashMap<String, Alias>();
public boolean loadAliases() {
File configFile = new File(plugin.getDataFolder(), "aliases.yml");
this.config.reload();
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");
this.aliases = this.config.loadAliases();
if (aliases.isEmpty()) {
return false;
}
for (String sAlias : aliasList) {
Alias alias = new Alias(
sAlias,
yml.getBoolean(sAlias + ".caseSensitive", false),
yml.getString(sAlias + ".permission", null));
for (String sArg : yml.getConfigurationSection(sAlias).getKeys(false)) {
List<AliasCommand> commandsList = new ArrayList<AliasCommand>();
if (!sArg.equalsIgnoreCase("permission") && !sArg.equalsIgnoreCase("caseSensitive")) {
int iArg;
if (sArg.equals("*")) {
iArg = -1;
} else {
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) {
AliasCommandTypes type = AliasCommandTypes.PLAYER;
int waitTime = 0;
if (sArgLine.contains(" ")) {
String sType = sArgLine.substring(0, sArgLine.indexOf(" "));
if (sType.equalsIgnoreCase("console")) {
type = AliasCommandTypes.CONSOLE;
sArgLine = sArgLine.substring(sArgLine.indexOf(" ") + 1);
} else if (sType.equalsIgnoreCase("player_as_op")) {
type = AliasCommandTypes.PLAYER_AS_OP;
sArgLine = sArgLine.substring(sArgLine.indexOf(" ") + 1);
} else if (sType.equalsIgnoreCase("reply")) {
type = AliasCommandTypes.REPLY_MESSAGE;
sArgLine = sArgLine.substring(sArgLine.indexOf(" ") + 1);
} else if (sType.equalsIgnoreCase("wait")) {
String[] sArgLineParams = sArgLine.split(" ");
try {
waitTime = Integer.parseInt(sArgLineParams[1]);
} catch (Exception e) {
plugin.getLogger().log(Level.WARNING, "Invalid wait time for command {0} in alias {1}, skipping line",
new Object[]{sArgLine, sAlias});
continue;
}
if (sArgLineParams[2].equalsIgnoreCase("reply")) {
type = AliasCommandTypes.WAIT_THEN_REPLY;
sArgLine = sArgLine.replace(sArgLineParams[0] + " " + sArgLineParams[1] + " " + sArgLineParams[2] + " ", "");
} else if (sArgLineParams[2].equalsIgnoreCase("console")) {
type = AliasCommandTypes.WAIT_THEN_CONSOLE;
sArgLine = sArgLine.replace(sArgLineParams[0] + " " + sArgLineParams[1] + " " + sArgLineParams[2] + " ", "");
} else {
type = AliasCommandTypes.WAIT;
sArgLine = sArgLine.replace(sArgLineParams[0] + " " + sArgLineParams[1] + " ", "");
}
}
}
sArgLine = this.replaceColorCodes(sArgLine);
commandsList.add(new AliasCommand(sArgLine, type, waitTime));
}
alias.setCommandsFor(iArg, commandsList);
}
}
this.aliases.put(sAlias, alias);
}
return true;
}
public boolean sendAliasCommands(Alias alias, CommandSender cs, String commandString) {
return sendAliasCommands(alias, cs, commandString, "");
}
public boolean sendAliasCommands(Alias alias, CommandSender cs, String commandString, String commandPrefix) {
Player player = null;
if (cs instanceof Player) {
@ -187,7 +99,7 @@ public class AliasManager {
}
} else if (text.equalsIgnoreCase("handItemName")) {
if (player != null) {
text = player.getItemInHand().getType().name();
text = player.getInventory().getItemInMainHand().getType().name();
} else {
cs.sendMessage("[BetterAlias] " + ChatColor.RED + "A parameter of this alias requires a player.");
@ -197,7 +109,7 @@ public class AliasManager {
}
} else if (text.equalsIgnoreCase("handItemID")) {
if (player != null) {
text = new Integer(player.getItemInHand().getTypeId()).toString();
text = new Integer(player.getInventory().getItemInMainHand().getTypeId()).toString();
} else {
cs.sendMessage("[BetterAlias] " + ChatColor.RED + "A parameter of this alias requires a player.");
@ -217,7 +129,7 @@ public class AliasManager {
}
} else if (text.equalsIgnoreCase("*")) {
//ltrim emulation
while(commandString.length() > 0 && commandString.substring(0,1).equals(" ")){
while (commandString.length() > 0 && commandString.substring(0,1).equals(" ")) {
commandString = commandString.substring(1);
}
@ -279,17 +191,17 @@ public class AliasManager {
}, ac.waitTime);
} else if (ac.type.equals(AliasCommandTypes.WAIT_THEN_CONSOLE)) {
if (player != null) {
plugin.getServer().getScheduler().runTaskLater(plugin, new waitConsoleCommand(sNewCommand.substring(1),
plugin.getServer().getScheduler().runTaskLater(plugin, new waitConsoleCommand(commandPrefix + 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),
plugin.getServer().getScheduler().runTaskLater(plugin, new waitConsoleCommand(commandPrefix + 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),
plugin.getServer().getScheduler().runTaskLater(plugin, new waitConsoleCommand(commandPrefix + sNewCommand.substring(1),
"[BetterAlias] " + ChatColor.AQUA + "Running: " + sNewCommand), ac.waitTime);
}
} else if (ac.type.equals(AliasCommandTypes.PLAYER_AS_OP) && player != null) {
@ -302,14 +214,14 @@ public class AliasManager {
if (player.isOp() == false) {
try {
player.setOp(true);
AliasManager.plugin.getServer().dispatchCommand(player, sNewCommand.substring(1));
AliasManager.plugin.getServer().dispatchCommand(player, commandPrefix + sNewCommand.substring(1));
} catch (Exception e) {
e.printStackTrace();
} finally {
player.setOp(false);
}
} else {
AliasManager.plugin.getServer().dispatchCommand(player, sNewCommand.substring(1));
AliasManager.plugin.getServer().dispatchCommand(player, commandPrefix + sNewCommand.substring(1));
}
} else if (ac.type.equals(AliasCommandTypes.CONSOLE)
|| player == null) {
@ -321,7 +233,7 @@ public class AliasManager {
cs.sendMessage("[BetterAlias] " + ChatColor.AQUA + "Running: " + sNewCommand);
}
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), sNewCommand.substring(1));
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), commandPrefix + sNewCommand.substring(1));
} else {
player.chat(sNewCommand);
}
@ -333,18 +245,21 @@ public class AliasManager {
return false;
}
public Collection<Alias> getAliasMatches(String sCommand) {
String sCommandLower = sCommand.toLowerCase()+" ";
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 != null && priority != alias.getPriority()) {
continue;
}
if (alias.caseSensitive) {
if (sCommand.startsWith(alias.command+" ")) {
if (sCommand.startsWith(alias.command + " ")) {
aliasMatches.add(alias);
}
}
else if (sCommandLower.startsWith(alias.command+" ")) {
} else if (sCommandLower.startsWith(alias.command + " ")) {
aliasMatches.add(alias);
}
}
@ -384,27 +299,4 @@ public class AliasManager {
}
}
}
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,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;
@ -23,7 +24,7 @@ public class BetterAliasCommandExecutor implements CommandExecutor {
cs.sendMessage(ChatColor.GOLD + "Reloading aliases...");
if (plugin.aliasManager.loadAliases()) {
if (plugin.reload()) {
cs.sendMessage(ChatColor.GOLD + "Aliases reloaded!");
} else {
cs.sendMessage(ChatColor.RED + "An error occurred while reloading aliases!");
@ -34,5 +35,4 @@ public class BetterAliasCommandExecutor implements CommandExecutor {
return true;
}
}

View File

@ -0,0 +1,179 @@
package com.ne0nx3r0.betteralias.config;
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 org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import com.ne0nx3r0.betteralias.BetterAlias;
import com.ne0nx3r0.betteralias.alias.Alias;
import com.ne0nx3r0.betteralias.alias.AliasCommand;
import com.ne0nx3r0.betteralias.alias.AliasCommandTypes;
public class AliasConfig {
private final Plugin plugin;
private final File configurationFile;
private static FileConfiguration configuration;
public AliasConfig(final BetterAlias plugin) {
this.plugin = plugin;
this.configurationFile = new File(plugin.getDataFolder(), "aliases.yml");
if (!configurationFile.exists()) {
configurationFile.getParentFile().mkdirs();
copy(plugin.getResource("aliases.yml"), configurationFile);
}
AliasConfig.configuration = YamlConfiguration.loadConfiguration(configurationFile);
}
public void reload() {
AliasConfig.configuration = YamlConfiguration.loadConfiguration(configurationFile);
}
public FileConfiguration getAliasConfiguration() {
return AliasConfig.configuration;
}
public final HashMap<String, Alias> loadAliases() {
HashMap<String, Alias> aliases = new HashMap<String, Alias>();
Set<String> aliasList = AliasConfig.configuration.getKeys(false);
if (aliasList.isEmpty()) {
plugin.getLogger().log(Level.WARNING, "No aliases found in aliases.yml");
return aliases;
}
for (String sAlias : aliasList) {
Alias alias = new Alias(
sAlias,
AliasConfig.configuration.getBoolean(sAlias + ".caseSensitive", false),
AliasConfig.configuration.getString(sAlias + ".permission", null),
AliasConfig.configuration.getString(sAlias + ".priority", null));
for (String sArg : AliasConfig.configuration.getConfigurationSection(sAlias).getKeys(false)) {
List<AliasCommand> commandsList = new ArrayList<AliasCommand>();
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);
}
List<String> sArgLines = new ArrayList<String>();
if (AliasConfig.configuration.isList(sAlias + "." + sArg)) {
sArgLines = AliasConfig.configuration.getStringList(sAlias + "." + sArg);
} else {
sArgLines.add(AliasConfig.configuration.getString(sAlias + "." + sArg));
}
for (String sArgLine : sArgLines) {
AliasCommandTypes type = AliasCommandTypes.PLAYER;
int waitTime = 0;
if (sArgLine.contains(" ")) {
String sType = sArgLine.substring(0, sArgLine.indexOf(" "));
if (sType.equalsIgnoreCase("console")) {
type = AliasCommandTypes.CONSOLE;
sArgLine = sArgLine.substring(sArgLine.indexOf(" ") + 1);
} else if (sType.equalsIgnoreCase("player_as_op")) {
type = AliasCommandTypes.PLAYER_AS_OP;
sArgLine = sArgLine.substring(sArgLine.indexOf(" ") + 1);
} else if (sType.equalsIgnoreCase("reply")) {
type = AliasCommandTypes.REPLY_MESSAGE;
sArgLine = sArgLine.substring(sArgLine.indexOf(" ") + 1);
} else if (sType.equalsIgnoreCase("wait")) {
String[] sArgLineParams = sArgLine.split(" ");
try {
waitTime = Integer.parseInt(sArgLineParams[1]);
} catch (Exception e) {
plugin.getLogger().log(Level.WARNING, "Invalid wait time for command {0} in alias {1}, skipping line",
new Object[]{sArgLine, sAlias});
continue;
}
if (sArgLineParams[2].equalsIgnoreCase("reply")) {
type = AliasCommandTypes.WAIT_THEN_REPLY;
sArgLine = sArgLine.replace(sArgLineParams[0] + " " + sArgLineParams[1] + " " + sArgLineParams[2] + " ", "");
} else if (sArgLineParams[2].equalsIgnoreCase("console")) {
type = AliasCommandTypes.WAIT_THEN_CONSOLE;
sArgLine = sArgLine.replace(sArgLineParams[0] + " " + sArgLineParams[1] + " " + sArgLineParams[2] + " ", "");
} else {
type = AliasCommandTypes.WAIT;
sArgLine = sArgLine.replace(sArgLineParams[0] + " " + sArgLineParams[1] + " ", "");
}
}
}
sArgLine = this.replaceColorCodes(sArgLine);
commandsList.add(new AliasCommand(sArgLine, type, waitTime));
}
alias.setCommandsFor(iArg, commandsList);
}
}
aliases.put(sAlias, alias);
}
return aliases;
}
private String replaceColorCodes(String str) {
for (ChatColor cc : ChatColor.values()) {
str = str.replace("&" + cc.name(), cc.toString());
}
return str;
}
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();
}
}
}

View File

@ -0,0 +1,37 @@
package com.ne0nx3r0.betteralias.config;
import java.io.File;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.YamlConfiguration;
import com.ne0nx3r0.betteralias.BetterAlias;
public class PluginConfig {
private static Configuration configuration;
private final File configurationFile;
public PluginConfig(final BetterAlias plugin) {
plugin.saveDefaultConfig();
PluginConfig.configuration = plugin.getConfig();
configurationFile = new File(plugin.getDataFolder(), "config.yml");
}
public boolean reload() {
PluginConfig.configuration = YamlConfiguration.loadConfiguration(configurationFile);
return !PluginConfig.configuration.getKeys(false).isEmpty();
}
public boolean isDebuggingAllowed() {
return PluginConfig.configuration.getBoolean("debug");
}
public boolean isCommandBlockAllowed() {
return PluginConfig.configuration.getBoolean("enableCommandBlocks");
}
}

View File

@ -0,0 +1,166 @@
package com.ne0nx3r0.betteralias.listener;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.CommandBlock;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import com.ne0nx3r0.betteralias.BetterAlias;
import com.ne0nx3r0.betteralias.alias.Alias;
public class BetterAliasCommandListener implements Listener {
private final BetterAlias plugin;
public BetterAliasCommandListener(BetterAlias plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
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);
if (this.plugin.config.isDebuggingAllowed() == true) {
this.plugin.getLogger().log(Level.INFO, "Player: " + e.getPlayer() + " issue command " + 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)) {
player.sendMessage(ChatColor.RED + "You do not have permission to use this alias.");
player.sendMessage(ChatColor.GRAY + "Node: " + sNode);
e.setCancelled(true);
} else {
if (plugin.aliasManager.sendAliasCommands(alias, (CommandSender) e.getPlayer(), sArgs)) {
e.setCancelled(true);
}
}
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
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();
if (this.plugin.config.isDebuggingAllowed() == true) {
this.plugin.getLogger().log(Level.INFO, "Server console: " + e.getSender() + " issue command " + sCommand);
}
for (Alias alias : plugin.aliasManager.getAliasMatches(sCommand, priority)) {
String sArgs = sCommand.substring(alias.command.length());
if (plugin.aliasManager.sendAliasCommands(alias, e.getSender(), sArgs)) {
//
e.setCommand("bareload donothing");
}
}
}
// Thanks to @DrOverbuild for this function
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void redstoneChanges(BlockRedstoneEvent e) {
if (this.plugin.config.isCommandBlockAllowed() == false) return;
Block block = e.getBlock();
if (e.getOldCurrent() == 0 && e.getNewCurrent() > 0) {
if (block != null) {
BlockState state = block.getState();
if (state != null) {
if (state instanceof CommandBlock) {
CommandBlock cb = (CommandBlock)state;
String sCommand = cb.getCommand();
String sName = cb.getName();
if (this.plugin.config.isDebuggingAllowed() == true) {
this.plugin.getLogger().log(Level.INFO, "CommandBlock: " + sName + " issue command " + sCommand);
}
for (Alias alias : plugin.aliasManager.getAliasMatches(sCommand, null)) {
String sArgs = sCommand.substring(alias.command.length());
// get location from block where commandblock is activated
int posX = block.getX();
int posY = block.getY();
int posZ = block.getZ();
// set execution location /execute @e[type=Player] x y z
String prefix = "execute @e[type=Player] " + posX + " " + posY + " " + posZ + " ";
ConsoleCommandSender sender = this.plugin.getServer().getConsoleSender();
plugin.aliasManager.sendAliasCommands(alias, sender, sArgs, prefix);
}
}
}
}
}
}
}

View File

@ -0,0 +1,7 @@
# Welcome in BetterAlias config
# Enable CommandBlocks
enableCommandBlocks: false
# Format for command to reload aliases
debug: false

View File

@ -1,6 +1,6 @@
name: BetterAlias
main: com.ne0nx3r0.betteralias.BetterAlias
version: '1.5.2'
version: '1.5.4'
database: false
description: Command alias system
commands: