diff --git a/plugin.yml b/plugin.yml index 5a39678..10bd229 100644 --- a/plugin.yml +++ b/plugin.yml @@ -3,3 +3,8 @@ version: 0.1 main: com.dre.managerxl.P authors: [Frank Baumann, Tobias Schmitz, Joscha Schmitz] softdepend: [Vault] +commands: + ban: + description: Ban a Player + unban: + description: Unban a Player diff --git a/src/com/dre/managerxl/LanguageReader.java b/src/com/dre/managerxl/LanguageReader.java index a49dcda..7126fdf 100644 --- a/src/com/dre/managerxl/LanguageReader.java +++ b/src/com/dre/managerxl/LanguageReader.java @@ -34,8 +34,34 @@ public LanguageReader(File file){ } private void setDefaults(){ + + /* Log */ + defaults.put("Log_PlayersSaved", "Spieler gespeichert"); + defaults.put("Log_PlayersLoaded", "Spieler geladen"); + defaults.put("Log_Error_NoConsoleCommand", "&6mxl &v1&4 kann man nicht als Konsole ausführen!"); + defaults.put("Log_Error_PlayersSaved", "Spieler konnten nicht gespeichert werden!"); + defaults.put("Log_Error_PlayersLoaded", "Spieler konnten nicht geladen werden!"); + + /* Help */ + defaults.put("Help_Ban", ""); + defaults.put("Help_UnBan", ""); + + /* Player */ + defaults.put("Player_Kick_Ban", "&4Du wurdest gebannt wegen &6&v1&4!"); + + /* CMDs */ + defaults.put("Cmd_Ban_Success", ""); + defaults.put("Cmd_Ban_DefaultReason", ""); + defaults.put("Cmd_UnBan_Success", ""); + /* Errors */ defaults.put("Error_NoPermissions","&4Du hast keine Erlaubnis dies zu tun!"); + defaults.put("Error_CmdBan_AlreadyBanned", ""); + defaults.put("Error_CmdBan_NotBanned", ""); + defaults.put("Error_NoPlayerCommand", "&6/mxl &v1&4 kann man nicht als Spieler ausführen!"); + defaults.put("Error_NoPermissions", "&4Du hast keine Erlaubnis dies zu tun!"); + defaults.put("Error_CmdNotExist1","&4Befehl &6&v1&4 existiert nicht!"); + defaults.put("Error_CmdNotExist2","&4Bitte gib &6/mxl help&4 für Hilfe ein!"); } private void check(){ @@ -80,8 +106,10 @@ public String get(String key, String... args){ if(entry!=null){ int i=0; for(String arg:args){ - i++; - entry = entry.replace("&v"+i, arg); + if(arg != null){ + i++; + entry = entry.replace("&v"+i, arg); + } } } diff --git a/src/com/dre/managerxl/MPlayer.java b/src/com/dre/managerxl/MPlayer.java new file mode 100644 index 0000000..7b467e3 --- /dev/null +++ b/src/com/dre/managerxl/MPlayer.java @@ -0,0 +1,114 @@ +package com.dre.managerxl; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; + +public class MPlayer { + private static Set mPlayers = new HashSet(); + + private String name; + public String getName() {return name;} + + public Player getPlayer(){ + if(this.isOnline){ + return P.p.getServer().getPlayer(this.name); + } + + return null; + } + + private boolean isOnline; + public boolean isOnline() {return isOnline;} + public void setOnline(boolean online) {isOnline = online;} + + private boolean isBanned; + public boolean isBanned() {return isBanned;} + public void setBanned(boolean banned){ + isBanned = banned; + + if(isBanned){ + if(isOnline()){ + getPlayer().kickPlayer(P.p.replaceColors(P.p.getLanguageReader().get("Player_Kick_Ban", this.getBannedReason()))); + } + } + } + + private int bannedTime; + public void setBannedTime(int bannedTime) {this.bannedTime = bannedTime;} + public int getBannedTime() {return bannedTime;} + + private String bannedReason; + public void setBannedReason(String bannedReason) {this.bannedReason = bannedReason;} + public String getBannedReason() {return bannedReason;} + + public MPlayer(String name){ + mPlayers.add(this); + + this.name = name; + } + + //Statics + public static Set get(){ + return mPlayers; + } + + public static MPlayer get(String name){ + for(MPlayer mPlayer : mPlayers){ + if(mPlayer.getName().equalsIgnoreCase(name)){ + return mPlayer; + } + } + + return null; + } + + public static MPlayer getOrCreate(String name){ + for(MPlayer mPlayer : mPlayers){ + if(mPlayer.getName().equalsIgnoreCase(name)){ + return mPlayer; + } + } + + return new MPlayer(name); + } + + //Save and Load Functions + public static boolean SaveAsYml(File file){ + FileConfiguration ymlFile = new YamlConfiguration(); + + for(MPlayer player : MPlayer.get()){ + ymlFile.set(player.getName()+".isBanned", player.isBanned()); + ymlFile.set(player.getName()+".bannedTime", player.getBannedTime()); + ymlFile.set(player.getName()+".bannedReason", player.getBannedReason()); + } + + try { + ymlFile.save(file); + } catch (IOException e) { + return false; + } + + return true; + } + + public static boolean LoadAsYml(File file){ + FileConfiguration ymlFile = YamlConfiguration.loadConfiguration(file); + + Set keys = ymlFile.getKeys(false); + + for(String name : keys){ + MPlayer mPlayer = new MPlayer(name); + mPlayer.setBanned(ymlFile.getBoolean(name+".isBanned")); + mPlayer.setBannedTime(ymlFile.getInt(name+".bannedTime")); + mPlayer.setBannedReason(ymlFile.getString(name+".bannedReason")); + } + + return true; + } +} diff --git a/src/com/dre/managerxl/P.java b/src/com/dre/managerxl/P.java index 9e6065e..22cf824 100644 --- a/src/com/dre/managerxl/P.java +++ b/src/com/dre/managerxl/P.java @@ -1,14 +1,23 @@ package com.dre.managerxl; import java.io.File; +import java.util.logging.Level; +import java.util.logging.Logger; +import net.milkbowl.vault.permission.Permission; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; +import com.dre.managerxl.commands.MCommand; +import com.dre.managerxl.listeners.PlayerListener; + public class P extends JavaPlugin{ public static P p; - - //Language Reader private LanguageReader languageReader; public LanguageReader getLanguageReader(){ @@ -22,10 +31,101 @@ public void onEnable(){ //Load LanguageReader languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/default.yml")); + + //Setup Permissions + setupPermissions(); + + // Init Listeners + Bukkit.getPluginManager().registerEvents(new PlayerListener(), this); + + //Setup Commands + MCommand.initCommands(); + + //Load + LoadAll(); } @Override public void onDisable(){ + //Save + SaveAll(); + } + + //Save and Load + public void SaveAll(){ + if(MPlayer.SaveAsYml(new File(this.getDataFolder(), "players.yml"))){ + P.p.log(getLanguageReader().get("Log_PlayersSaved")); + } else { + P.p.log(Level.WARNING, getLanguageReader().get("Log_Error_PlayersSaved")); + } + } + + public void LoadAll(){ + if(MPlayer.LoadAsYml(new File(this.getDataFolder(), "players.yml"))){ + P.p.log(getLanguageReader().get("Log_PlayersLoaded")); + } else { + P.p.log(Level.WARNING, getLanguageReader().get("Log_Error_PlayersLoaded")); + } + } + + //Msg + public void msg(CommandSender sender,String msg){ + msg = replaceColors(msg); + sender.sendMessage(msg); + } + + public String replaceColors(String msg){ + if (msg!=null) { + msg = msg.replace("&0", ChatColor.getByChar("0").toString()); + msg = msg.replace("&1", ChatColor.getByChar("1").toString()); + msg = msg.replace("&2", ChatColor.getByChar("2").toString()); + msg = msg.replace("&3", ChatColor.getByChar("3").toString()); + msg = msg.replace("&4", ChatColor.getByChar("4").toString()); + msg = msg.replace("&5", ChatColor.getByChar("5").toString()); + msg = msg.replace("&6", ChatColor.getByChar("6").toString()); + msg = msg.replace("&7", ChatColor.getByChar("7").toString()); + msg = msg.replace("&8", ChatColor.getByChar("8").toString()); + msg = msg.replace("&9", ChatColor.getByChar("9").toString()); + msg = msg.replace("&a", ChatColor.getByChar("a").toString()); + msg = msg.replace("&b", ChatColor.getByChar("b").toString()); + msg = msg.replace("&c", ChatColor.getByChar("c").toString()); + msg = msg.replace("&d", ChatColor.getByChar("d").toString()); + msg = msg.replace("&e", ChatColor.getByChar("e").toString()); + msg = msg.replace("&f", ChatColor.getByChar("f").toString()); + msg = msg.replace("&k", ChatColor.getByChar("k").toString()); + msg = msg.replace("&l", ChatColor.getByChar("l").toString()); + msg = msg.replace("&m", ChatColor.getByChar("m").toString()); + msg = msg.replace("&n", ChatColor.getByChar("n").toString()); + msg = msg.replace("&o", ChatColor.getByChar("o").toString()); + msg = msg.replace("&r", ChatColor.getByChar("r").toString()); + } + + return msg; + } + + //Permissions + private Permission permissionProvider = null; + + private Boolean setupPermissions() + { + RegisteredServiceProvider permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class); + if (permissionProvider != null) { + this.permissionProvider = permissionProvider.getProvider(); + } + return (this.permissionProvider != null); + } + + public Permission getPermissionHandler(){ + return permissionProvider; + } + + //Logger + public void log(String msg){ + log(Level.INFO, msg); + } + + public void log(Level level, Object msg){ + Logger.getLogger("Minecraft").log(level, "["+this.getDescription().getFullName()+"] "+msg); } } diff --git a/src/com/dre/managerxl/commands/MCommand.java b/src/com/dre/managerxl/commands/MCommand.java new file mode 100644 index 0000000..bb8ca13 --- /dev/null +++ b/src/com/dre/managerxl/commands/MCommand.java @@ -0,0 +1,87 @@ +package com.dre.managerxl.commands; + +import java.util.HashSet; +import java.util.Set; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.dre.managerxl.P; +import com.dre.managerxl.commands.player.Ban; +import com.dre.managerxl.commands.player.Unban; + +public abstract class MCommand { + private static MCommandExecutor commandListener = new MCommandExecutor(); + private static Set commands = new HashSet(); + + public static MCommand cmdHelp; + + protected String command; + protected MCommand parrent; + protected String help; + protected String permission; + + protected boolean isConsoleCommand; + protected boolean isPlayerCommand; + + public abstract void onExecute(String[] args, CommandSender sender); + + public String getCommand(){ + return command; + } + + public MCommand getParrent(){ + return parrent; + } + + public String getHelp(){ + return help; + } + + public boolean isConsoleCommand(){ + return isConsoleCommand; + } + + public boolean isPlayerCommand(){ + return isPlayerCommand; + } + + public boolean playerHasPermissions(Player player){ + if(P.p.getPermissionHandler().playerHas(player, permission) || player.isOp()){ + return true; + } + + return false; + } + + public void init(){ + commands.add(this); + P.p.getCommand(this.getCommand()).setExecutor(commandListener); + } + + public void displayHelp(CommandSender sender){ + P.p.msg(sender, getHelp()); + } + + //Static + public static void initCommands(){ + + // PlayerCommands + new Ban(); + new Unban(); + } + + public static Set get(){ + return commands; + } + + public static MCommand get(String command){ + for(MCommand mCommand : commands){ + if(mCommand.getCommand().equals(command)){ + return mCommand; + } + } + + return null; + } +} \ No newline at end of file diff --git a/src/com/dre/managerxl/commands/MCommandExecutor.java b/src/com/dre/managerxl/commands/MCommandExecutor.java new file mode 100644 index 0000000..169649c --- /dev/null +++ b/src/com/dre/managerxl/commands/MCommandExecutor.java @@ -0,0 +1,53 @@ +package com.dre.managerxl.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.Player; + +import com.dre.managerxl.P; + +public class MCommandExecutor implements CommandExecutor{ + + @Override + public boolean onCommand(CommandSender sender, Command command, String arg, String[] args) { + if(args.length > 0){ + String cmd = args[0]; + + MCommand mCommand = MCommand.get(command.getName()); + + if(mCommand != null){ + if(sender instanceof ConsoleCommandSender){ + if(!mCommand.isConsoleCommand()){ + P.p.msg(sender, P.p.getLanguageReader().get("Log_Error_NoConsoleCommand", mCommand.getCommand())); + return false; + } + } + + if(sender instanceof Player){ + Player player = (Player) sender; + if(!mCommand.isPlayerCommand){ + P.p.msg(player, P.p.getLanguageReader().get("Error_NoPlayerCommand", mCommand.command)); + return false; + } else { + if(!mCommand.playerHasPermissions(player)){ + P.p.msg(player, P.p.getLanguageReader().get("Error_NoPermissions")); + return false; + } + } + } + + mCommand.onExecute(args,sender); + } else { + P.p.msg(sender, P.p.getLanguageReader().get("Error_CmdNotExist1",cmd)); + P.p.msg(sender, P.p.getLanguageReader().get("Error_CmdNotExist2")); + } + }else{ + MCommand.cmdHelp.onExecute(args,sender); + } + + return false; + } + +} diff --git a/src/com/dre/managerxl/commands/player/Ban.java b/src/com/dre/managerxl/commands/player/Ban.java new file mode 100644 index 0000000..ff1f636 --- /dev/null +++ b/src/com/dre/managerxl/commands/player/Ban.java @@ -0,0 +1,54 @@ +package com.dre.managerxl.commands.player; + +import org.bukkit.command.CommandSender; + +import com.dre.managerxl.MPlayer; +import com.dre.managerxl.P; +import com.dre.managerxl.commands.MCommand; + +public class Ban extends MCommand{ + + public Ban(){ + this.command = "ban"; + this.parrent = null; + this.help = P.p.getLanguageReader().get("Help_Ban"); + this.permission = "mxl.cmd.player.ban"; + + this.isConsoleCommand = true; + this.isPlayerCommand = true; + + this.init(); + } + + @Override + public void onExecute(String[] args, CommandSender sender) { + if(args.length > 0){ + MPlayer player = MPlayer.getOrCreate(args[0]); + + if(!player.isBanned()){ + player.setBannedReason(parseMessage(args)); + player.setBanned(true); + + P.p.msg(sender, P.p.getLanguageReader().get("Cmd_Ban_Success", player.getName())); + } else { + P.p.msg(sender, P.p.getLanguageReader().get("Error_CmdBan_AlreadyBanned", player.getName())); + } + } else { + P.p.msg(sender, this.help); + } + } + + private String parseMessage(String[] args){ + String message = ""; + + for (int i = 1; i < args.length; i++) { + message += args[i] + " "; + } + + message = message.trim(); + if (message.isEmpty()) { + message = P.p.getLanguageReader().get("Cmd_Ban_DefaultReason"); + } + return message; + } +} diff --git a/src/com/dre/managerxl/commands/player/Unban.java b/src/com/dre/managerxl/commands/player/Unban.java new file mode 100644 index 0000000..1e49866 --- /dev/null +++ b/src/com/dre/managerxl/commands/player/Unban.java @@ -0,0 +1,39 @@ +package com.dre.managerxl.commands.player; + +import org.bukkit.command.CommandSender; + +import com.dre.managerxl.MPlayer; +import com.dre.managerxl.P; +import com.dre.managerxl.commands.MCommand; + +public class Unban extends MCommand{ + + public Unban(){ + this.command = "unban"; + this.parrent = null; + this.help = P.p.getLanguageReader().get("Help_Unban"); + this.permission = "mxl.cmd.player.unban"; + + this.isConsoleCommand = true; + this.isPlayerCommand = true; + + this.init(); + } + + @Override + public void onExecute(String[] args, CommandSender sender) { + if(args.length > 0){ + MPlayer player = MPlayer.getOrCreate(args[0]); + + if(player.isBanned()){ + player.setBanned(false); + + P.p.msg(sender, P.p.getLanguageReader().get("Cmd_UnBan_Success", player.getName())); + } else { + P.p.msg(sender, P.p.getLanguageReader().get("Error_CmdBan_NotBanned", player.getName())); + } + } else { + P.p.msg(sender, this.help); + } + } +} diff --git a/src/com/dre/managerxl/listeners/PlayerListener.java b/src/com/dre/managerxl/listeners/PlayerListener.java new file mode 100644 index 0000000..1bdc92c --- /dev/null +++ b/src/com/dre/managerxl/listeners/PlayerListener.java @@ -0,0 +1,48 @@ +package com.dre.managerxl.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerLoginEvent.Result; +import org.bukkit.event.player.PlayerQuitEvent; + +import com.dre.managerxl.MPlayer; +import com.dre.managerxl.P; + +public class PlayerListener implements Listener{ + + @EventHandler() + public void onPlayerLogin(PlayerLoginEvent event){ + MPlayer player = MPlayer.get(event.getPlayer().getName()); + + if(player != null){ + if(player.isBanned()){ + event.disallow(Result.KICK_OTHER, P.p.replaceColors(P.p.getLanguageReader().get("Player_Kick_Ban", player.getBannedReason()))); + } + } + } + + @EventHandler() + public void onPlayerJoin(PlayerJoinEvent event){ + MPlayer player = MPlayer.get(event.getPlayer().getName()); + + if(player == null){ + player = new MPlayer(event.getPlayer().getName()); + } + + player.setOnline(true); + } + + + @EventHandler() + public void onPlayerQuit(PlayerQuitEvent event){ + MPlayer player = MPlayer.get(event.getPlayer().getName()); + + if(player == null){ + player = new MPlayer(event.getPlayer().getName()); + } + + player.setOnline(false); + } +}