From 1da8b6b30a3b4e3ad32bd5a57aa26f6d1c64b6e5 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 18 Mar 2011 17:33:23 +0100 Subject: [PATCH] Started the refactoring --- .../mcteam/factions/{entities => }/Board.java | 133 +++++++--- src/com/bukkit/mcteam/factions/Commands.java | 86 +++---- .../mcteam/factions/{entities => }/Conf.java | 42 +++- .../mcteam/factions/{entities => }/Coord.java | 5 +- .../{entities/Follower.java => FPlayer.java} | 205 ++++++++++++---- .../factions/{entities => }/Faction.java | 162 ++++++++++-- src/com/bukkit/mcteam/factions/Factions.java | 161 ++++++++++-- .../mcteam/factions/commands/FCommand.java | 142 +++++++++++ .../factions/commands/FCommandLeave.java | 45 ++++ .../bukkit/mcteam/factions/entities/EM.java | 18 +- .../listeners/FactionsBlockListener.java | 11 +- .../listeners/FactionsEntityListener.java | 14 +- .../listeners/FactionsPlayerListener.java | 21 +- .../mcteam/factions/struct/Relation.java | 2 +- .../bukkit/mcteam/factions/struct/Role.java | 2 +- src/com/bukkit/mcteam/factions/util/Log.java | 39 --- .../bukkit/mcteam/factions/util/TextUtil.java | 2 +- src/com/bukkit/mcteam/util/ChatFixUtil.java | 230 ------------------ 18 files changed, 849 insertions(+), 471 deletions(-) rename src/com/bukkit/mcteam/factions/{entities => }/Board.java (63%) rename src/com/bukkit/mcteam/factions/{entities => }/Conf.java (83%) rename src/com/bukkit/mcteam/factions/{entities => }/Coord.java (93%) rename src/com/bukkit/mcteam/factions/{entities/Follower.java => FPlayer.java} (71%) rename src/com/bukkit/mcteam/factions/{entities => }/Faction.java (64%) create mode 100644 src/com/bukkit/mcteam/factions/commands/FCommand.java create mode 100644 src/com/bukkit/mcteam/factions/commands/FCommandLeave.java delete mode 100644 src/com/bukkit/mcteam/factions/util/Log.java delete mode 100644 src/com/bukkit/mcteam/util/ChatFixUtil.java diff --git a/src/com/bukkit/mcteam/factions/entities/Board.java b/src/com/bukkit/mcteam/factions/Board.java similarity index 63% rename from src/com/bukkit/mcteam/factions/entities/Board.java rename to src/com/bukkit/mcteam/factions/Board.java index 29fa2878..2f70dfa0 100644 --- a/src/com/bukkit/mcteam/factions/entities/Board.java +++ b/src/com/bukkit/mcteam/factions/Board.java @@ -1,23 +1,38 @@ -package com.bukkit.mcteam.factions.entities; +package com.bukkit.mcteam.factions; -import java.util.*; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import java.util.Map.Entry; import org.bukkit.ChatColor; -import org.bukkit.World; -import com.bukkit.mcteam.factions.util.Log; +import com.bukkit.mcteam.factions.entities.EM; import com.bukkit.mcteam.factions.util.TextUtil; +import com.bukkit.mcteam.gson.reflect.TypeToken; import com.bukkit.mcteam.util.AsciiCompass; +import com.bukkit.mcteam.util.DiscUtil; //import com.bukkit.mcteam.factions.util.*; public class Board { + protected static transient Map instances = new HashMap(); + protected static transient File file = new File(Factions.instance.getDataFolder(), "boards.json"); + public transient String worldName; - protected Map coordFactionIds; + protected Map coordFactionIds = new HashMap(); public Board() { - coordFactionIds = new HashMap(); + + } + + public Board(String worldName) { + this.worldName = worldName; } public Faction getFactionAt(Coord coord) { @@ -64,7 +79,7 @@ public class Board { while (iter.hasNext()) { Entry entry = iter.next(); if ( ! EM.factionExists(entry.getValue())) { - Log.debug("Cleaner removed coord with non existing factionId "+entry.getValue()); + Factions.log("Cleaner removed coord with non existing factionId "+entry.getValue()); iter.remove(); } } @@ -72,37 +87,11 @@ public class Board { public static void cleanAll() { for (Board board : getAll()) { - Log.debug("Cleaning board for world "+board.worldName); + Factions.log("Cleaning board for world "+board.worldName); board.clean(); } } - //----------------------------------------------// - // Purge faction Currently skipped and we use clean instead as that will solve orphaned keys to :) - //----------------------------------------------// - /* - public void purgeFaction(int factionId) { - Iterator> iter = coordFactionIds.entrySet().iterator(); - while (iter.hasNext()) { - Entry entry = iter.next(); - if (entry.getValue().equals(factionId)) { - iter.remove(); - } - } - } - public void purgeFaction(Faction faction) { - purgeFaction(faction.id); - } - - public static void purgeFactionFromAllBoards(int factionId) { - for (Board board : getAll()) { - board.purgeFaction(factionId); - } - } - public static void purgeFactionFromAllBoards(Faction faction) { - purgeFactionFromAllBoards(faction.id); - }*/ - //----------------------------------------------// // Coord count //----------------------------------------------// @@ -186,7 +175,7 @@ public class Board { // Persistance //----------------------------------------------// - public boolean save() { + /*public boolean save() { return EM.boardSave(this.worldName); } @@ -196,6 +185,80 @@ public class Board { public static Collection getAll() { return EM.boardGetAll(); + }*/ + + + // -------------------------------------------- // + // Persistance + // -------------------------------------------- // + + public boolean shouldBeSaved() { + return this.coordFactionIds.size() > 0; + } + + public static boolean save() { + Factions.log("Saving boards to disk"); + + Map instancesToSave = new HashMap(); + for (Entry entry : instances.entrySet()) { + if (entry.getValue().shouldBeSaved()) { + instancesToSave.put(entry.getKey(), entry.getValue()); + } + } + + try { + DiscUtil.write(file, Factions.gson.toJson(instancesToSave)); + } catch (IOException e) { + Factions.log("Failed to save the boards to disk."); + e.printStackTrace(); + return false; + } + return true; + } + + public static boolean load() { + if ( ! file.exists()) { + Factions.log("No boards to load from disk. Creating new file."); + save(); + return true; + } + + try { + Type type = new TypeToken>(){}.getType(); + instances = Factions.gson.fromJson(DiscUtil.read(file), type); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + + fillNames(); + + return true; + } + + public static void fillNames() { + for(Entry entry : instances.entrySet()) { + entry.getValue().worldName = entry.getKey(); + } + } + + public static Board get(String worldName) { + if (instances.containsKey(worldName)) { + return instances.get(worldName); + } + + Board board = new Board(worldName); + instances.put(worldName, board); + return board; + } + + // You should use this one to be sure you do not spell the player name wrong. + public static Board get(Board board) { + return get(board.worldName); + } + + public static Collection getAll() { + return instances.values(); } } diff --git a/src/com/bukkit/mcteam/factions/Commands.java b/src/com/bukkit/mcteam/factions/Commands.java index 210ad0c7..fa032e82 100644 --- a/src/com/bukkit/mcteam/factions/Commands.java +++ b/src/com/bukkit/mcteam/factions/Commands.java @@ -98,12 +98,12 @@ public class Commands { // Update to work with tag and follower names - public static Follower findFollower(Follower me, String name, boolean defaultsToMe) { + public static FPlayer findFollower(FPlayer me, String name, boolean defaultsToMe) { if (name.length() == 0 && defaultsToMe) { return me; } - Follower follower = Follower.find(name); + FPlayer follower = FPlayer.find(name); if (follower != null) { return follower; } @@ -112,13 +112,13 @@ public class Commands { return null; } - public static Faction findFaction(Follower me, String name, boolean defaultsToMe) { + public static Faction findFaction(FPlayer me, String name, boolean defaultsToMe) { if (name.length() == 0 && defaultsToMe) { return me.getFaction(); } // Search player names - Follower follower = Follower.find(name); + FPlayer follower = FPlayer.find(name); if (follower != null) { return follower.getFaction(); } @@ -133,7 +133,7 @@ public class Commands { return null; } - public static boolean canIAdministerYou(Follower i, Follower you) { + public static boolean canIAdministerYou(FPlayer i, FPlayer you) { if ( ! i.getFaction().equals(you.getFaction())) { i.sendMessage(you.getNameAndRelevant(i)+Conf.colorSystem+" is not in the same faction as you."); return false; @@ -158,7 +158,7 @@ public class Commands { // The base command //----------------------------------------------// - public static void base(Follower me, ArrayList tokens) { + public static void base(FPlayer me, ArrayList tokens) { if (tokens.size() == 0) { help(me); return; @@ -230,11 +230,11 @@ public class Commands { //----------------------------------------------// // The other commands //----------------------------------------------// - public static void help(Follower me) { + public static void help(FPlayer me) { help(me, 1); } - public static void help(Follower me, Integer page) { + public static void help(FPlayer me, Integer page) { me.sendMessage(TextUtil.titleize("Factions Help ("+page+"/"+helpPages.size()+")"), false); page -= 1; if (page < 0 || page >= helpPages.size()) { @@ -244,7 +244,7 @@ public class Commands { me.sendMessage(helpPages.get(page), false); } - public static void leave(Follower me) { + public static void leave(FPlayer me) { Faction faction = me.getFaction(); ArrayList errors = me.leave(); @@ -257,14 +257,14 @@ public class Commands { if (faction.getFollowersAll().size() == 0) { // Remove this faction - for (Follower follower : Follower.getAll()) { + for (FPlayer follower : FPlayer.getAll()) { follower.sendMessage(Conf.colorSystem+"The faction "+faction.getTag(follower)+Conf.colorSystem+" was disbanded."); } EM.factionDelete(faction.id); } } - public static void join(Follower me, String name) { + public static void join(FPlayer me, String name) { Faction faction = findFaction(me, name, false); if (faction == null) { return; @@ -281,7 +281,7 @@ public class Commands { } } - public static void create(Follower me, String tag) { + public static void create(FPlayer me, String tag) { ArrayList errors = new ArrayList(); if (me.hasFaction()) { @@ -306,7 +306,7 @@ public class Commands { me.role = Role.ADMIN; me.save(); - for (Follower follower : Follower.getAll()) { + for (FPlayer follower : FPlayer.getAll()) { follower.sendMessage(me.getNameAndRelevant(follower)+Conf.colorSystem+" created a new faction "+faction.getTag(follower)); } @@ -314,7 +314,7 @@ public class Commands { me.sendMessage(Conf.colorCommand+Conf.aliasBase.get(0)+" "+Conf.aliasDescription.get(0)+" "+"[description]"); } - public static void tag(Follower me, String tag) { + public static void tag(FPlayer me, String tag) { ArrayList errors = new ArrayList(); if (me.withoutFaction()) { @@ -349,7 +349,7 @@ public class Commands { } } - public static void list(Follower me, String inPage) { + public static void list(FPlayer me, String inPage) { ArrayList FactionList = new ArrayList(Faction.getAll()); int page = 1; @@ -411,14 +411,14 @@ public class Commands { } } - public static void showFaction(Follower me, String name) { + public static void showFaction(FPlayer me, String name) { Faction faction = findFaction(me, name, true); if (faction == null) { return; } - Collection admins = faction.getFollowersWhereRole(Role.ADMIN); - Collection mods = faction.getFollowersWhereRole(Role.MODERATOR); - Collection normals = faction.getFollowersWhereRole(Role.NORMAL); + Collection admins = faction.getFollowersWhereRole(Role.ADMIN); + Collection mods = faction.getFollowersWhereRole(Role.MODERATOR); + Collection normals = faction.getFollowersWhereRole(Role.NORMAL); me.sendMessage(TextUtil.titleize(faction.getTag(me)), false); me.sendMessage(Conf.colorChrome+"Description: "+Conf.colorSystem+faction.getDescription()); @@ -462,7 +462,7 @@ public class Commands { // List the members... String onlineList = Conf.colorChrome+"Members online: "; String offlineList = Conf.colorChrome+"Members offline: "; - for (Follower follower : admins) { + for (FPlayer follower : admins) { listpart = follower.getNameAndTitle(me)+Conf.colorSystem+", "; if (follower.isOnline()) { onlineList += listpart; @@ -470,7 +470,7 @@ public class Commands { offlineList += listpart; } } - for (Follower follower : mods) { + for (FPlayer follower : mods) { listpart = follower.getNameAndTitle(me)+Conf.colorSystem+", "; if (follower.isOnline()) { onlineList += listpart; @@ -478,7 +478,7 @@ public class Commands { offlineList += listpart; } } - for (Follower follower : normals) { + for (FPlayer follower : normals) { listpart = follower.getNameAndTitle(me)+Conf.colorSystem+", "; if (follower.isOnline()) { onlineList += listpart; @@ -499,7 +499,7 @@ public class Commands { } - public static void showMap(Follower me, String mapAutoUpdating) { + public static void showMap(FPlayer me, String mapAutoUpdating) { Board board = Board.get(me.getPlayer().getWorld()); if (mapAutoUpdating.length() > 0) { if (Conf.aliasTrue.contains(mapAutoUpdating.toLowerCase())) { @@ -519,8 +519,8 @@ public class Commands { } } - public static void invite(Follower me, String name) { - Follower follower = findFollower(me, name, false); + public static void invite(FPlayer me, String name) { + FPlayer follower = findFollower(me, name, false); if (follower == null) { return; } @@ -534,8 +534,8 @@ public class Commands { } } - public static void deinvite(Follower me, String name) { // TODO Move out! - Follower follower = findFollower(me, name, false); + public static void deinvite(FPlayer me, String name) { // TODO Move out! + FPlayer follower = findFollower(me, name, false); if (follower == null) { return; } @@ -549,7 +549,7 @@ public class Commands { } } - public static void open(Follower me) { + public static void open(FPlayer me) { if (me.role.value < Role.MODERATOR.value) { me.sendMessage(Conf.colorSystem+"You must be moderator to do this"); return; @@ -569,7 +569,7 @@ public class Commands { } } - public static void title(Follower me, ArrayList tokens) { + public static void title(FPlayer me, ArrayList tokens) { if (tokens.size() == 0) { me.sendMessage(Conf.colorSystem+"You must specify a player name"); return; @@ -578,7 +578,7 @@ public class Commands { String name = tokens.get(0); tokens.remove(0); - Follower you = findFollower(me, name, true); + FPlayer you = findFollower(me, name, true); if (you == null) { return; } @@ -596,13 +596,13 @@ public class Commands { myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" changed a title: "+you.getNameAndRelevant(myFaction)); } - public static void kick(Follower me, String name) { + public static void kick(FPlayer me, String name) { if (name.length() == 0) { me.sendMessage(Conf.colorSystem+"You must specify a player name."); return; } - Follower you = findFollower(me, name, false); + FPlayer you = findFollower(me, name, false); if (you == null) { return; } @@ -617,7 +617,7 @@ public class Commands { } } - public static void roleChange(Follower me, Role targetRole, String name) { + public static void roleChange(FPlayer me, Role targetRole, String name) { if (me.role.value < Role.ADMIN.value) { me.sendMessage(Conf.colorSystem+"You must be faction admin to do this"); return; @@ -628,7 +628,7 @@ public class Commands { return; } - Follower targetFollower = findFollower(me, name, false); + FPlayer targetFollower = findFollower(me, name, false); if (targetFollower == null) { return; } @@ -648,7 +648,7 @@ public class Commands { targetFollower.role = Role.ADMIN; // Inform all players - for (Follower follower : Follower.getAll()) { + for (FPlayer follower : FPlayer.getAll()) { if (follower.factionId == me.factionId) { follower.sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" gave "+targetFollower.getNameAndRelevant(me)+Conf.colorSystem+" the leadership of your faction."); } else { @@ -668,7 +668,7 @@ public class Commands { } } - public static void claim(Follower me) { + public static void claim(FPlayer me) { if (me.withoutFaction()) { me.sendMessage(Conf.colorSystem+"You are not part of any faction."); return; @@ -723,7 +723,7 @@ public class Commands { board.claim(coord, myFaction); } - public static void unclaim(Follower me) { + public static void unclaim(FPlayer me) { if (me.withoutFaction()) { me.sendMessage(Conf.colorSystem+"You are not part of any faction"); return; @@ -746,7 +746,7 @@ public class Commands { me.getFaction().sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" unclaimed some land."); } - public static void relation(Follower me, Relation whishedRelation, String otherFactionName) { + public static void relation(FPlayer me, Relation whishedRelation, String otherFactionName) { if (me.withoutFaction()) { me.sendMessage(Conf.colorSystem+"You are not part of any faction."); return; @@ -791,7 +791,7 @@ public class Commands { } } - public static void description(Follower me, String desc) { + public static void description(FPlayer me, String desc) { if (me.withoutFaction()) { me.sendMessage(Conf.colorSystem+"You are not part of any faction"); return; @@ -807,13 +807,13 @@ public class Commands { me.sendMessage(Conf.colorSystem+"The new description was set :D"); // Broadcast the description to everyone - for (Follower follower : EM.followerGetAll()) { + for (FPlayer follower : EM.followerGetAll()) { follower.sendMessage(Conf.colorSystem+"The faction "+follower.getRelationColor(me)+me.getFaction().getTag()+Conf.colorSystem+" changed their description to:"); follower.sendMessage(Conf.colorSystem+desc); } } - public static void chat(Follower me, String msg) { + public static void chat(FPlayer me, String msg) { if (me.withoutFaction()) { me.sendMessage(Conf.colorSystem+"You are not part of any faction"); return; @@ -830,8 +830,8 @@ public class Commands { } } - public static void version(Follower me) { - me.sendMessage(Conf.colorSystem+"You are running "+Factions.factions.getDescription().getFullName()); + public static void version(FPlayer me) { + me.sendMessage(Conf.colorSystem+"You are running "+Factions.instance.getDescription().getFullName()); } } diff --git a/src/com/bukkit/mcteam/factions/entities/Conf.java b/src/com/bukkit/mcteam/factions/Conf.java similarity index 83% rename from src/com/bukkit/mcteam/factions/entities/Conf.java rename to src/com/bukkit/mcteam/factions/Conf.java index 81bafaef..3a7109c6 100644 --- a/src/com/bukkit/mcteam/factions/entities/Conf.java +++ b/src/com/bukkit/mcteam/factions/Conf.java @@ -1,9 +1,15 @@ -package com.bukkit.mcteam.factions.entities; +package com.bukkit.mcteam.factions; +import java.io.File; +import java.io.IOException; import java.util.*; import org.bukkit.*; +import com.bukkit.mcteam.util.DiscUtil; + public class Conf { + public static transient File file = new File(Factions.instance.getDataFolder(), "conf.json"); + // Colors public static ChatColor colorMember = ChatColor.GREEN; public static ChatColor colorAlly = ChatColor.LIGHT_PURPLE; @@ -14,8 +20,6 @@ public class Conf { public static ChatColor colorChrome = ChatColor.GOLD; public static ChatColor colorCommand = ChatColor.AQUA; public static ChatColor colorParameter = ChatColor.DARK_AQUA; - - public static Integer logThreshold = 10; // Power public static double powerPlayerMax = 10; @@ -154,11 +158,37 @@ public class Conf { aliasTrue.add("+"); } - //----------------------------------------------// + // -------------------------------------------- // // Persistance - //----------------------------------------------// + // -------------------------------------------- // public static boolean save() { - return EM.configSave(); + Factions.log("Saving config to disk."); + try { + DiscUtil.write(file, Factions.gson.toJson(new Conf())); + } catch (IOException e) { + e.printStackTrace(); + Factions.log("Failed to save the config to disk."); + return false; + } + return true; + } + + public static boolean load() { + if ( ! file.exists()) { + Factions.log("No conf to load from disk. Creating new file."); + save(); + return true; + } + + try { + Factions.gson.fromJson(DiscUtil.read(file), Conf.class); + } catch (IOException e) { + e.printStackTrace(); + Factions.log("Failed to load the config from disk."); + return false; + } + + return true; } } diff --git a/src/com/bukkit/mcteam/factions/entities/Coord.java b/src/com/bukkit/mcteam/factions/Coord.java similarity index 93% rename from src/com/bukkit/mcteam/factions/entities/Coord.java rename to src/com/bukkit/mcteam/factions/Coord.java index 82e97b08..2ea4ecad 100644 --- a/src/com/bukkit/mcteam/factions/entities/Coord.java +++ b/src/com/bukkit/mcteam/factions/Coord.java @@ -1,9 +1,10 @@ -package com.bukkit.mcteam.factions.entities; +package com.bukkit.mcteam.factions; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.Player; + public class Coord { protected static transient int cellSize = 16; public int x, z; @@ -35,7 +36,7 @@ public class Coord { return from(player.getLocation()); } - public static Coord from(Follower follower) { + public static Coord from(FPlayer follower) { return from(follower.getPlayer()); } diff --git a/src/com/bukkit/mcteam/factions/entities/Follower.java b/src/com/bukkit/mcteam/factions/FPlayer.java similarity index 71% rename from src/com/bukkit/mcteam/factions/entities/Follower.java rename to src/com/bukkit/mcteam/factions/FPlayer.java index 6f774caa..2af27483 100644 --- a/src/com/bukkit/mcteam/factions/entities/Follower.java +++ b/src/com/bukkit/mcteam/factions/FPlayer.java @@ -1,16 +1,23 @@ -package com.bukkit.mcteam.factions.entities; +package com.bukkit.mcteam.factions; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Type; import java.util.*; +import java.util.Map.Entry; import org.bukkit.ChatColor; import org.bukkit.entity.Player; -import com.bukkit.mcteam.factions.Factions; -import com.bukkit.mcteam.factions.struct.*; -import com.bukkit.mcteam.util.ChatFixUtil; +import com.bukkit.mcteam.factions.entities.EM; +import com.bukkit.mcteam.gson.reflect.TypeToken; +import com.bukkit.mcteam.util.DiscUtil; -public class Follower { - public transient String id; // The is the name of the player +public class FPlayer { + public static transient Map instances = new HashMap(); + public static transient File file = new File(Factions.instance.getDataFolder(), "players.json"); + + public transient String playername; public transient Coord lastStoodInCoord = new Coord(); // Where did this player stand the last time we checked? public int factionId; @@ -21,6 +28,43 @@ public class Follower { private boolean mapAutoUpdating; private boolean factionChatting; + public FPlayer(Player player) { + this.playername = player.getName(); + } + + public FPlayer(String playername) { + this.playername = playername; + } + + // GSON need this noarg constructor. + public FPlayer() { + + } + + public Player getPlayer() { + return Factions.instance.getServer().getPlayer(playername); + } + + public String getPlayerName() { + return this.playername; + } + + // -------------------------------------------- // + // Online / Offline State Checking + // -------------------------------------------- // + + public boolean isOnline() { + return Factions.instance.getServer().getPlayer(playername) != null; + } + + public boolean isOffline() { + return ! isOnline(); + } + + + + + public boolean isFactionChatting() { if (this.factionId == 0) { return false; @@ -32,7 +76,7 @@ public class Follower { this.factionChatting = factionChatting; } - public Follower() { + public FPlayer() { this.resetFactionData(); this.power = this.getPowerMax(); this.lastPowerUpdateTime = System.currentTimeMillis(); @@ -46,14 +90,6 @@ public class Follower { this.title = ""; } - public Player getPlayer() { - return Factions.factions.getServer().getPlayer(this.getName()); - } - - public boolean isOnline() { - return this.getPlayer() != null; - } - public boolean isMapAutoUpdating() { return mapAutoUpdating; } @@ -113,14 +149,14 @@ public class Follower { public String getNameAndTitle(Faction faction) { return this.getRelationColor(faction)+this.getNameAndTitle(); } - public String getNameAndTitle(Follower follower) { + public String getNameAndTitle(FPlayer follower) { return this.getRelationColor(follower)+this.getNameAndTitle(); } public String getNameAndTag(Faction faction) { return this.getRelationColor(faction)+this.getNameAndTag(); } - public String getNameAndTag(Follower follower) { + public String getNameAndTag(FPlayer follower) { return this.getRelationColor(follower)+this.getNameAndTag(); } @@ -136,7 +172,7 @@ public class Follower { // For non members we show tag return rel.getColor() + this.getNameAndTag(); } - public String getNameAndRelevant(Follower follower) { + public String getNameAndRelevant(FPlayer follower) { return getNameAndRelevant(follower.getFaction()); } @@ -159,7 +195,7 @@ public class Follower { return this.getRelation(faction).getColor()+getChatTag(); } - public String getChatTag(Follower follower) { + public String getChatTag(FPlayer follower) { if (this.withoutFaction()) { return ""; } @@ -175,7 +211,7 @@ public class Follower { return faction.getRelation(this); } - public Relation getRelation(Follower follower) { + public Relation getRelation(FPlayer follower) { return this.getFaction().getRelation(follower); } @@ -183,7 +219,7 @@ public class Follower { return faction.getRelationColor(this); } - public ChatColor getRelationColor(Follower follower) { + public ChatColor getRelationColor(FPlayer follower) { return this.getRelation(follower).getColor(); } @@ -343,7 +379,7 @@ public class Follower { return errors; } - public ArrayList invite(Follower follower) { + public ArrayList invite(FPlayer follower) { ArrayList errors = new ArrayList(); //Log.debug("this.role: "+this.role); @@ -361,7 +397,7 @@ public class Follower { return this.getFaction().invite(follower); } - public ArrayList deinvite(Follower follower) { + public ArrayList deinvite(FPlayer follower) { ArrayList errors = new ArrayList(); if (this.role.value < Role.MODERATOR.value) { @@ -375,7 +411,7 @@ public class Follower { return this.getFaction().deinvite(follower); } - public ArrayList kick(Follower follower) { + public ArrayList kick(FPlayer follower) { ArrayList errors = new ArrayList(); if ( ! follower.getFaction().equals(this.getFaction())) { @@ -403,31 +439,11 @@ public class Follower { //this.getPlayer().sendMessage(ChatColor.GREEN + "This is a faction server! Type "+Conf.colorCommand+"/f"+ChatColor.GREEN +" for more info :D"); } - //----------------------------------------------// - // Messages - Directly connected to ChatFixUtil - //----------------------------------------------// - public void sendMessage(String message, boolean fix) { - Player player = this.getPlayer(); - ChatFixUtil.sendMessage(player, message, fix); - } - public void sendMessage(List messages, boolean fix) { - Player player = this.getPlayer(); - ChatFixUtil.sendMessage(player, messages, fix); - } - public void sendMessage(String message) { - Player player = this.getPlayer(); - ChatFixUtil.sendMessage(player, message, true); - } - public void sendMessage(List messages) { - Player player = this.getPlayer(); - ChatFixUtil.sendMessage(player, messages, true); - } - //----------------------------------------------// // Search //----------------------------------------------// - public static Follower find(String name) { - for (Follower follower : EM.followerGetAll()) { + public static FPlayer find(String name) { // TODO felaktig! + for (FPlayer follower : EM.followerGetAll()) { if (follower.getName().equalsIgnoreCase(name.trim())) { return follower; } @@ -436,19 +452,110 @@ public class Follower { return null; } + // -------------------------------------------- // + // Get + // You can only get a "skin" for online players. + // The same object is always returned for the same player. + // This means you can use the == operator. No .equals method necessary. + // -------------------------------------------- // + public static FPlayer get(String playername) { + if (instances.containsKey(playername)) { + return instances.get(playername); + } + + FPlayer vplayer = new FPlayer(playername); + instances.put(playername, vplayer); + return vplayer; + } + + // You should use this one to be sure you do not spell the player name wrong. + public static FPlayer get(Player player) { + return get(player.getName()); + } + + // -------------------------------------------- // + // Messages + // -------------------------------------------- // + public void sendMessage(String message) { + this.getPlayer().sendMessage(Conf.colorSystem + message); + } + + public void sendMessage(List messages) { + for(String message : messages) { + this.sendMessage(message); + } + } + + //----------------------------------------------// // Persistance and entity management //----------------------------------------------// - + /* public boolean save() { return EM.followerSave(this.id); } - public static Follower get(Player player) { + public static FPlayer get(Player player) { return EM.followerGet(player); } - public static Collection getAll() { + public static Collection getAll() { return EM.followerGetAll(); } + */ + // -------------------------------------------- // + // Persistance + // -------------------------------------------- // + + public boolean shouldBeSaved() { + return this.factionId != 0; + } + + public static boolean save() { + Factions.log("Saving players to disk"); + + // We only wan't to save the vplayers with non default values + Map vplayersToSave = new HashMap(); + for (Entry entry : instances.entrySet()) { + if (entry.getValue().shouldBeSaved()) { + vplayersToSave.put(entry.getKey(), entry.getValue()); + } + } + + try { + DiscUtil.write(file, Factions.gson.toJson(vplayersToSave)); + } catch (IOException e) { + Factions.log("Failed to save the players to disk."); + e.printStackTrace(); + return false; + } + return true; + } + + public static boolean load() { + if ( ! file.exists()) { + Factions.log("No players to load from disk. Creating new file."); + save(); + return true; + } + + try { + Type type = new TypeToken>(){}.getType(); + instances = Factions.gson.fromJson(DiscUtil.read(file), type); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + + fillPlayernames(); + + return true; + } + + public static void fillPlayernames() { + for(Entry entry : instances.entrySet()) { + entry.getValue().playername = entry.getKey(); + } + } + } \ No newline at end of file diff --git a/src/com/bukkit/mcteam/factions/entities/Faction.java b/src/com/bukkit/mcteam/factions/Faction.java similarity index 64% rename from src/com/bukkit/mcteam/factions/entities/Faction.java rename to src/com/bukkit/mcteam/factions/Faction.java index f943263e..dec839bb 100644 --- a/src/com/bukkit/mcteam/factions/entities/Faction.java +++ b/src/com/bukkit/mcteam/factions/Faction.java @@ -1,17 +1,26 @@ -package com.bukkit.mcteam.factions.entities; +package com.bukkit.mcteam.factions; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Type; import java.util.*; +import java.util.Map.Entry; +import java.util.logging.Level; import org.bukkit.ChatColor; import org.bukkit.entity.Player; -import com.bukkit.mcteam.factions.Factions; +import com.bukkit.mcteam.factions.entities.EM; import com.bukkit.mcteam.factions.struct.Relation; import com.bukkit.mcteam.factions.struct.Role; import com.bukkit.mcteam.factions.util.*; -import com.bukkit.mcteam.util.ChatFixUtil; +import com.bukkit.mcteam.gson.reflect.TypeToken; +import com.bukkit.mcteam.util.DiscUtil; public class Faction { + public static transient Map instances = new HashMap(); + public static transient File file = new File(Factions.instance.getDataFolder(), "factions.json"); + public static transient int nextId; public transient int id; protected Map relationWish; @@ -40,7 +49,7 @@ public class Faction { public String getTag(Faction otherFaction) { return this.getTag(otherFaction.getRelationColor(this).toString()); } - public String getTag(Follower otherFollower) { + public String getTag(FPlayer otherFollower) { return this.getTag(otherFollower.getRelationColor(this).toString()); } public void setTag(String str) { @@ -74,7 +83,7 @@ public class Faction { //----------------------------------------------// public double getPower() { double ret = 0; - for (Follower follower : this.getFollowersAll()) { + for (FPlayer follower : this.getFollowersAll()) { ret += follower.getPower(); } return ret; @@ -82,7 +91,7 @@ public class Faction { public double getPowerMax() { double ret = 0; - for (Follower follower : this.getFollowersAll()) { + for (FPlayer follower : this.getFollowersAll()) { ret += follower.getPowerMax(); } return ret; @@ -109,7 +118,7 @@ public class Faction { // ------------------------------- - public ArrayList invite(Follower follower) { // TODO Move out + public ArrayList invite(FPlayer follower) { // TODO Move out ArrayList errors = new ArrayList(); if (follower.getFaction().equals(this)) { // error här? @@ -125,7 +134,7 @@ public class Faction { return errors; } - public ArrayList deinvite(Follower follower) { // TODO move out! + public ArrayList deinvite(FPlayer follower) { // TODO move out! ArrayList errors = new ArrayList(); if (follower.getFaction() == this) { @@ -142,14 +151,14 @@ public class Faction { return errors; } - public ArrayList kick(Follower follower) { + public ArrayList kick(FPlayer follower) { ArrayList errors = new ArrayList(); removeFollower(follower); return errors; } - public boolean isInvited(Follower follower) { + public boolean isInvited(FPlayer follower) { return invites.contains(follower.id); } @@ -157,9 +166,9 @@ public class Faction { // Followers // ------------------------------- - public ArrayList getFollowersAll() { - ArrayList ret = new ArrayList(); - for (Follower follower : Follower.getAll()) { + public ArrayList getFollowersAll() { + ArrayList ret = new ArrayList(); + for (FPlayer follower : FPlayer.getAll()) { if (follower.factionId == this.id) { ret.add(follower); } @@ -167,9 +176,9 @@ public class Faction { return ret; } - public ArrayList getFollowersWhereOnline(boolean online) { - ArrayList ret = new ArrayList(); - for (Follower follower : Follower.getAll()) { + public ArrayList getFollowersWhereOnline(boolean online) { + ArrayList ret = new ArrayList(); + for (FPlayer follower : FPlayer.getAll()) { if (follower.factionId == this.id && follower.isOnline() == online) { ret.add(follower); } @@ -177,10 +186,10 @@ public class Faction { return ret; } - public ArrayList getFollowersWhereRole(Role role) { - ArrayList ret = new ArrayList(); + public ArrayList getFollowersWhereRole(Role role) { + ArrayList ret = new ArrayList(); - for (Follower follower : Follower.getAll()) { + for (FPlayer follower : FPlayer.getAll()) { if (follower.factionId == this.id && follower.role.equals(role)) { ret.add(follower); } @@ -189,7 +198,7 @@ public class Faction { return ret; } - public void removeFollower(Follower follower) { + public void removeFollower(FPlayer follower) { if (this.id != follower.factionId) { return; // safety check } @@ -202,8 +211,8 @@ public class Faction { public ArrayList getOnlinePlayers() { ArrayList ret = new ArrayList(); - for (Player player: Factions.factions.getServer().getOnlinePlayers()) { - Follower follower = Follower.get(player); + for (Player player: Factions.instance.getServer().getOnlinePlayers()) { + FPlayer follower = FPlayer.get(player); if (follower.factionId == this.id) { ret.add(player); } @@ -302,7 +311,7 @@ public class Faction { return this.getRelationWish(otherFaction); } - public Relation getRelation(Follower follower) { + public Relation getRelation(FPlayer follower) { return getRelation(follower.getFaction()); } @@ -310,7 +319,7 @@ public class Faction { return this.getRelation(otherFaction).getColor(); } - public ChatColor getRelationColor(Follower follower) { + public ChatColor getRelationColor(FPlayer follower) { return this.getRelation(follower).getColor(); } @@ -318,6 +327,109 @@ public class Faction { // Persistance and entity management //----------------------------------------------// + public static boolean save() { + Factions.log("Saving factions to disk"); + + try { + DiscUtil.write(file, Factions.gson.toJson(instances)); + } catch (IOException e) { + Factions.log("Failed to save the factions to disk."); + e.printStackTrace(); + return false; + } + + return true; + } + + public static boolean load() { + if ( ! file.exists()) { + Factions.log("No factions to load from disk. Creating new file."); + save(); + return true; + } + + try { + Type type = new TypeToken>(){}.getType(); + instances = Factions.gson.fromJson(DiscUtil.read(file), type); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + + fillIds(); + + // Make sure the default neutral faction exists + if ( ! instances.containsKey(0)) { + Faction faction = new Faction(); + faction.tag = "*No faction*"; + faction.description = "\"The faction for the factionless :P\""; + faction.id = 0; + instances.put(faction.id, faction); + } + + return true; + } + + public static void fillIds() { + nextId = 1; + for(Entry entry : instances.entrySet()) { + entry.getValue().id = entry.getKey(); + if (nextId < entry.getKey()) { + nextId = entry.getKey(); + } + } + nextId += 1; // make it the next id and not the current highest. + } + + public static Faction get(Integer factionId) { + if ( ! instances.containsKey(factionId)) { + Factions.log(Level.WARNING, "Non existing factionId "+factionId+" requested! Issuing board cleaning!"); + Board.cleanAll(); + } + return instances.get(factionId); + } + + public static boolean exists(Integer factionId) { + return instances.containsKey(factionId); + } + + public static Collection getAll() { + return instances.values(); + } + + //TODO ta parametrar här. All info som behövs ska matas in här och så sparar vi i denna method. + public static Faction create() { + Faction faction = new Faction(); + faction.id = nextId; + nextId += 1; + instances.put(faction.id, faction); + Factions.log("created new faction "+faction.id); + //faction.save(); + return faction; + } + + public static boolean delete(Integer id) { + // NOTE that this does not do any security checks. + // Follower might get orphaned foreign id's + + // purge from all boards + // Board.purgeFactionFromAllBoards(id); + Board.cleanAll(); + + // Remove the file + //File file = new File(folderFaction, id+ext); + //file.delete(); + + // Remove the faction + instances.remove(id); + + // TODO REMOVE ALL MEMBERS! + + // TODO SAVE files + return true; // TODO + } + + /* public static Faction create() { return EM.factionCreate(); } @@ -333,5 +445,5 @@ public class Faction { public boolean save() { return EM.factionSave(this.id); } - + */ } diff --git a/src/com/bukkit/mcteam/factions/Factions.java b/src/com/bukkit/mcteam/factions/Factions.java index ee20ee9a..8bfac69f 100644 --- a/src/com/bukkit/mcteam/factions/Factions.java +++ b/src/com/bukkit/mcteam/factions/Factions.java @@ -1,37 +1,84 @@ package com.bukkit.mcteam.factions; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + + + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; import org.bukkit.event.Event; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; -import com.bukkit.mcteam.factions.entities.*; +import com.bukkit.mcteam.factions.commands.FCommand; import com.bukkit.mcteam.factions.listeners.FactionsBlockListener; import com.bukkit.mcteam.factions.listeners.FactionsEntityListener; import com.bukkit.mcteam.factions.listeners.FactionsPlayerListener; -import com.bukkit.mcteam.factions.util.Log; +import com.bukkit.mcteam.gson.Gson; +import com.bukkit.mcteam.gson.GsonBuilder; +import com.bukkit.mcteam.gson.MapAsArrayTypeAdapter; + +import com.nijiko.permissions.PermissionHandler; +import com.nijikokun.bukkit.Permissions.Permissions; + +import me.taylorkelly.help.Help; public class Factions extends JavaPlugin { - public static Factions factions; + // -------------------------------------------- // + // Fields + // -------------------------------------------- // + public static Factions instance; + + public final static Gson gson = new GsonBuilder() + .setPrettyPrinting() + .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE) + .registerTypeAdapter(Map.class, new MapAsArrayTypeAdapter()) // a "must have" adapter for GSON + .create(); private final FactionsPlayerListener playerListener = new FactionsPlayerListener(this); private final FactionsEntityListener entityListener = new FactionsEntityListener(this); private final FactionsBlockListener blockListener = new FactionsBlockListener(this); + + public static PermissionHandler Permissions; + public static Help helpPlugin; - @Override - public void onDisable() { - // TODO Auto-generated method stub - + // Commands + public List commands = new ArrayList(); + + public Factions() { + Factions.instance = this; } - + + @Override public void onEnable() { - Factions.factions = this; + // Add the commands + /*commands.add(new VCommandBlood()); + commands.add(new VCommandInfect()); + commands.add(new VCommandLoad()); + commands.add(new VCommandSave()); + commands.add(new VCommandTime()); + commands.add(new VCommandTurn()); + commands.add(new VCommandCure()); + commands.add(new VCommandList()); + commands.add(new VCommandVersion());*/ - Log.info("=== INIT START ==="); + setupPermissions(); + setupHelp(); + + log("=== INIT START ==="); long timeInitStart = System.currentTimeMillis(); - Log.info("You are running version: "+this.getDescription().getVersion()); - EM.loadAll(); + FPlayer.load(); + Faction.load(); + Board.load(); // Register events PluginManager pm = this.getServer().getPluginManager(); @@ -48,8 +95,94 @@ public class Factions extends JavaPlugin { pm.registerEvent(Event.Type.BLOCK_PLACED, this.blockListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.BLOCK_INTERACT, this.blockListener, Event.Priority.Normal, this); - Log.info("=== INIT DONE (Took "+(System.currentTimeMillis()-timeInitStart)+"ms) ==="); - Log.threshold = Conf.logThreshold; + log("=== INIT DONE (Took "+(System.currentTimeMillis()-timeInitStart)+"ms) ==="); + } + + @Override + public void onDisable() { + // TODO Auto-generated method stub + + } + + // -------------------------------------------- // + // Integration with other plugins + // -------------------------------------------- // + + private void setupPermissions() { + Plugin test = this.getServer().getPluginManager().getPlugin("Permissions"); + + if (Permissions != null) { + return; + } + + if (test != null) { + Permissions = ((Permissions)test).getHandler(); + Factions.log("Found and will use plugin "+((Permissions)test).getDescription().getFullName()); + } else { + Factions.log("Permission system not detected, defaulting to OP"); + } + } + + private void setupHelp() { + Plugin test = this.getServer().getPluginManager().getPlugin("Help"); + + if (helpPlugin != null) { + return; + } + + if (test != null) { + helpPlugin = ((Help) test); + Factions.log("Found and will use plugin "+helpPlugin.getDescription().getFullName()); + for(FCommand fcommand : commands) { + fcommand.helpRegister(); + } + helpPlugin.registerCommand("help vampire", "help for the vampire plugin.", helpPlugin, true); + } else { + Factions.log(Level.WARNING, "'Help' plugin isn't detected. No /help support."); + } + } + + + // -------------------------------------------- // + // Commands + // -------------------------------------------- // + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { + List parameters = new ArrayList(Arrays.asList(args)); + this.handleCommand(sender, parameters); + return true; + } + + public void handleCommand(CommandSender sender, List parameters) { + if (parameters.size() == 0) { + this.commands.get(0).execute(sender, parameters); + return; + } + + String commandName = parameters.get(0).toLowerCase(); + parameters.remove(0); + + for (FCommand fcommand : this.commands) { + if (fcommand.getAliases().contains(commandName)) { + fcommand.execute(sender, parameters); + return; + } + } + + sender.sendMessage(Conf.colorSystem+"Unknown faction command \""+commandName+"\". Try /help faction"); // TODO test help messages exists.... + //TODO should we use internal help system instead? + } + + // -------------------------------------------- // + // Logging + // -------------------------------------------- // + public static void log(String msg) { + log(Level.INFO, msg); + } + + public static void log(Level level, String msg) { + Logger.getLogger("Minecraft").log(level, "["+instance.getDescription().getFullName()+"] "+msg); } } diff --git a/src/com/bukkit/mcteam/factions/commands/FCommand.java b/src/com/bukkit/mcteam/factions/commands/FCommand.java new file mode 100644 index 00000000..0059e5f8 --- /dev/null +++ b/src/com/bukkit/mcteam/factions/commands/FCommand.java @@ -0,0 +1,142 @@ +package com.bukkit.mcteam.factions.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.bukkit.mcteam.factions.Conf; +import com.bukkit.mcteam.factions.FPlayer; +import com.bukkit.mcteam.factions.Factions; + +public class FCommand { + public List requiredParameters; + public List optionalParameters; + + public String permissions; + + public String helpNameAndParams; + public String helpDescription; + + public CommandSender sender; + public boolean senderMustBePlayer; + public Player player; + public FPlayer fplayer; + + public List parameters; + + + public FCommand() { + requiredParameters = new ArrayList(); + optionalParameters = new ArrayList(); + + permissions = ""; + + senderMustBePlayer = false; + + helpNameAndParams = "fail!"; + helpDescription = "no description"; + } + + public ArrayList getAliases() { + String name = this.getClass().getName().toLowerCase(); + if (name.lastIndexOf('.') > 0) { + name = name.substring(name.lastIndexOf('.')+1); + } + + name = name.substring(8); + + ArrayList aliases = new ArrayList(); + aliases.add(name); + + return aliases; + } + + public String getBaseName() { + // TODO fetch from the plugin.yaml or something... + return "f"; + } + + public void execute(CommandSender sender, List parameters) { + this.sender = sender; + this.parameters = parameters; + + if ( ! validateCall()) { + sendMessage("try /help factions"); + return; + } + + if (this.senderMustBePlayer) { + this.player = (Player)sender; + this.fplayer = FPlayer.get(this.player); + } + + perform(); + } + + public void perform() { + + } + + public void helpRegister() { + Factions.helpPlugin.registerCommand(this.getBaseName()+ " " +this.helpNameAndParams, this.helpDescription, Factions.instance, false, permissions); + } + + public void sendMessage(String message) { + sender.sendMessage(Conf.colorSystem+message); + } + + public void sendMessage(List messages) { + for(String message : messages) { + this.sendMessage(message); + } + } + + // Test if the number of params is correct. + public boolean validateCall() { + if( ! testPermission(sender)) { + sendMessage("You do not have sufficient permissions to use this command."); + return false; + } + + if ( this.senderMustBePlayer && ! (sender instanceof Player)) { + sendMessage("This command can only be used by ingame players."); + return false; + } + + if (parameters.size() < requiredParameters.size()) { + int missing = requiredParameters.size() - parameters.size(); + sendMessage("Missing parameters. You must enter "+missing+" more."); + return false; + } + + if (parameters.size() > requiredParameters.size() + optionalParameters.size()) { + sendMessage("To many parameters."); + return false; + } + + return true; + } + + public boolean testPermission(CommandSender sender) { + if (sender.isOp()) { + return true; + } + + if (this.permissions.length() == 0) { + return true; + } + + if ( ! (sender instanceof Player)) { + return false; + } + + if (Factions.Permissions == null) { + return false; + } + + Player player = (Player)sender; + return Factions.Permissions.has(player, this.permissions); + } +} diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java b/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java new file mode 100644 index 00000000..e6add17e --- /dev/null +++ b/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java @@ -0,0 +1,45 @@ +package com.bukkit.mcteam.factions.commands; + +import java.util.ArrayList; + +import org.bukkit.entity.Player; + +import com.bukkit.mcteam.factions.Conf; +import com.bukkit.mcteam.factions.FPlayer; +import com.bukkit.mcteam.factions.Faction; + +public class FCommandLeave extends FCommand { + + public FCommandLeave() { + requiredParameters = new ArrayList(); + optionalParameters = new ArrayList(); + + permissions = ""; + + senderMustBePlayer = true; + + helpNameAndParams = "leave"; + helpDescription = "Leave your faction"; + } + + public void perform() { + Faction faction = fplayer.getFaction(); + + ArrayList errors = fplayer.leave(); + fplayer.sendMessage(errors); + + if (errors.size() == 0) { + faction.sendMessage(fplayer.getNameAndRelevant(faction)+Conf.colorSystem+" left your faction."); + fplayer.sendMessage("You left "+faction.getTag(fplayer)); + } + + if (faction.getFollowersAll().size() == 0) { + // Remove this faction + for (FPlayer follower : FPlayer.getAll()) { + follower.sendMessage(Conf.colorSystem+"The faction "+faction.getTag(follower)+Conf.colorSystem+" was disbanded."); + } + Faction.delete(faction.id); + } + } + +} diff --git a/src/com/bukkit/mcteam/factions/entities/EM.java b/src/com/bukkit/mcteam/factions/entities/EM.java index 25a6754e..ba5b941b 100644 --- a/src/com/bukkit/mcteam/factions/entities/EM.java +++ b/src/com/bukkit/mcteam/factions/entities/EM.java @@ -7,6 +7,10 @@ import java.util.*; import org.bukkit.World; import org.bukkit.entity.Player; +import com.bukkit.mcteam.factions.Board; +import com.bukkit.mcteam.factions.Conf; +import com.bukkit.mcteam.factions.FPlayer; +import com.bukkit.mcteam.factions.Faction; import com.bukkit.mcteam.factions.Factions; import com.bukkit.mcteam.factions.util.*; import com.bukkit.mcteam.util.DiscUtil; @@ -18,14 +22,14 @@ import com.bukkit.mcteam.gson.*; * The methods assume that all on disc is loaded into memory. */ public class EM { - protected static Map followers = new HashMap(); // Where String is a lowercase playername + protected static Map followers = new HashMap(); // Where String is a lowercase playername protected static Map factions = new HashMap(); // Where Integer is a primary auto increment key protected static Map boards = new HashMap(); // Where Long is the semi (sadly) unique world id. protected static int nextFactionId; // hardcoded config protected final static String ext = ".json"; - protected final static File folderBase = Factions.factions.getDataFolder(); + protected final static File folderBase = Factions.instance.getDataFolder(); protected final static File folderFaction = new File(folderBase, "faction"); protected final static File folderFollower = new File(folderBase, "follower"); protected final static File folderBoard = new File(folderBase, "board"); @@ -218,7 +222,7 @@ public class EM { String name = jsonFile.getName(); name = name.substring(0, name.length() - ext.length()); try { - Follower follower = gson.fromJson(DiscUtil.read(jsonFile), Follower.class); + FPlayer follower = gson.fromJson(DiscUtil.read(jsonFile), FPlayer.class); follower.id = name; followers.put(follower.id, follower); //Log.debug("loaded follower "+name); @@ -229,7 +233,7 @@ public class EM { } } - public static Collection followerGetAll() { + public static Collection followerGetAll() { return followers.values(); } @@ -237,7 +241,7 @@ public class EM { * This method returns the follower object for a player * A new Follower will be created if the player did not have one */ - public static Follower followerGet(Player player) { + public static FPlayer followerGet(Player player) { String key = followerKey(player); if (followers.containsKey(key)) { @@ -272,9 +276,9 @@ public class EM { return player.getName(); } - protected static Follower followerCreate(Player player) { + protected static FPlayer followerCreate(Player player) { Log.debug("Creating new follower "+followerKey(player)); - Follower follower = new Follower(); + FPlayer follower = new FPlayer(); follower.id = followerKey(player); followers.put(follower.id, follower); follower.save(); diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java index 94b28350..0c436fd0 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java @@ -11,6 +11,11 @@ import org.bukkit.event.block.BlockInteractEvent; import org.bukkit.event.block.BlockListener; import org.bukkit.event.block.BlockPlaceEvent; +import com.bukkit.mcteam.factions.Board; +import com.bukkit.mcteam.factions.Conf; +import com.bukkit.mcteam.factions.Coord; +import com.bukkit.mcteam.factions.FPlayer; +import com.bukkit.mcteam.factions.Faction; import com.bukkit.mcteam.factions.Factions; import com.bukkit.mcteam.factions.entities.*; import com.bukkit.mcteam.factions.util.*; @@ -35,7 +40,7 @@ public class FactionsBlockListener extends BlockListener { } //special cases, check for destruction of: torch, redstone torch (on & off), repeater (on & off), redstonewire, sapling, crops, sugar cane - private static Set specialBlocks = new HashSet(Arrays.asList( + private static Set specialBlocks = new HashSet(Arrays.asList( new Integer[] {50, 75, 76, 93, 94, 55, 6, 59, 83} )); @@ -62,7 +67,7 @@ public class FactionsBlockListener extends BlockListener { return true; // This is no faction territory. You may build or break stuff here. } - Follower me = Follower.get(player); + FPlayer me = FPlayer.get(player); Faction myFaction = me.getFaction(); // Cancel if we are not in our own territory @@ -105,7 +110,7 @@ public class FactionsBlockListener extends BlockListener { return true; } - Follower me = Follower.get(player); + FPlayer me = FPlayer.get(player); Faction myFaction = me.getFaction(); Coord blockCoord = Coord.from(block.getLocation()); Faction otherFaction = Board.get(player.getWorld()).getFactionAt(blockCoord); diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsEntityListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsEntityListener.java index f521bff8..1b366940 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsEntityListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsEntityListener.java @@ -14,11 +14,11 @@ import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityListener; +import com.bukkit.mcteam.factions.Board; +import com.bukkit.mcteam.factions.Conf; +import com.bukkit.mcteam.factions.Coord; +import com.bukkit.mcteam.factions.FPlayer; import com.bukkit.mcteam.factions.Factions; -import com.bukkit.mcteam.factions.entities.Board; -import com.bukkit.mcteam.factions.entities.Conf; -import com.bukkit.mcteam.factions.entities.Coord; -import com.bukkit.mcteam.factions.entities.Follower; import com.bukkit.mcteam.factions.struct.Relation; public class FactionsEntityListener extends EntityListener { @@ -35,7 +35,7 @@ public class FactionsEntityListener extends EntityListener { } Player player = (Player) entity; - Follower follower = Follower.get(player); + FPlayer follower = FPlayer.get(player); follower.onDeath(); follower.sendMessage(Conf.colorSystem+"Your power is now "+follower.getPowerRounded()+" / "+follower.getPowerMaxRounded()); } @@ -98,8 +98,8 @@ public class FactionsEntityListener extends EntityListener { return true; } - Follower defender = Follower.get((Player)damagee); - Follower attacker = Follower.get((Player)damager); + FPlayer defender = FPlayer.get((Player)damagee); + FPlayer attacker = FPlayer.get((Player)damager); Relation relation = defender.getRelation(attacker); //Log.debug(attacker.getName() + " attacked " + defender.getName()); diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java index 7853d707..a6273cd8 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java @@ -11,7 +11,12 @@ import org.bukkit.event.player.PlayerItemEvent; import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerMoveEvent; +import com.bukkit.mcteam.factions.Board; import com.bukkit.mcteam.factions.Commands; +import com.bukkit.mcteam.factions.Conf; +import com.bukkit.mcteam.factions.Coord; +import com.bukkit.mcteam.factions.FPlayer; +import com.bukkit.mcteam.factions.Faction; import com.bukkit.mcteam.factions.Factions; import com.bukkit.mcteam.factions.entities.*; import com.bukkit.mcteam.factions.util.TextUtil; @@ -55,7 +60,7 @@ public class FactionsPlayerListener extends PlayerListener{ } // ... it was not a command. This means that it is a chat message! - Follower me = Follower.get(talkingPlayer); + FPlayer me = FPlayer.get(talkingPlayer); // Is it a faction chat message? if (me.isFactionChatting()) { @@ -83,8 +88,8 @@ public class FactionsPlayerListener extends PlayerListener{ // Why? Because the relations will differ. event.setCancelled(true); - for (Player listeningPlayer : Factions.factions.getServer().getOnlinePlayers()) { - Follower you = Follower.get(listeningPlayer); + for (Player listeningPlayer : Factions.instance.getServer().getOnlinePlayers()) { + FPlayer you = FPlayer.get(listeningPlayer); String yourFormat = formatStart + me.getChatTag(you) + formatEnd; listeningPlayer.sendMessage(String.format(yourFormat, talkingPlayer.getDisplayName(), msg)); } @@ -102,7 +107,7 @@ public class FactionsPlayerListener extends PlayerListener{ ArrayList tokens = TextUtil.split(msg.trim()); if (Conf.aliasBase.contains(tokens.get(0))) { tokens.remove(0); - Follower follower = Follower.get(player); + FPlayer follower = FPlayer.get(player); Commands.base(follower, tokens); return true; } @@ -116,14 +121,14 @@ public class FactionsPlayerListener extends PlayerListener{ @Override public void onPlayerQuit(PlayerEvent event) { - Follower follower = Follower.get(event.getPlayer()); + FPlayer follower = FPlayer.get(event.getPlayer()); Log.debug("Saved follower on player quit: "+follower.getName()); follower.save(); // We save the followers on logout in order to save their non autosaved state like power. } @Override public void onPlayerMove(PlayerMoveEvent event) { - Follower me = Follower.get(event.getPlayer()); + FPlayer me = FPlayer.get(event.getPlayer()); // Did we change coord? Coord coordFrom = me.lastStoodInCoord; @@ -168,7 +173,7 @@ public class FactionsPlayerListener extends PlayerListener{ } //currently checking placement/use of: redstone, sign, flint&steel, beds (not currently detected by Bukkit), buckets (empty, water, lava), repeater (not currently detected by Bukkit) - private static Set badItems = new HashSet(Arrays.asList( + private static Set badItems = new HashSet(Arrays.asList( new Integer[] {331, 323, 259, 355, 325, 326, 327, 356} )); @@ -185,7 +190,7 @@ public class FactionsPlayerListener extends PlayerListener{ return true; // This is not faction territory. Use whatever you like here. } - Follower me = Follower.get(player); + FPlayer me = FPlayer.get(player); Faction myFaction = me.getFaction(); // Cancel if we are not in our own territory diff --git a/src/com/bukkit/mcteam/factions/struct/Relation.java b/src/com/bukkit/mcteam/factions/struct/Relation.java index f3626df7..8b231b18 100644 --- a/src/com/bukkit/mcteam/factions/struct/Relation.java +++ b/src/com/bukkit/mcteam/factions/struct/Relation.java @@ -2,7 +2,7 @@ package com.bukkit.mcteam.factions.struct; import org.bukkit.ChatColor; -import com.bukkit.mcteam.factions.entities.*; +import com.bukkit.mcteam.factions.Conf; public enum Relation { MEMBER(3, "member"), diff --git a/src/com/bukkit/mcteam/factions/struct/Role.java b/src/com/bukkit/mcteam/factions/struct/Role.java index 140afadc..e73a7039 100644 --- a/src/com/bukkit/mcteam/factions/struct/Role.java +++ b/src/com/bukkit/mcteam/factions/struct/Role.java @@ -1,6 +1,6 @@ package com.bukkit.mcteam.factions.struct; -import com.bukkit.mcteam.factions.entities.Conf; +import com.bukkit.mcteam.factions.Conf; public enum Role { ADMIN(2, "admin"), diff --git a/src/com/bukkit/mcteam/factions/util/Log.java b/src/com/bukkit/mcteam/factions/util/Log.java deleted file mode 100644 index 9517e638..00000000 --- a/src/com/bukkit/mcteam/factions/util/Log.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.bukkit.mcteam.factions.util; - -import java.util.*; - -import org.bukkit.entity.Player; - -import com.bukkit.mcteam.factions.Factions; - -public class Log { - public static String prefix = Factions.factions.getDescription().getName(); - public static ArrayList debuggers = new ArrayList(); - public static int threshold = 10; - - public static void log(int level, String prefix, String msg) { - if (threshold <= level) { - msg = Log.prefix+prefix+msg; - System.out.println(msg); - for(Player debugger : debuggers) { - debugger.sendMessage(msg); - } - } - } - - public static void debug (String msg) { - log(10, " debug: ", msg); - } - - public static void info (String msg) { - log(20, " info: ", msg); - } - - public static void warn (String msg) { - log(30, " warn: ", msg); - } - - public static void severe (String msg) { - log(40, " severe: ", msg); - } -} diff --git a/src/com/bukkit/mcteam/factions/util/TextUtil.java b/src/com/bukkit/mcteam/factions/util/TextUtil.java index 0cff1ef3..88deb96b 100644 --- a/src/com/bukkit/mcteam/factions/util/TextUtil.java +++ b/src/com/bukkit/mcteam/factions/util/TextUtil.java @@ -3,7 +3,7 @@ import java.util.*; import org.bukkit.Material; -import com.bukkit.mcteam.factions.entities.*; +import com.bukkit.mcteam.factions.Conf; public class TextUtil { public static String titleize(String str) { diff --git a/src/com/bukkit/mcteam/util/ChatFixUtil.java b/src/com/bukkit/mcteam/util/ChatFixUtil.java deleted file mode 100644 index 3345f197..00000000 --- a/src/com/bukkit/mcteam/util/ChatFixUtil.java +++ /dev/null @@ -1,230 +0,0 @@ -package com.bukkit.mcteam.util; - -import java.util.*; - -import org.bukkit.entity.Player; - -/** - * The purpose of this tool is twofold: - * 1: Avoid client crashes due to bad color formating. - * 2: Make color continue on word wrapping - * - * In minecraft the degree sign is used as a prefix to another char to create a color. - * For example the code for white is "\u00A7f". - * The "\u00A7" is the unicode notation for the degree sign and the "f" means white. - * - * When does minecraft wrap the text? After how many chars? - * Answer: - * Because the font isn't monospace this differs depending on what you write. - * However we can fit 53 "M" without wrapping and the 54th char would then wrap (be at the beginning of the next line instead) - * As there is no broader char than "M" we can know for sure the minimum line length is 53. - * Note that this means the number of DISPLAYED chars per row is 53. - * A degree sign and the char after will NOT count, as they will not be displayed as chars. - * - * Good to know: Numbers have the same font width as an M. - * - * When does the client crash? - * Answer: - * When a row ends with a degree char and optionally another sign after. - * Another way to say the same: When a line ends with either a broken or valid color notation. - * AND - * The client will ALWAYS crash if the sign after the last displayed char in a row is a degree char. - * A goofy way to explatin it: - * For a line with only "M" and numbers, the fiftyfourth "displayed char" musn't be a degree sign. - * - * WARNING: - * Above is a hypothesis I have created based on what my experiments have shown. - * I am fairly sure it is correct but please help me test it further. - */ -public class ChatFixUtil { - public final static char deg = '\u00A7'; - public final static int lineLength = 53; - - /** - * This method wraps the msg for you at row lengths of 53, - * avoids client crash scenarios and makes the previous color continue on - * the next line. - * - * The upsides with filtering your messages through this method are: - * - No client crashes. - * - Line wrapping with preserved color. - * - * The downsides are: - * - The width of the chat window will not be used to it's fullest. - * For example you can fit more that 53 commas (,) in a chatwindow row - * but the line would break after 53 displayed chars. - * - * Suggested usage: - * NO NEED TO USE the fix method for static help pages in your plugin. - * As the text is static you can make sure there is no client crash yourself - * and be able to use the full line length. - * - * DO USE in cases like where you output colored messages with playernames in your - * plugin. As the player names have different length there is potential for client crash. - */ - public static ArrayList fix(String msg) { - // Make sure the end of msg is good - msg = cleanMsgEnding(msg); - - ArrayList ret = new ArrayList(); - int displen = 0; // The number of displayed chars in row so far. - String row = ""; - String latestColor = null; - - for (int i = 0; i < msg.length(); i++) { - if (displen == lineLength) { - // it is time to start on the next row! - ret.add(row); - displen = 0; - row = ""; - if (latestColor != null) { - row += deg+latestColor; - } - } - char c = msg.charAt(i); - - if (c == deg) { - latestColor = String.valueOf(msg.charAt(i+1)); - row += deg+latestColor; - i++; - } else { - displen += 1; - row += c; - } - } - ret.add(row); - return ret; - } - - public static ArrayList fix(List messages) { - ArrayList ret = new ArrayList(); - for(String message : messages) { - ret.addAll(fix(message)); - } - return ret; - } - - - /** - * Removes the ending chars as long as they are deg or deg+'anychar' or a space - * As I see it we would never want those chars at the end of a msg. - */ - protected static String cleanMsgEnding (String msg) { - - while (msg.length() > 0) { - if (msg.endsWith(String.valueOf(deg)) || msg.endsWith(" ")) { - msg = msg.substring(0, msg.length()-1); - } else if (msg.length() >= 2 && msg.charAt(msg.length() - 2) == deg) { - msg = msg.substring(0, msg.length()-2); - } else { - break; - } - } - return msg; - } - - /** - * This test util assumes line break after 53 displayed chars. - * The fix method above breaks like that so this method should - * be a valid way to test if a message row would crash a client. - */ - public static String thisMsgWouldCrashClient(String str) { - // There would always be crash if we end with deg or deg+'anychar' - if (str.length() >= 1 && str.charAt(str.length() - 1) == deg) { - return "Crash: The str ends with deg."; - } else if (str.length() >= 2 && str.charAt(str.length() - 2) == deg) { - return "Crash: The str ends with deg+'anychar'."; - } - - int displayedChars = 0; - - for (int i = 0; i < str.length(); i++) { - char c = str.charAt(i); - if (c == deg && displayedChars == lineLength) { - return "Crash: Deg as fiftyforth \"displayed\" char"; - } else if (c == deg) { - i++; // this and next: they are not displayed... skip them... - } else { - displayedChars += 1; - } - } - return "all ok"; - } - - //----------------------------------------------// - // Methods for effectively sending messages - //----------------------------------------------// - //----------------------------------------------// - // One player - //----------------------------------------------// - public static void sendMessage(Player player, String message, boolean fix) { - if (fix) { - List messages = ChatFixUtil.fix(message); - sendMessage(player, messages, false); - } else { - if (player != null) { - player.sendMessage(message); - } - } - } - public static void sendMessage(Player player, List messages, boolean fix) { - if (fix) { - messages = ChatFixUtil.fix(messages); - } - for (String message : messages) { - sendMessage(player, message, false); - } - } - public static void sendMessage(Player player, String message) { - sendMessage(player, message, true); - } - public static void sendMessage(Player player, List messages) { - sendMessage(player, messages, true); - } - //----------------------------------------------// - // Many Players - //----------------------------------------------// - public static void sendMessage(Collection players, String message, boolean fix) { - if (fix) { - List messages = ChatFixUtil.fix(message); - sendMessage(players, messages, false); - } else { - for (Player player : players) { - sendMessage(player, message, false); - } - } - } - public static void sendMessage(Collection players, List messages, boolean fix) { - if (fix) { - messages = ChatFixUtil.fix(messages); - } - - for (String message : messages) { - sendMessage(players, message, false); - } - } - public static void sendMessage(Collection players, String message) { - sendMessage(players, message, true); - } - public static void sendMessage(Collection players, List messages) { - sendMessage(players, messages, true); - } -} - - - - - - - - - - - - - - - - - -