mirror of
https://github.com/Ne0nx3r0/BetterAlias.git
synced 2025-01-24 22:01:18 +01:00
Initial commit
This commit is contained in:
commit
6f8083c634
16
aliases.yml
Normal file
16
aliases.yml
Normal file
@ -0,0 +1,16 @@
|
||||
#'dumb' parameter, will just use what you put in
|
||||
banhammer:
|
||||
1: /ban !1
|
||||
#name parameter is replaced with the person's name
|
||||
gmon:
|
||||
0: /gamemode !name 1
|
||||
gmoff:
|
||||
0: /gamemode !name 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
|
17
com/gmail/Ne0nx3r0/AliasManager/Alias.java
Normal file
17
com/gmail/Ne0nx3r0/AliasManager/Alias.java
Normal file
@ -0,0 +1,17 @@
|
||||
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<Integer,String[]> params = new HashMap<Integer,String[]>();
|
||||
|
||||
public Alias(Map<Integer,String[]> p){
|
||||
this.params = p;
|
||||
}
|
||||
|
||||
public String[] getCommands(int p){
|
||||
return this.params.get(p);
|
||||
}
|
||||
}
|
91
com/gmail/Ne0nx3r0/AliasManager/AliasManager.java
Normal file
91
com/gmail/Ne0nx3r0/AliasManager/AliasManager.java
Normal file
@ -0,0 +1,91 @@
|
||||
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.List;
|
||||
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;
|
||||
|
||||
// loadAliasesLocal();
|
||||
|
||||
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<Integer,String[]> shareMap = new HashMap<Integer,String[]>();
|
||||
|
||||
for(int i=0;i<10;i++){
|
||||
|
||||
if(yml.isList(sAlias+"."+i)){
|
||||
|
||||
shareMap.put(i,yml.getStringList(sAlias+"."+i).toArray(new String[]{}));
|
||||
|
||||
}else if(yml.isString(sAlias+"."+i)){
|
||||
|
||||
shareMap.put(i,new String[]{yml.getString(sAlias+"."+i)});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
44
com/gmail/Ne0nx3r0/BetterAlias.java
Normal file
44
com/gmail/Ne0nx3r0/BetterAlias.java
Normal file
@ -0,0 +1,44 @@
|
||||
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;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
//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("ba").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);
|
||||
}
|
||||
}
|
48
com/gmail/Ne0nx3r0/BetterAliasCommandExecutor.java
Normal file
48
com/gmail/Ne0nx3r0/BetterAliasCommandExecutor.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.gmail.Ne0nx3r0;
|
||||
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class BetterAliasCommandExecutor implements CommandExecutor {
|
||||
private BetterAlias plugin;
|
||||
|
||||
public BetterAliasCommandExecutor(BetterAlias plugin)
|
||||
{
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
|
||||
if (!(cs instanceof Player)){
|
||||
System.out.println("You can't run this from the console!");
|
||||
return false;
|
||||
}
|
||||
|
||||
Player player = (Player) cs;
|
||||
|
||||
if(cmd.getName().equalsIgnoreCase("to") && args.length == 1 && player.isOp()){
|
||||
String sPlayerName = args[0].toLowerCase();
|
||||
|
||||
for(Player p : plugin.getServer().getOnlinePlayers()){
|
||||
if(p.getName().toLowerCase().contains(sPlayerName)){
|
||||
player.chat("/tp "+player.getName()+" "+p.getName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(cmd.getName().equalsIgnoreCase("gm")){
|
||||
if(player.getGameMode() == GameMode.SURVIVAL){
|
||||
player.chat("/gamemode "+player.getName()+" 1");
|
||||
}else{
|
||||
player.chat("/gamemode "+player.getName()+" 0");
|
||||
}
|
||||
}
|
||||
else if(cmd.getName().equalsIgnoreCase("go") && args.length > 0){
|
||||
player.chat("/res tp "+args[0]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
24
com/gmail/Ne0nx3r0/BetterAliasCommandListener.java
Normal file
24
com/gmail/Ne0nx3r0/BetterAliasCommandListener.java
Normal file
@ -0,0 +1,24 @@
|
||||
//TODO: remove if not implemented
|
||||
|
||||
package com.gmail.Ne0nx3r0;
|
||||
|
||||
import com.gmail.Ne0nx3r0.AliasManager.AliasManager;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
class BetterAliasCommandListener implements Listener{
|
||||
private final BetterAlias plugin;
|
||||
private final AliasManager aliasManager;
|
||||
|
||||
BetterAliasCommandListener(BetterAlias p, AliasManager am) {
|
||||
this.plugin = p;
|
||||
this.aliasManager = am;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onServerCommandEvent(CommandSender sender, String command){
|
||||
System.out.println(sender);
|
||||
System.out.println(command);
|
||||
}
|
||||
}
|
90
com/gmail/Ne0nx3r0/BetterAliasPlayerListener.java
Normal file
90
com/gmail/Ne0nx3r0/BetterAliasPlayerListener.java
Normal file
@ -0,0 +1,90 @@
|
||||
package com.gmail.Ne0nx3r0;
|
||||
|
||||
import com.gmail.Ne0nx3r0.AliasManager.AliasManager;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
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
|
||||
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("name")){
|
||||
text = player.getName();
|
||||
}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];
|
||||
|
||||
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 >= iParam){
|
||||
text = cmd[iParam];
|
||||
}
|
||||
}
|
||||
//else if is numeric and cmd[i] exists, replace with cmd[i]
|
||||
|
||||
m.appendReplacement(sb, Matcher.quoteReplacement(text));
|
||||
}
|
||||
m.appendTail(sb);
|
||||
|
||||
//tp !name !~1
|
||||
//tp !1 !2
|
||||
//tp !~1 !~2
|
||||
//debug: player.sendMessage(sb.toString());
|
||||
player.chat(sb.toString());
|
||||
}
|
||||
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
5
plugin.yml
Normal file
5
plugin.yml
Normal file
@ -0,0 +1,5 @@
|
||||
name: BetterAlias
|
||||
main: com.gmail.Ne0nx3r0.BetterAlias
|
||||
version: '0.1'
|
||||
database: false
|
||||
description: Command alias system
|
Loading…
Reference in New Issue
Block a user