Skip to content

Commit

Permalink
Ban Command, Save and Load
Browse files Browse the repository at this point in the history
added Ban command + functionality
added Player save and load function 

Signed-off-by: Grafe <[email protected]>
  • Loading branch information
Grafe committed Apr 6, 2013
1 parent c64775b commit 2bcc12b
Show file tree
Hide file tree
Showing 9 changed files with 532 additions and 4 deletions.
5 changes: 5 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 30 additions & 2 deletions src/com/dre/managerxl/LanguageReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
114 changes: 114 additions & 0 deletions src/com/dre/managerxl/MPlayer.java
Original file line number Diff line number Diff line change
@@ -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<MPlayer> mPlayers = new HashSet<MPlayer>();

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<MPlayer> 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<String> 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;
}
}
104 changes: 102 additions & 2 deletions src/com/dre/managerxl/P.java
Original file line number Diff line number Diff line change
@@ -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(){
Expand All @@ -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<Permission> 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);
}
}
87 changes: 87 additions & 0 deletions src/com/dre/managerxl/commands/MCommand.java
Original file line number Diff line number Diff line change
@@ -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<MCommand> commands = new HashSet<MCommand>();

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<MCommand> get(){
return commands;
}

public static MCommand get(String command){
for(MCommand mCommand : commands){
if(mCommand.getCommand().equals(command)){
return mCommand;
}
}

return null;
}
}
Loading

0 comments on commit 2bcc12b

Please sign in to comment.