From 4e964ba19419d12862706455bf7acb8d6c86d929 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 22 Mar 2011 17:20:21 +0100 Subject: [PATCH] Fewer and fewer errors... --- src/com/bukkit/mcteam/factions/Board.java | 6 +- src/com/bukkit/mcteam/factions/FLocation.java | 14 +- src/com/bukkit/mcteam/factions/FPlayer.java | 100 ++- src/com/bukkit/mcteam/factions/Faction.java | 238 +++-- src/com/bukkit/mcteam/factions/Factions.java | 6 +- .../factions/commands/FBaseCommand.java | 8 +- .../factions/commands/FCommandAdmin.java | 6 +- .../factions/commands/FCommandClaim.java | 4 +- .../factions/commands/FCommandCreate.java | 4 +- .../factions/commands/FCommandDeinvite.java | 2 +- .../factions/commands/FCommandInvite.java | 2 +- .../factions/commands/FCommandJoin.java | 6 +- .../factions/commands/FCommandKick.java | 4 +- .../factions/commands/FCommandLeave.java | 4 +- .../factions/commands/FCommandList.java | 6 +- .../mcteam/factions/commands/FCommandMod.java | 6 +- .../factions/commands/FCommandOpen.java | 2 +- .../factions/commands/FCommandShow.java | 2 +- .../mcteam/factions/commands/FCommandTag.java | 2 +- .../factions/commands/FRelationCommand.java | 2 +- .../mcteam/factions/entities/CommandsOld.java | 845 ------------------ .../mcteam/factions/entities/CoordOld.java | 74 -- .../mcteam/factions/entities/EMOld.java | 405 --------- .../listeners/FactionsBlockListener.java | 39 +- .../listeners/FactionsEntityListener.java | 45 +- .../listeners/FactionsPlayerListener.java | 8 - 26 files changed, 247 insertions(+), 1593 deletions(-) delete mode 100644 src/com/bukkit/mcteam/factions/entities/CommandsOld.java delete mode 100644 src/com/bukkit/mcteam/factions/entities/CoordOld.java delete mode 100644 src/com/bukkit/mcteam/factions/entities/EMOld.java diff --git a/src/com/bukkit/mcteam/factions/Board.java b/src/com/bukkit/mcteam/factions/Board.java index 5c4caa8f..015f96de 100644 --- a/src/com/bukkit/mcteam/factions/Board.java +++ b/src/com/bukkit/mcteam/factions/Board.java @@ -55,7 +55,7 @@ public class Board { } public static void setFactionAt(Faction faction, FLocation flocation) { - setIdAt(faction.id, flocation); + setIdAt(faction.getId(), flocation); } public static void removeAt(FLocation flocation) { @@ -112,7 +112,7 @@ public class Board { } public static int getFactionCoordCount(Faction faction) { - return getFactionCoordCount(faction.id); + return getFactionCoordCount(faction.getId()); } //----------------------------------------------// @@ -144,7 +144,7 @@ public class Board { } else { FLocation flocationHere = topLeft.getRelative(dx, dz); Faction factionHere = getFactionAt(flocationHere); - if (factionHere.id == 0) { + if (factionHere.getId() == 0) { row += ChatColor.GRAY+"-"; } else { row += factionHere.getRelation(faction).getColor()+"+"; diff --git a/src/com/bukkit/mcteam/factions/FLocation.java b/src/com/bukkit/mcteam/factions/FLocation.java index 7672b8cc..ab807bcf 100644 --- a/src/com/bukkit/mcteam/factions/FLocation.java +++ b/src/com/bukkit/mcteam/factions/FLocation.java @@ -7,8 +7,8 @@ import org.bukkit.entity.Player; public class FLocation { private String worldName = "world"; - private long x = 0; - private long z = 0; + private int x = 0; + private int z = 0; private final static transient double cellSize = 16; @@ -20,14 +20,14 @@ public class FLocation { } - public FLocation(String worldName, long x, long z) { + public FLocation(String worldName, int x, int z) { this.worldName = worldName; this.x = x; this.z = z; } public FLocation(Location location) { - this(location.getWorld().getName(), (long) Math.floor(location.getX() / cellSize) , (long) Math.floor(location.getZ() / cellSize)); + this(location.getWorld().getName(), (int) Math.floor(location.getX() / cellSize) , (int) Math.floor(location.getZ() / cellSize)); } public FLocation(Player player) { @@ -58,7 +58,7 @@ public class FLocation { return x; } - public void setX(long x) { + public void setX(int x) { this.x = x; } @@ -66,7 +66,7 @@ public class FLocation { return z; } - public void setZ(long z) { + public void setZ(int z) { this.z = z; } @@ -86,8 +86,6 @@ public class FLocation { // Comparison //----------------------------------------------// - // TODO hash code - @Override public boolean equals(Object obj) { if (obj == this) diff --git a/src/com/bukkit/mcteam/factions/FPlayer.java b/src/com/bukkit/mcteam/factions/FPlayer.java index d0ea9f50..ae3d500f 100644 --- a/src/com/bukkit/mcteam/factions/FPlayer.java +++ b/src/com/bukkit/mcteam/factions/FPlayer.java @@ -27,20 +27,29 @@ import com.bukkit.mcteam.util.DiscUtil; */ 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 FLocation lastStoodAt = new FLocation(); // Where did this player stand the last time we checked? + // -------------------------------------------- // + // Fields + // -------------------------------------------- // - public int factionId; - public Role role; + private static transient Map instances = new HashMap(); + private static transient File file = new File(Factions.instance.getDataFolder(), "players.json"); + + private transient String playerName; + private transient FLocation lastStoodAt = new FLocation(); // Where did this player stand the last time we checked? + + private int factionId; + private Role role; private String title; private double power; private long lastPowerUpdateTime; private transient boolean mapAutoUpdating; private boolean factionChatting; + // -------------------------------------------- // + // Construct + // -------------------------------------------- // + public FPlayer(Player player) { this.playerName = player.getName().toLowerCase(); } @@ -64,18 +73,20 @@ public class FPlayer { this.title = ""; } + // -------------------------------------------- // + // Minecraft Player + // -------------------------------------------- // + public Player getPlayer() { return Factions.instance.getServer().getPlayer(playerName); } + + // TODO lowercase vs mixedcase for logged in chars... public String getPlayerName() { return this.playerName; } - // -------------------------------------------- // - // Online / Offline State Checking - // -------------------------------------------- // - public boolean isOnline() { return Factions.instance.getServer().getPlayer(playerName) != null; } @@ -84,6 +95,30 @@ public class FPlayer { return ! isOnline(); } + // -------------------------------------------- // + // Getters And Setters + // -------------------------------------------- // + + public Faction getFaction() { + return Faction.get(factionId); + } + + public void setFaction(Faction faction) { + this.factionId = faction.getId(); + } + + public boolean hasFaction() { + return factionId != 0; + } + + public Role getRole() { + return this.role; + } + + public void setRole(Role role) { + this.role = role; + } + public boolean isFactionChatting() { if (this.factionId == 0) { return false; @@ -94,8 +129,6 @@ public class FPlayer { public void setFactionChatting(boolean factionChatting) { this.factionChatting = factionChatting; } - - public boolean isMapAutoUpdating() { return mapAutoUpdating; @@ -105,6 +138,14 @@ public class FPlayer { this.mapAutoUpdating = mapAutoUpdating; } + public FLocation getLastStoodAt() { + return this.lastStoodAt; + } + + public void setLastStoodAt(FLocation flocation) { + this.lastStoodAt = flocation; + } + //----------------------------------------------// // Title, Name, Faction Tag and Chat //----------------------------------------------// @@ -300,7 +341,7 @@ public class FPlayer { // Territory //----------------------------------------------// public boolean isInOwnTerritory() { - return Board.getIdAt(new FLocation(this)) == this.factionId; + return Board.getFactionAt(new FLocation(this)) == this.getFaction(); } public boolean isInOthersTerritory() { @@ -311,23 +352,25 @@ public class FPlayer { public void sendFactionHereMessage() { Faction factionHere = Board.getFactionAt(new FLocation(this)); String msg = Conf.colorSystem+" ~ "+factionHere.getTag(this); - if (factionHere.id != 0) { + if (factionHere.getId() != 0) { msg += " - "+factionHere.getDescription(); } this.sendMessage(msg); } - //----------------------------------------------// - // Faction management - //----------------------------------------------// - public Faction getFaction() { - return Faction.get(factionId); + // -------------------------------------------- // + // Messages + // -------------------------------------------- // + public void sendMessage(String message) { + this.getPlayer().sendMessage(Conf.colorSystem + message); } - public boolean hasFaction() { - return factionId != 0; + public void sendMessage(List messages) { + for(String message : messages) { + this.sendMessage(message); + } } - + // -------------------------------------------- // // Get and search // -------------------------------------------- // @@ -368,19 +411,6 @@ public class FPlayer { return null; } - // -------------------------------------------- // - // 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 // -------------------------------------------- // diff --git a/src/com/bukkit/mcteam/factions/Faction.java b/src/com/bukkit/mcteam/factions/Faction.java index 33878e2c..0f30a172 100644 --- a/src/com/bukkit/mcteam/factions/Faction.java +++ b/src/com/bukkit/mcteam/factions/Faction.java @@ -10,7 +10,6 @@ import java.util.logging.Level; import org.bukkit.ChatColor; import org.bukkit.entity.Player; -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.*; @@ -18,16 +17,25 @@ 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; - public Set invites; // Where string is a follower id (lower case name) - protected boolean open; - protected String tag; - protected String description; + // -------------------------------------------- // + // Fields + // -------------------------------------------- // + + private static transient Map instances = new HashMap(); + private static transient File file = new File(Factions.instance.getDataFolder(), "factions.json"); + private static transient int nextId; + + private transient int id; + private Map relationWish; + private Set invites; // Where string is a follower id (lower case name) + private boolean open; + private String tag; + private String description; + + // -------------------------------------------- // + // Construct + // -------------------------------------------- // public Faction() { this.relationWish = new HashMap(); @@ -37,6 +45,69 @@ public class Faction { this.description = "Default faction description :("; } + // -------------------------------------------- // + // Getters And Setters + // -------------------------------------------- // + + public int getId() { + return this.id; + } + + // ------------------------------- + // Invites + // ------------------------------- + + public void invite(FPlayer fplayer) { + this.invites.add(fplayer.getName()); //TODO Lowercase paradigm shit.... + } + + public void deinvite(FPlayer fplayer) { + this.invites.remove(fplayer.getName()); //TODO Lowercase paradigm shit.... + } + + public boolean isInvited(FPlayer fplayer) { + return this.invites.contains(fplayer.getName()); //TODO Lowercase paradigm shit.... + } + + // ------------------------------- + // Relation and relation colors TODO + // ------------------------------- + + public Relation getRelationWish(Faction otherFaction) { + if (this.relationWish.containsKey(otherFaction.getId())){ + return this.relationWish.get(otherFaction.getId()); + } + return Relation.NEUTRAL; + } + + public void setRelationWish(Faction otherFaction, Relation relation) { + if (this.relationWish.containsKey(otherFaction.getId()) && relation.equals(Relation.NEUTRAL)){ + this.relationWish.remove(otherFaction.getId()); + } else { + this.relationWish.put(otherFaction.getId(), relation); + } + Faction.save(); + } + + public Relation getRelation(Faction otherFaction) { + if (otherFaction.getId() == 0 || this.getId() == 0) { + return Relation.NEUTRAL; + } + if (otherFaction.equals(this)) { + return Relation.MEMBER; + } + if(this.getRelationWish(otherFaction).value >= otherFaction.getRelationWish(this).value) { + return otherFaction.getRelationWish(this); + } + return this.getRelationWish(otherFaction); + } + + public Relation getRelation(FPlayer follower) { + return getRelation(follower.getFaction()); + } + + + // ------------------------------- // Information // ------------------------------- @@ -106,62 +177,13 @@ public class Faction { } public int getLandRounded() { - return Board.getFactionCoordCountAllBoards(this); + return Board.getFactionCoordCount(this); } public boolean hasLandInflation() { return this.getLandRounded() > this.getPowerRounded(); } - // ------------------------------- - // Membership management - // ------------------------------- - - - /*public ArrayList invite(FPlayer follower) { // TODO Move out - ArrayList errors = new ArrayList(); - - if (follower.getFaction().equals(this)) { // error här? - errors.add(Conf.colorSystem+follower.getName()+" is already a member of "+this.getTag()); - } - - if(errors.size() > 0) { - return errors; - } - - this.invites.add(follower.id); - this.save(); - return errors; - } - - public ArrayList deinvite(FPlayer follower) { // TODO move out! - ArrayList errors = new ArrayList(); - - if (follower.getFaction() == this) { - errors.add(Conf.colorSystem+follower.getName()+" is already a member of "+this.getTag()); - errors.add(Conf.colorSystem+"You might want to "+Conf.colorCommand+Conf.aliasBase.get(0)+" "+Conf.aliasKick.get(0)+Conf.colorParameter+" "+follower.getName()); - } - - if(errors.size() > 0) { - return errors; - } - - this.invites.remove(follower.id); - this.save(); - return errors; - }*/ - - public ArrayList kick(FPlayer follower) { - ArrayList errors = new ArrayList(); - removeFollower(follower); - return errors; - } - - - public boolean isInvited(FPlayer follower) { - return invites.contains(follower.id); - } - // ------------------------------- // Followers // ------------------------------- @@ -169,7 +191,7 @@ public class Faction { public ArrayList getFPlayers() { ArrayList ret = new ArrayList(); for (FPlayer follower : FPlayer.getAll()) { - if (follower.factionId == this.id) { + if (follower.getFaction() == this) { ret.add(follower); } } @@ -178,9 +200,9 @@ public class Faction { public ArrayList getFPlayersWhereOnline(boolean online) { ArrayList ret = new ArrayList(); - for (FPlayer follower : FPlayer.getAll()) { - if (follower.factionId == this.id && follower.isOnline() == online) { - ret.add(follower); + for (FPlayer fplayer : FPlayer.getAll()) { + if (fplayer.getFaction() == this && fplayer.isOnline() == online) { + ret.add(fplayer); } } return ret; @@ -189,9 +211,9 @@ public class Faction { public ArrayList getFPlayersWhereRole(Role role) { ArrayList ret = new ArrayList(); - for (FPlayer follower : FPlayer.getAll()) { - if (follower.factionId == this.id && follower.role.equals(role)) { - ret.add(follower); + for (FPlayer fplayer : FPlayer.getAll()) { + if (fplayer.getFaction() == this && fplayer.getRole() == role) { + ret.add(fplayer); } } @@ -214,7 +236,7 @@ public class Faction { ArrayList ret = new ArrayList(); for (Player player: Factions.instance.getServer().getOnlinePlayers()) { FPlayer follower = FPlayer.get(player); - if (follower.factionId == this.id) { + if (follower.getFaction() == this) { ret.add(player); } } @@ -264,57 +286,23 @@ public class Faction { } //----------------------------------------------// - // Messages - Directly connected to ChatFixUtil + // Messages //----------------------------------------------// - public void sendMessage(String message, boolean fix) { - ChatFixUtil.sendMessage(this.getOnlinePlayers(), message, fix); - } - public void sendMessage(List messages, boolean fix) { - ChatFixUtil.sendMessage(this.getOnlinePlayers(), messages, fix); - } public void sendMessage(String message) { - ChatFixUtil.sendMessage(this.getOnlinePlayers(), message, true); + for (FPlayer fplayer : this.getFPlayersWhereOnline(true)) { + fplayer.sendMessage(message); + } } + public void sendMessage(List messages) { - ChatFixUtil.sendMessage(this.getOnlinePlayers(), messages, true); + for (FPlayer fplayer : this.getFPlayersWhereOnline(true)) { + fplayer.sendMessage(messages); + } } - // ------------------------------- - // Relation and relation colors - // ------------------------------- - - public Relation getRelationWish(Faction otherFaction) { - if (this.relationWish.containsKey(otherFaction.id)){ - return this.relationWish.get(otherFaction.id); - } - return Relation.NEUTRAL; - } - - public void setRelationWish(Faction otherFaction, Relation relation) { - if (this.relationWish.containsKey(otherFaction.id) && relation.equals(Relation.NEUTRAL)){ - this.relationWish.remove(otherFaction.id); - } else { - this.relationWish.put(otherFaction.id, relation); - } - this.save(); - } - - public Relation getRelation(Faction otherFaction) { - if (otherFaction.id == 0 || this.id == 0) { - return Relation.NEUTRAL; - } - if (otherFaction.equals(this)) { - return Relation.MEMBER; - } - if(this.getRelationWish(otherFaction).value >= otherFaction.getRelationWish(this).value) { - return otherFaction.getRelationWish(this); - } - return this.getRelationWish(otherFaction); - } - - public Relation getRelation(FPlayer follower) { - return getRelation(follower.getFaction()); - } + //----------------------------------------------// + // Mudd TODO + //----------------------------------------------// public ChatColor getRelationColor(Faction otherFaction) { return this.getRelation(otherFaction).getColor(); @@ -324,6 +312,8 @@ public class Faction { return this.getRelation(follower).getColor(); } + + //----------------------------------------------// // Persistance and entity management //----------------------------------------------// @@ -385,7 +375,7 @@ public class Faction { public static Faction get(Integer factionId) { if ( ! instances.containsKey(factionId)) { Factions.log(Level.WARNING, "Non existing factionId "+factionId+" requested! Issuing board cleaning!"); - Board.cleanAll(); + Board.clean(); } return instances.get(factionId); } @@ -415,7 +405,7 @@ public class Faction { // purge from all boards // Board.purgeFactionFromAllBoards(id); - Board.cleanAll(); + Board.clean(); // Remove the file //File file = new File(folderFaction, id+ext); @@ -429,22 +419,4 @@ public class Faction { // TODO SAVE files return true; // TODO } - - /* - public static Faction create() { - return EM.factionCreate(); - } - - public static Faction get(Integer factionId) { - return EM.factionGet(factionId); - } - - public static Collection getAll() { - return EM.factionGetAll(); - } - - 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 5cbc9ab6..46be31ba 100644 --- a/src/com/bukkit/mcteam/factions/Factions.java +++ b/src/com/bukkit/mcteam/factions/Factions.java @@ -39,9 +39,9 @@ public class Factions extends JavaPlugin { .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE) .create(); - private final FactionsPlayerListener playerListener = new FactionsPlayerListener(this); - private final FactionsEntityListener entityListener = new FactionsEntityListener(this); - private final FactionsBlockListener blockListener = new FactionsBlockListener(this); + private final FactionsPlayerListener playerListener = new FactionsPlayerListener(); + private final FactionsEntityListener entityListener = new FactionsEntityListener(); + private final FactionsBlockListener blockListener = new FactionsBlockListener(); public static PermissionHandler Permissions; public static Help helpPlugin; diff --git a/src/com/bukkit/mcteam/factions/commands/FBaseCommand.java b/src/com/bukkit/mcteam/factions/commands/FBaseCommand.java index 5b896dc7..16625421 100644 --- a/src/com/bukkit/mcteam/factions/commands/FBaseCommand.java +++ b/src/com/bukkit/mcteam/factions/commands/FBaseCommand.java @@ -185,7 +185,7 @@ public class FBaseCommand { } public boolean assertMinRole(Role role) { - if (me.role.value < role.value) { + if (me.getRole().value < role.value) { sendMessage("You must be "+role+" to "+this.helpDescription+"."); return false; } @@ -246,13 +246,13 @@ public class FBaseCommand { return false; } - if (i.role.value > you.role.value || i.role.equals(Role.ADMIN) ) { + if (i.getRole().value > you.getRole().value || i.getRole().equals(Role.ADMIN) ) { return true; } - if (you.role.equals(Role.ADMIN)) { + if (you.getRole().equals(Role.ADMIN)) { i.sendMessage(Conf.colorSystem+"Only the faction admin can do that."); - } else if (i.role.equals(Role.MODERATOR)) { + } else if (i.getRole().equals(Role.MODERATOR)) { i.sendMessage(Conf.colorSystem+"Moderators can't control each other..."); } else { i.sendMessage(Conf.colorSystem+"You must be a faction moderator to do that."); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandAdmin.java b/src/com/bukkit/mcteam/factions/commands/FCommandAdmin.java index f9271bc6..d89c9892 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandAdmin.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandAdmin.java @@ -50,12 +50,12 @@ public class FCommandAdmin extends FBaseCommand { } - me.role = Role.MODERATOR; - you.role = Role.ADMIN; + me.setRole(Role.MODERATOR); + you.setRole(Role.ADMIN); // Inform all players for (FPlayer fplayer : FPlayer.getAllOnline()) { - if (fplayer.factionId == me.factionId) { + if (fplayer.getFaction() == me.getFaction()) { fplayer.sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" gave "+you.getNameAndRelevant(me)+Conf.colorSystem+" the leadership of your faction."); } else { fplayer.sendMessage(me.getNameAndRelevant(fplayer)+Conf.colorSystem+" gave "+you.getNameAndRelevant(fplayer)+Conf.colorSystem+" the leadership of "+myFaction.getTag(fplayer)); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandClaim.java b/src/com/bukkit/mcteam/factions/commands/FCommandClaim.java index 30527531..b66eaf8d 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandClaim.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandClaim.java @@ -51,7 +51,7 @@ public class FCommandClaim extends FBaseCommand { return; } - if (otherFaction.id != 0) { + if (otherFaction.getId() != 0) { if ( ! otherFaction.hasLandInflation()) { // TODO more messages WARN current faction most importantly sendMessage(me.getRelationColor(otherFaction)+otherFaction.getTag()+Conf.colorSystem+" owns this land and is strong enough to keep it."); return; @@ -63,7 +63,7 @@ public class FCommandClaim extends FBaseCommand { } } - if (otherFaction.id == 0) { + if (otherFaction.getId() == 0) { myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" claimed some new land :D"); } else { // ASDF claimed some of your land 450 blocks NNW of you. diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandCreate.java b/src/com/bukkit/mcteam/factions/commands/FCommandCreate.java index a385515c..2b40f0d9 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandCreate.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandCreate.java @@ -42,8 +42,8 @@ public class FCommandCreate extends FBaseCommand { Faction faction = Faction.create(); faction.setTag(tag); - me.role = Role.ADMIN; - me.factionId = faction.id; + me.setRole(Role.ADMIN); + me.setFaction(faction); Faction.save(); FPlayer.save(); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandDeinvite.java b/src/com/bukkit/mcteam/factions/commands/FCommandDeinvite.java index d77bd3b2..c6302fb4 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandDeinvite.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandDeinvite.java @@ -44,7 +44,7 @@ public class FCommandDeinvite extends FBaseCommand { return; } - myFaction.invites.remove(you.playerName); + myFaction.deinvite(you); Faction.save(); you.sendMessage(me.getNameAndRelevant(you)+Conf.colorSystem+" revoked your invitation to "+myFaction.getTag(you)); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandInvite.java b/src/com/bukkit/mcteam/factions/commands/FCommandInvite.java index 2257119b..5a8289b7 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandInvite.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandInvite.java @@ -45,7 +45,7 @@ public class FCommandInvite extends FBaseCommand { return; } - myFaction.invites.add(you.playerName); + myFaction.invite(you); Faction.save(); you.sendMessage(me.getNameAndRelevant(you)+Conf.colorSystem+" invited you to "+myFaction.getTag(you)); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandJoin.java b/src/com/bukkit/mcteam/factions/commands/FCommandJoin.java index 400bb27a..b9159cb5 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandJoin.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandJoin.java @@ -28,7 +28,7 @@ public class FCommandJoin extends FBaseCommand { return; } - if (faction.id == me.factionId) { + if (faction == me.getFaction()) { sendMessage("You are already a member of "+faction.getTag(me)); return; } @@ -48,8 +48,8 @@ public class FCommandJoin extends FBaseCommand { faction.sendMessage(me.getNameAndRelevant(faction)+Conf.colorSystem+" joined your faction."); me.resetFactionData(); - me.factionId = faction.id; - faction.invites.remove(me.playerName); + me.setFaction(faction); + faction.deinvite(me); FPlayer.save(); } diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandKick.java b/src/com/bukkit/mcteam/factions/commands/FCommandKick.java index fcd0fe72..b02f9781 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandKick.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandKick.java @@ -41,12 +41,12 @@ public class FCommandKick extends FBaseCommand { return; } - if (you.role.value >= me.role.value) { // TODO add more informative messages. + if (you.getRole().value >= me.getRole().value) { // TODO add more informative messages. sendMessage("Your rank is too low to kick this player."); return; } - myFaction.invites.remove(you.playerName); + myFaction.deinvite(you); you.resetFactionData(); FPlayer.save(); Faction.save(); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java b/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java index 9ab97344..44a1c33d 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java @@ -27,7 +27,7 @@ public class FCommandLeave extends FBaseCommand { Faction faction = me.getFaction(); - if (me.role == Role.ADMIN && faction.getFPlayers().size() > 1) { + if (me.getRole() == Role.ADMIN && faction.getFPlayers().size() > 1) { sendMessage("You must give the admin role to someone else first."); return; } @@ -41,7 +41,7 @@ public class FCommandLeave extends FBaseCommand { for (FPlayer fplayer : FPlayer.getAllOnline()) { fplayer.sendMessage("The faction "+faction.getTag(fplayer)+Conf.colorSystem+" was disbanded."); } - Faction.delete(faction.id); + Faction.delete(faction.getId()); } } diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandList.java b/src/com/bukkit/mcteam/factions/commands/FCommandList.java index 74696010..580b977e 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandList.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandList.java @@ -40,9 +40,9 @@ public class FCommandList extends FBaseCommand { Collections.sort(FactionList, new Comparator(){ @Override public int compare(Faction f1, Faction f2) { - if (f1.id == 0) + if (f1.getId() == 0) return 1; - else if (f2.id == 0) + else if (f2.getId() == 0) return -1; else if (f1.getFPlayers().size() < f2.getFPlayers().size()) return 1; @@ -78,7 +78,7 @@ public class FCommandList extends FBaseCommand { if (maxPos > FactionList.size()) maxPos = FactionList.size(); for (int pos = page * 9; pos < maxPos; pos++) { Faction faction = FactionList.get(pos); - if (faction.id == 0) { + if (faction.getId() == 0) { sendMessage(faction.getTag(me)+Conf.colorSystem+" "+faction.getFPlayersWhereOnline(true).size() + " online"); } else { sendMessage(faction.getTag(me)+Conf.colorSystem+" "+faction.getFPlayersWhereOnline(true).size()+"/"+faction.getFPlayers().size()+" online, "+faction.getLandRounded()+"/"+faction.getPowerRounded()+"/"+faction.getPowerMaxRounded()); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandMod.java b/src/com/bukkit/mcteam/factions/commands/FCommandMod.java index f5faad95..681736df 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandMod.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandMod.java @@ -49,13 +49,13 @@ public class FCommandMod extends FBaseCommand { return; } - if (you.role == Role.MODERATOR) { + if (you.getRole() == Role.MODERATOR) { // Revoke - you.role = Role.NORMAL; + you.setRole(Role.NORMAL); myFaction.sendMessage(you.getNameAndRelevant(myFaction)+Conf.colorSystem+" is no longer moderator in your faction."); } else { // Give - you.role = Role.MODERATOR; + you.setRole(Role.MODERATOR); myFaction.sendMessage(you.getNameAndRelevant(myFaction)+Conf.colorSystem+" was promoted to moderator in your faction."); } } diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandOpen.java b/src/com/bukkit/mcteam/factions/commands/FCommandOpen.java index eabe29b0..3fe44555 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandOpen.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandOpen.java @@ -36,7 +36,7 @@ public class FCommandOpen extends FBaseCommand { // Inform myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" changed the faction to "+open); for (Faction faction : Faction.getAll()) { - if (faction.id == me.factionId) { + if (faction == me.getFaction()) { continue; } faction.sendMessage(Conf.colorSystem+"The faction "+myFaction.getTag(faction)+Conf.colorSystem+" is now "+open); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandShow.java b/src/com/bukkit/mcteam/factions/commands/FCommandShow.java index 17de6687..2e9e3656 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandShow.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandShow.java @@ -38,7 +38,7 @@ public class FCommandShow extends FBaseCommand { sendMessage(TextUtil.titleize(faction.getTag(me))); sendMessage(Conf.colorChrome+"Description: "+Conf.colorSystem+faction.getDescription()); - if (faction.id == 0) { + if (faction.getId() == 0) { return; } diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandTag.java b/src/com/bukkit/mcteam/factions/commands/FCommandTag.java index 10586d4b..03c5af5e 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandTag.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandTag.java @@ -53,7 +53,7 @@ public class FCommandTag extends FBaseCommand { // Inform myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" changed your faction tag to "+Conf.colorMember+myFaction.getTag()); for (Faction faction : Faction.getAll()) { - if (faction.id == me.factionId) { + if (faction == me.getFaction()) { continue; } faction.sendMessage(Conf.colorSystem+"The faction "+me.getRelationColor(faction)+oldtag+Conf.colorSystem+" chainged their name to "+myFaction.getTag(faction)); diff --git a/src/com/bukkit/mcteam/factions/commands/FRelationCommand.java b/src/com/bukkit/mcteam/factions/commands/FRelationCommand.java index 1fa63142..c750105e 100644 --- a/src/com/bukkit/mcteam/factions/commands/FRelationCommand.java +++ b/src/com/bukkit/mcteam/factions/commands/FRelationCommand.java @@ -36,7 +36,7 @@ public class FRelationCommand extends FBaseCommand { return; } - if (otherFaction.id == 0) { + if (otherFaction.getId() == 0) { sendMessage("Nope! You can't :) The default faction is not a real faction."); return; } diff --git a/src/com/bukkit/mcteam/factions/entities/CommandsOld.java b/src/com/bukkit/mcteam/factions/entities/CommandsOld.java deleted file mode 100644 index 15c20a2f..00000000 --- a/src/com/bukkit/mcteam/factions/entities/CommandsOld.java +++ /dev/null @@ -1,845 +0,0 @@ -package com.bukkit.mcteam.factions.entities; - -import java.util.*; - -import org.bukkit.ChatColor; - -import com.bukkit.mcteam.factions.entities.*; -import com.bukkit.mcteam.factions.struct.*; -import com.bukkit.mcteam.factions.util.*; - -public class CommandsOld { - public static ArrayList> helpPages; - - //----------------------------------------------// - // Build the help pages - //----------------------------------------------// - - static { - helpPages = new ArrayList>(); - ArrayList pageLines; - - pageLines = new ArrayList(); - pageLines.add(TextUtil.commandHelp(Conf.aliasHelp, "*[page]", "Display a help page")); - pageLines.add(TextUtil.commandHelp(Conf.aliasList, "*[page]", "List all factions, paginated")); - pageLines.add(TextUtil.commandHelp(Conf.aliasShow, "*[faction name]", "Show faction information")); // TODO display relations! - pageLines.add(TextUtil.commandHelp(Conf.aliasMap, "*[on|off]", "Show territory map, set optional auto update")); - pageLines.add(TextUtil.commandHelp(Conf.aliasJoin, "[faction name]", "Join a faction")); - pageLines.add(TextUtil.commandHelp(Conf.aliasLeave, "", "Leave your faction")); - pageLines.add(TextUtil.commandHelp(Conf.aliasChat, "", "Switch faction only chat on and off")); - pageLines.add(TextUtil.commandHelp(Conf.aliasCreate, "[faction tag]", "Create new faction")); - pageLines.add(TextUtil.commandHelp(Conf.aliasTag, "[faction tag]", "Change the faction tag")); - pageLines.add(TextUtil.commandHelp(Conf.aliasDescription, "[description]", "Change the faction description")); - - helpPages.add(pageLines); - pageLines = new ArrayList(); - pageLines.add(TextUtil.commandHelp(Conf.aliasOpen, "", "Switch if invitation is required to join")); - pageLines.add(TextUtil.commandHelp(Conf.aliasTitle, "[player name] *[title]", "Set or remove a players title")); - pageLines.add(TextUtil.commandHelp(Conf.aliasInvite, "[player name]", "Invite player")); - pageLines.add(TextUtil.commandHelp(Conf.aliasDeinvite, "[player name]", "Remove a pending invitation")); - pageLines.add(TextUtil.commandHelp(Conf.aliasClaim, "", "Claim the land where you are standing")); - pageLines.add(TextUtil.commandHelp(Conf.aliasUnclaim, "", "Unclaim the land where you are standing")); - pageLines.add(TextUtil.commandHelp(Conf.aliasKick, "[player name]", "Kick a player from the faction")); - pageLines.add(TextUtil.commandHelp(Conf.aliasModerator, "[player name]", "Give or revoke moderator rights")); - pageLines.add(TextUtil.commandHelp(Conf.aliasAdmin, "[player name]", "Hand over your admin rights")); - - helpPages.add(pageLines); - pageLines = new ArrayList(); - - pageLines.add(TextUtil.commandHelp(Conf.aliasRelationAlly, "[faction name]", " ")); - pageLines.add(TextUtil.commandHelp(Conf.aliasRelationNeutral, "[faction name]", " ")); - pageLines.add(TextUtil.commandHelp(Conf.aliasRelationEnemy, "[faction name]", " ")); - pageLines.add(""); - pageLines.add(Conf.colorSystem+"Set the relation you WISH to have with another faction."); - pageLines.add(Conf.colorSystem+"Your default relation with other factions will be neutral."); - pageLines.add(""); - pageLines.add(Conf.colorSystem+"If BOTH factions choose \"ally\" you will be allies."); - pageLines.add(Conf.colorSystem+"If ONE faction chooses \"enemy\" you will be enemies."); - - helpPages.add(pageLines); - pageLines = new ArrayList(); - - pageLines.add(Conf.colorSystem+"You can never hurt members or allies."); - pageLines.add(Conf.colorSystem+"You can not hurt neutrals in their own territory."); - pageLines.add(Conf.colorSystem+"You can always hurt enemies and players without faction."); - pageLines.add(""); - pageLines.add(Conf.colorSystem+"Damage from enemies is reduced in your own territory."); - pageLines.add(Conf.colorSystem+"When you die you lose power. It is restored over time."); - pageLines.add(Conf.colorSystem+"The power of a faction is the sum of all member power."); - pageLines.add(Conf.colorSystem+"The power of a faction determines how much land it can hold."); - pageLines.add(Conf.colorSystem+"You can claim land from factions with too little power."); - - helpPages.add(pageLines); - pageLines = new ArrayList(); - - pageLines.add(Conf.colorSystem+"Only faction members can build and destroy in their own"); - pageLines.add(Conf.colorSystem+"territory. Usage of the following items is also restricted:"); - pageLines.add(Conf.colorSystem+"Door, Chest, Furnace and Dispenser."); - pageLines.add(" "); - pageLines.add(Conf.colorSystem+"Make sure to put pressure plates in front of doors for your"); - pageLines.add(Conf.colorSystem+"guest visitors. Otherwise they can't get through. You can "); - pageLines.add(Conf.colorSystem+"also use this to create member only areas."); - pageLines.add(Conf.colorSystem+"As dispensers are protected, you can create traps without"); - pageLines.add(Conf.colorSystem+"worrying about those arrows getting stolen."); - - helpPages.add(pageLines); - pageLines = new ArrayList(); - - pageLines.add(TextUtil.commandHelp(Conf.aliasVersion, "", "Which version are you using?")); - - helpPages.add(pageLines); - } - - - //----------------------------------------------// - // Some utils - //----------------------------------------------// - - // Update to work with tag and follower names - - public static FPlayer findFollower(FPlayer me, String name, boolean defaultsToMe) { - if (name.length() == 0 && defaultsToMe) { - return me; - } - - FPlayer follower = FPlayer.find(name); - if (follower != null) { - return follower; - } - - me.sendMessage(Conf.colorSystem+"The player \""+name+"\" could not be found"); - return null; - } - - public static Faction findFaction(FPlayer me, String name, boolean defaultsToMe) { - if (name.length() == 0 && defaultsToMe) { - return me.getFaction(); - } - - // Search player names - FPlayer follower = FPlayer.find(name); - if (follower != null) { - return follower.getFaction(); - } - - // Then faction names - Faction faction = Faction.findByTag(name); - if (faction != null) { - return faction; - } - - me.sendMessage(Conf.colorSystem+"No faction or player \""+name+"\" was found"); - return null; - } - - 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; - } - - if (i.role.value > you.role.value || i.role.equals(Role.ADMIN) ) { - return true; - } - - if (you.role.equals(Role.ADMIN)) { - i.sendMessage(Conf.colorSystem+"Only the faction admin can do that."); - } else if (i.role.equals(Role.MODERATOR)) { - i.sendMessage(Conf.colorSystem+"Moderators can't control each other..."); - } else { - i.sendMessage(Conf.colorSystem+"You must be a faction moderator to do that."); - } - - return false; - } - - //----------------------------------------------// - // The base command - //----------------------------------------------// - - public static void base(FPlayer me, ArrayList tokens) { - if (tokens.size() == 0) { - help(me); - return; - } - - String command = tokens.get(0).toLowerCase(); - tokens.remove(0); - - if (Conf.aliasHelp.contains(command)) { - int page = 1; - if (tokens.size() > 0) { - try { - page = Integer.parseInt(tokens.get(0)); - } - catch (NumberFormatException e) { - // wasn't an integer - } - } - help(me, page); - } else if (Conf.aliasLeave.contains(command)) { - leave(me); - } else if (Conf.aliasJoin.contains(command)) { - join(me, TextUtil.implode(tokens)); - } else if (Conf.aliasCreate.contains(command)) { - create(me, TextUtil.implode(tokens)); - } else if (Conf.aliasTag.contains(command)) { - tag(me, TextUtil.implode(tokens)); - } else if (Conf.aliasDescription.contains(command)) { - description(me, TextUtil.implode(tokens)); - } else if (Conf.aliasChat.contains(command)) { - chat(me, TextUtil.implode(tokens)); - } else if (Conf.aliasList.contains(command)) { - list(me, TextUtil.implode(tokens)); - } else if (Conf.aliasShow.contains(command)) { - showFaction(me, TextUtil.implode(tokens)); - } else if (Conf.aliasMap.contains(command)) { - showMap(me, TextUtil.implode(tokens)); - } else if (Conf.aliasInvite.contains(command)) { - invite(me, TextUtil.implode(tokens)); - } else if (Conf.aliasDeinvite.contains(command)) { - deinvite(me, TextUtil.implode(tokens)); - } else if (Conf.aliasOpen.contains(command)) { - open(me); - } else if (Conf.aliasTitle.contains(command)) { - title(me, tokens); - } else if (Conf.aliasKick.contains(command)) { - kick(me, TextUtil.implode(tokens)); - } else if (Conf.aliasModerator.contains(command)) { - roleChange(me, Role.MODERATOR, TextUtil.implode(tokens)); - } else if (Conf.aliasAdmin.contains(command)) { - roleChange(me, Role.ADMIN, TextUtil.implode(tokens)); - } else if (Conf.aliasClaim.contains(command)) { - claim(me); - } else if (Conf.aliasUnclaim.contains(command)) { - unclaim(me); - } else if (Conf.aliasRelationAlly.contains(command)) { - relation(me, Relation.ALLY, TextUtil.implode(tokens)); - } else if (Conf.aliasRelationNeutral.contains(command)) { - relation(me, Relation.NEUTRAL, TextUtil.implode(tokens)); - } else if (Conf.aliasRelationEnemy.contains(command)) { - relation(me, Relation.ENEMY, TextUtil.implode(tokens)); - } else if (Conf.aliasVersion.contains(command)) { - version(me); - } else { - me.sendMessage(Conf.colorSystem+"Unknown faction command "+Conf.colorCommand+command); - } - } - - //----------------------------------------------// - // The other commands - //----------------------------------------------// - public static void help(FPlayer me) { - help(me, 1); - } - - 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()) { - me.sendMessage(Conf.colorSystem+"That page does not exist"); - return; - } - me.sendMessage(helpPages.get(page), false); - } - /* - public static void leave(FPlayer me) { - Faction faction = me.getFaction(); - - ArrayList errors = me.leave(); - me.sendMessage(errors); - - if (errors.size() == 0) { - faction.sendMessage(me.getNameAndRelevant(faction)+Conf.colorSystem+" left your faction."); - me.sendMessage("You left "+faction.getTag(me)); - } - - if (faction.getFPlayers().size() == 0) { - // Remove this faction - 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(FPlayer me, String name) { - Faction faction = findFaction(me, name, false); - if (faction == null) { - return; - } - - ArrayList errors = me.join(faction); - me.sendMessage(errors); - - if (errors.size() > 0) { - faction.sendMessage(me.getNameAndRelevant(faction)+Conf.colorSystem+" tried to join your faction."); - } else { - me.sendMessage(Conf.colorSystem+"You successfully joined "+faction.getTag(me)); - faction.sendMessage(me.getNameAndRelevant(faction)+Conf.colorSystem+" joined your faction."); - } - } - - public static void create(FPlayer me, String tag) { - ArrayList errors = new ArrayList(); - - if (me.hasFaction()) { - errors.add(Conf.colorSystem+"You must leave your current faction first."); - } - - if (Faction.isTagTaken(tag)) { - errors.add(Conf.colorSystem+"That tag is already in use."); - } - - errors.addAll(Faction.validateTag(tag)); - - if (errors.size() > 0) { - me.sendMessage(errors); - return; - } - - Faction faction = EM.factionCreate(); - faction.setTag(tag); - faction.save(); - me.join(faction); - me.role = Role.ADMIN; - me.save(); - - for (FPlayer follower : FPlayer.getAll()) { - follower.sendMessage(me.getNameAndRelevant(follower)+Conf.colorSystem+" created a new faction "+faction.getTag(follower)); - } - - me.sendMessage(Conf.colorSystem+"Now update your faction description. Use:"); - me.sendMessage(Conf.colorCommand+Conf.aliasBase.get(0)+" "+Conf.aliasDescription.get(0)+" "+"[description]"); - } - - - public static void tag(FPlayer me, String tag) { - ArrayList errors = new ArrayList(); - - if (me.withoutFaction()) { - errors.add(Conf.colorSystem+"You are not part of any faction"); - } else if (me.role.value < Role.MODERATOR.value) { - errors.add(Conf.colorSystem+"You must be moderator to rename your faction"); - } - - if (Faction.isTagTaken(tag) && ! TextUtil.getComparisonString(tag).equals(me.getFaction().getComparisonTag())) { - errors.add(Conf.colorSystem+"That tag is already taken"); - } - - errors.addAll(Faction.validateTag(tag)); - - if (errors.size() > 0) { - me.sendMessage(errors); - return; - } - - Faction myFaction = me.getFaction(); - - String oldtag = myFaction.getTag(); - myFaction.setTag(tag); - - // Inform - myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" changed your faction tag to "+Conf.colorMember+myFaction.getTag()); - for (Faction faction : Faction.getAll()) { - if (faction.id == me.factionId) { - continue; - } - faction.sendMessage(Conf.colorSystem+"The faction "+me.getRelationColor(faction)+oldtag+Conf.colorSystem+" chainged their name to "+me.getRelationColor(faction)+myFaction.getTag()); - } - } - - public static void list(FPlayer me, String inPage) { - ArrayList FactionList = new ArrayList(Faction.getAll()); - - int page = 1; - try { - page = Integer.parseInt(inPage); - } catch (NumberFormatException e) { - // wasn't an integer - } - page -= 1; - - // Sort by total followers first - Collections.sort(FactionList, new Comparator(){ - @Override - public int compare(Faction f1, Faction f2) { - if (f1.id == 0) - return 1; - else if (f2.id == 0) - return -1; - else if (f1.getFPlayers().size() < f2.getFPlayers().size()) - return 1; - else if (f1.getFPlayers().size() > f2.getFPlayers().size()) - return -1; - return 0; - } - }); - - // Then sort by how many members are online now - Collections.sort(FactionList, new Comparator(){ - @Override - public int compare(Faction f1, Faction f2) { - if (f1.getFPlayersWhereOnline(true).size() < f2.getFPlayersWhereOnline(true).size()) - return 1; - else if (f1.getFPlayersWhereOnline(true).size() > f2.getFPlayersWhereOnline(true).size()) - return -1; - return 0; - } - }); - - int maxPage = (int)Math.floor((double)FactionList.size() / 9D); - if (page < 0 || page > maxPage) { - me.sendMessage(Conf.colorSystem+"The faction list is only " + (maxPage+1) + " page(s) long"); - return; - } - - String header = "Faction List"; - if (maxPage > 1) header += " (page " + (page+1) + " of " + (maxPage+1) + ")"; - me.sendMessage(TextUtil.titleize(header), false); - - int maxPos = (page+1) * 9; - if (maxPos > FactionList.size()) maxPos = FactionList.size(); - for (int pos = page * 9; pos < maxPos; pos++) { - Faction faction = FactionList.get(pos); - if (faction.id == 0) { - me.sendMessage(faction.getTag(me)+Conf.colorSystem+" "+faction.getFPlayersWhereOnline(true).size() + " online"); - } else { - me.sendMessage(faction.getTag(me)+Conf.colorSystem+" "+faction.getFPlayersWhereOnline(true).size()+"/"+faction.getFPlayers().size()+" online, "+faction.getLandRounded()+"/"+faction.getPowerRounded()+"/"+faction.getPowerMaxRounded()); - } - } - } - - public static void showFaction(FPlayer me, String name) { - Faction faction = findFaction(me, name, true); - if (faction == null) { - return; - } - Collection admins = faction.getFPlayersWhereRole(Role.ADMIN); - Collection mods = faction.getFPlayersWhereRole(Role.MODERATOR); - Collection normals = faction.getFPlayersWhereRole(Role.NORMAL); - - me.sendMessage(TextUtil.titleize(faction.getTag(me)), false); - me.sendMessage(Conf.colorChrome+"Description: "+Conf.colorSystem+faction.getDescription()); - if (faction.id == 0) { - return; - } - - if(faction.getOpen()) { - me.sendMessage(Conf.colorChrome+"Joining: "+Conf.colorSystem+"no invitation is needed"); - } else { - me.sendMessage(Conf.colorChrome+"Joining: "+Conf.colorSystem+"invitation is required"); - } - me.sendMessage(Conf.colorChrome+"Land / Power / Maxpower: "+Conf.colorSystem+ faction.getLandRounded()+" / "+faction.getPowerRounded()+" / "+faction.getPowerMaxRounded()); - - String listpart; - - // List relation - String allyList = Conf.colorChrome+"Allies: "; - String enemyList = Conf.colorChrome+"Enemies: "; - for (Faction otherFaction : Faction.getAll()) { - if (otherFaction == faction) { - continue; - } - listpart = otherFaction.getTag(me)+Conf.colorSystem+", "; - if (otherFaction.getRelation(faction) == Relation.ALLY) { - allyList += listpart; - } else if (otherFaction.getRelation(faction) == Relation.ENEMY) { - enemyList += listpart; - } - } - if (allyList.endsWith(", ")) { - allyList = allyList.substring(0, allyList.length()-2); - } - if (enemyList.endsWith(", ")) { - enemyList = enemyList.substring(0, enemyList.length()-2); - } - - me.sendMessage(allyList); - me.sendMessage(enemyList); - - // List the members... - String onlineList = Conf.colorChrome+"Members online: "; - String offlineList = Conf.colorChrome+"Members offline: "; - for (FPlayer follower : admins) { - listpart = follower.getNameAndTitle(me)+Conf.colorSystem+", "; - if (follower.isOnline()) { - onlineList += listpart; - } else { - offlineList += listpart; - } - } - for (FPlayer follower : mods) { - listpart = follower.getNameAndTitle(me)+Conf.colorSystem+", "; - if (follower.isOnline()) { - onlineList += listpart; - } else { - offlineList += listpart; - } - } - for (FPlayer follower : normals) { - listpart = follower.getNameAndTitle(me)+Conf.colorSystem+", "; - if (follower.isOnline()) { - onlineList += listpart; - } else { - offlineList += listpart; - } - } - - if (onlineList.endsWith(", ")) { - onlineList = onlineList.substring(0, onlineList.length()-2); - } - if (offlineList.endsWith(", ")) { - offlineList = offlineList.substring(0, offlineList.length()-2); - } - - me.sendMessage(onlineList); - me.sendMessage(offlineList); - } - - - 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())) { - // Turn on - me.setMapAutoUpdating(true); - me.sendMessage(Conf.colorSystem + "Map auto update ENABLED."); - - // And show the map once - showMap(me,""); - } else { - // Turn off - me.setMapAutoUpdating(false); - me.sendMessage(Conf.colorSystem + "Map auto update DISABLED."); - } - } else { - me.sendMessage(board.getMap(me.getFaction(), Coord.from(me), me.getPlayer().getLocation().getYaw()), false); - } - } - - public static void invite(FPlayer me, String name) { - FPlayer follower = findFollower(me, name, false); - if (follower == null) { - return; - } - - ArrayList errors = me.invite(follower); - me.sendMessage(errors); - - if (errors.size() == 0) { - follower.sendMessage(me.getNameAndRelevant(follower)+Conf.colorSystem+" invited you to "+me.getFaction().getTag(follower)); - me.getFaction().sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" invited "+follower.getNameAndRelevant(me)+Conf.colorSystem+" to your faction."); - } - } - - public static void deinvite(FPlayer me, String name) { // TODO Move out! - FPlayer follower = findFollower(me, name, false); - if (follower == null) { - return; - } - - ArrayList errors = me.deinvite(follower); - me.sendMessage(errors); - - if (errors.size() == 0) { - follower.sendMessage(me.getNameAndRelevant(follower)+Conf.colorSystem+" revoked your invitation to "+me.getFaction().getTag(follower)); - me.getFaction().sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" revoked "+follower.getNameAndRelevant(me)+"'s"+Conf.colorSystem+" invitation."); - } - } - - 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; - } - Faction myFaction = me.getFaction(); - myFaction.setOpen( ! me.getFaction().getOpen()); - - String open = myFaction.getOpen() ? "open" : "closed"; - - // Inform - myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" changed the faction to "+open); - for (Faction faction : Faction.getAll()) { - if (faction.id == me.factionId) { - continue; - } - faction.sendMessage(Conf.colorSystem+"The faction "+myFaction.getTag(faction)+Conf.colorSystem+" is now "+open); - } - } - - public static void title(FPlayer me, ArrayList tokens) { - if (tokens.size() == 0) { - me.sendMessage(Conf.colorSystem+"You must specify a player name"); - return; - } - - String name = tokens.get(0); - tokens.remove(0); - - FPlayer you = findFollower(me, name, true); - if (you == null) { - return; - } - - if ( ! canIAdministerYou(me, you)) { - return; - } - - // All ok! Set the title! - String title = TextUtil.implode(tokens); - you.setTitle(title); - - // Inform - Faction myFaction = me.getFaction(); - myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" changed a title: "+you.getNameAndRelevant(myFaction)); - } - - public static void kick(FPlayer me, String name) { - if (name.length() == 0) { - me.sendMessage(Conf.colorSystem+"You must specify a player name."); - return; - } - - FPlayer you = findFollower(me, name, false); - if (you == null) { - return; - } - - ArrayList errors = me.kick(you); - me.sendMessage(errors); - - if (errors.size() == 0) { - Faction myFaction = me.getFaction(); - myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" kicked "+you.getNameAndRelevant(myFaction)+Conf.colorSystem+" from the faction! :O"); - you.sendMessage(me.getNameAndRelevant(you)+Conf.colorSystem+" kicked you from "+myFaction.getTag(you)+Conf.colorSystem+"! :O"); - } - } - - 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; - } - - if (name.length() == 0) { - me.sendMessage(Conf.colorSystem+"You must specify a player name."); - return; - } - - FPlayer targetFollower = findFollower(me, name, false); - if (targetFollower == null) { - return; - } - - if (targetFollower.factionId != me.factionId) { - me.sendMessage(targetFollower.getNameAndRelevant(me)+Conf.colorSystem+" is not a member in your faction."); - return; - } - - if (targetFollower == me) { - me.sendMessage(Conf.colorSystem+"The target player musn't be yourself."); - return; - } - - if (targetRole == Role.ADMIN) { - me.role = Role.MODERATOR; - targetFollower.role = Role.ADMIN; - - // Inform all players - 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 { - follower.sendMessage(me.getNameAndRelevant(follower)+Conf.colorSystem+" gave "+targetFollower.getNameAndRelevant(follower)+Conf.colorSystem+" the leadership of "+me.getFaction().getTag(follower)); - } - } - } else if (targetRole == Role.MODERATOR) { - if (targetFollower.role == Role.MODERATOR) { - // Revoke - targetFollower.role = Role.NORMAL; - me.getFaction().sendMessage(targetFollower.getNameAndRelevant(me.getFaction())+Conf.colorSystem+" is no longer moderator in your faction."); - } else { - // Give - targetFollower.role = Role.MODERATOR; - me.getFaction().sendMessage(targetFollower.getNameAndRelevant(me.getFaction())+Conf.colorSystem+" was promoted to moderator in your faction."); - } - } - } - - public static void claim(FPlayer me) { - if (me.withoutFaction()) { - me.sendMessage(Conf.colorSystem+"You are not part of any faction."); - return; - } - - Coord coord = Coord.from(me); - Board board = Board.get(me.getPlayer().getWorld()); - Faction otherFaction = board.getFactionAt(coord); - Faction myFaction = me.getFaction(); - - if (myFaction.equals(otherFaction)) { - me.sendMessage(Conf.colorSystem+"You already own this land."); - return; - } - - if (me.role.value < Role.MODERATOR.value) { - me.sendMessage(Conf.colorSystem+"You must be moderator to claim land."); - return; - } - - if (myFaction.getLandRounded() >= myFaction.getPowerRounded()) { - me.sendMessage(Conf.colorSystem+"You can't claim more land! You need more power!"); - return; - } - - if (otherFaction.getRelation(me) == Relation.ALLY) { - me.sendMessage(Conf.colorSystem+"You can't claim the land of your allies."); - return; - } - - if (otherFaction.id != 0) { - if ( ! otherFaction.hasLandInflation()) { // TODO more messages WARN current faction most importantly - me.sendMessage(me.getRelationColor(otherFaction)+otherFaction.getTag()+Conf.colorSystem+" owns this land and is strong enough to keep it."); - return; - } - - if ( ! board.isBorderCoord(coord)) { - me.sendMessage(Conf.colorSystem+"You must start claiming land at the border of the territory."); - return; - } - } - - if (otherFaction.id == 0) { - myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" claimed some new land :D"); - } else { - // ASDF claimed some of your land 450 blocks NNW of you. - // ASDf claimed some land from FACTION NAME - otherFaction.sendMessage(me.getNameAndRelevant(otherFaction)+Conf.colorSystem+" stole some of your land :O"); - myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" claimed some land from "+otherFaction.getTag(myFaction)); - } - - board.claim(coord, myFaction); - } - - public static void unclaim(FPlayer me) { - if (me.withoutFaction()) { - me.sendMessage(Conf.colorSystem+"You are not part of any faction"); - return; - } - - if (me.role.value < Role.MODERATOR.value) { - me.sendMessage(Conf.colorSystem+"You must be moderator to unclaim land"); - return; - } - - Coord coord = Coord.from(me.getPlayer()); - Board board = Board.get(me.getPlayer().getWorld()); - - if ( me.getFaction() != board.getFactionAt(coord)) { - me.sendMessage(Conf.colorSystem+"You don't own this land."); - return; - } - - board.unclaim(coord); - me.getFaction().sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" unclaimed some land."); - } - - 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; - } - - if (me.role.value < Role.MODERATOR.value) { - me.sendMessage(Conf.colorSystem+"You must be moderator to set relation to other factions."); - return; - } - - if (otherFactionName.length() == 0) { - me.sendMessage(Conf.colorSystem+"You must specify another faction."); - return; - } - - Faction otherFaction = findFaction(me, otherFactionName, false); - if (otherFaction == null) { - return; - } - - if (otherFaction.id == 0) { - me.sendMessage(Conf.colorSystem+"Nope! You can't :) The default faction is not a real faction."); - return; - } - - if (otherFaction.equals(me.getFaction())) { - me.sendMessage(Conf.colorSystem+"Nope! You can't declare a relation to yourself :)"); - return; - } - - Faction myFaction = me.getFaction(); - myFaction.setRelationWish(otherFaction, whishedRelation); - Relation currentRelation = myFaction.getRelation(otherFaction); - ChatColor currentRelationColor = currentRelation.getColor(); - if (whishedRelation == currentRelation) { - otherFaction.sendMessage(Conf.colorSystem+"Your faction is now "+currentRelationColor+whishedRelation.toString()+Conf.colorSystem+" to "+currentRelationColor+myFaction.getTag()); - myFaction.sendMessage(Conf.colorSystem+"Your faction is now "+currentRelationColor+whishedRelation.toString()+Conf.colorSystem+" to "+currentRelationColor+otherFaction.getTag()); - } else { - otherFaction.sendMessage(currentRelationColor+myFaction.getTag()+Conf.colorSystem+ " wishes to be your "+whishedRelation.getColor()+whishedRelation.toString()); - otherFaction.sendMessage(Conf.colorSystem+"Type "+Conf.colorCommand+Conf.aliasBase.get(0)+" "+whishedRelation+" "+myFaction.getTag()+Conf.colorSystem+" to accept."); - myFaction.sendMessage(currentRelationColor+otherFaction.getTag()+Conf.colorSystem+ " were informed that you wish to be "+whishedRelation.getColor()+whishedRelation); - } - } - - public static void description(FPlayer me, String desc) { - if (me.withoutFaction()) { - me.sendMessage(Conf.colorSystem+"You are not part of any faction"); - return; - } - - if (me.role.value < Role.MODERATOR.value) { - me.sendMessage(Conf.colorSystem+"You must be moderator to set the description"); - return; - } - - me.getFaction().setDescription(desc); - - me.sendMessage(Conf.colorSystem+"The new description was set :D"); - - // Broadcast the description to everyone - 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(FPlayer me, String msg) { - if (me.withoutFaction()) { - me.sendMessage(Conf.colorSystem+"You are not part of any faction"); - return; - } - - if ( ! me.isFactionChatting()) { - // Turn on - me.setFactionChatting(true); - me.sendMessage(Conf.colorSystem + "Faction-only chat ENABLED."); - } else { - // Turn off - me.setFactionChatting(false); - me.sendMessage(Conf.colorSystem + "Faction-only chat DISABLED."); - } - } - - 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/CoordOld.java b/src/com/bukkit/mcteam/factions/entities/CoordOld.java deleted file mode 100644 index 177c9aea..00000000 --- a/src/com/bukkit/mcteam/factions/entities/CoordOld.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.bukkit.mcteam.factions.entities; - -import org.bukkit.Location; -import org.bukkit.block.Block; -import org.bukkit.entity.Player; - - -public class CoordOld { - protected static transient int cellSize = 16; - public int x, z; - - public CoordOld(int x, int z) { - this.x = x; - this.z = z; - } - - // TODO implements cloneable - public CoordOld(Coord coord) { - this.x = coord.x; - this.z = coord.z; - } - - public CoordOld() { - // Noarg constructor for google gson. - } - - public Coord getRelative(int dx, int dz) { - return new Coord(this.x + dx, this.z + dz); - } - - public static Coord from(int x, int z) { - return new Coord(x / cellSize - (x < 0 ? 1 : 0), z / cellSize - (z < 0 ? 1 : 0)); - } - - public static Coord from(Player player) { - return from(player.getLocation()); - } - - public static Coord from(FPlayer follower) { - return from(follower.getPlayer()); - } - - public static Coord from(Location loc) { - return from(loc.getBlockX(), loc.getBlockZ()); - } - - public static Coord parseCoord(Block block) { - return from(block.getX(), block.getZ()); - } - - @Override - public String toString() { - return this.x + "," + this.z; - } - - @Override - public int hashCode() { - int result = 17; - result = 31 * result + x; - result = 31 * result + z; - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == this) - return true; - if (!(obj instanceof Coord)) - return false; - - Coord o = (Coord) obj; - return this.x == o.x && this.z == o.z; - } -} diff --git a/src/com/bukkit/mcteam/factions/entities/EMOld.java b/src/com/bukkit/mcteam/factions/entities/EMOld.java deleted file mode 100644 index 517fe742..00000000 --- a/src/com/bukkit/mcteam/factions/entities/EMOld.java +++ /dev/null @@ -1,405 +0,0 @@ -package com.bukkit.mcteam.factions.entities; - -import java.io.*; -import java.lang.reflect.Modifier; -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; -import com.bukkit.mcteam.gson.*; - -/** - * This is a entity manager that persists object to json. - * Before using the the EM you should always EM.loadAll(). - * The methods assume that all on disc is loaded into memory. - */ -public class EMOld { - 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.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"); - protected final static File fileConfig = new File(folderBase, "conf"+ext); - - 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(); - - public static void loadAll() { - folderBase.mkdirs(); - configLoad(); - Log.threshold = Conf.logThreshold; - factionLoadAll(); - followerLoadAll(); - boardLoadAll(); - Board.cleanAll(); - } - - //----------------------------------------------// - // Config methods (load, save) - //----------------------------------------------// - public static boolean configLoad() { - if (fileConfig.exists()) { - try { - gson.fromJson(DiscUtil.read(fileConfig), Conf.class); - Log.info("Config was loaded from disk"); - return true; - } catch (IOException e) { - e.printStackTrace(); - Log.warn("Failed to load the config"); - return false; - } - } - Log.info("No conf.json found! Creating a new one with the default values"); - configSave(); - return true; - } - - public static boolean configSave() { - folderBase.mkdirs(); - try { - DiscUtil.write(fileConfig, gson.toJson(new Conf())); - Log.debug("Config was saved to disc"); - return true; - } catch (IOException e) { - e.printStackTrace(); - Log.warn("Failed to save the config"); - return false; - } - } - - //----------------------------------------------// - // Board methods (load, save) - //----------------------------------------------// - /*public static boolean boardLoad() { - if (fileBoard.exists()) { - try { - gson.fromJson(DiscUtil.read(fileBoard), Board.class); - Log.info("Board was loaded from disc"); - return true; - } catch (IOException e) { - e.printStackTrace(); - Log.warn("Failed to load the board"); - return false; - } - } - Log.info("No board.json found! Creating a new one with the default values"); - boardSave(); - return true; - } - - public static boolean boardSave() { - folderBase.mkdirs(); - try { - DiscUtil.write(fileBoard, gson.toJson(new Board())); - Log.debug("Board was saved to disc"); - return true; - } catch (IOException e) { - e.printStackTrace(); - Log.warn("Failed to save the board"); - return false; - } - }*/ - - //----------------------------------------------// - // Board methods (loadAll, get, save) - //----------------------------------------------// - - /** - * This method loads all boards from disc into memory. - */ - public static void boardLoadAll() { - Log.info("Loading all boards from disk..."); - folderBoard.mkdirs(); - - class jsonFileFilter implements FileFilter { - @Override - public boolean accept(File file) { - return (file.getName().toLowerCase().endsWith(ext) && file.isFile()); - } - } - - File[] jsonFiles = folderBoard.listFiles(new jsonFileFilter()); - - for (File jsonFile : jsonFiles) { - // Extract the name from the filename. The name is filename minus ".json" - String name = jsonFile.getName(); - name = name.substring(0, name.length() - ext.length()); - try { - Board board = gson.fromJson(DiscUtil.read(jsonFile), Board.class); - board.worldName = name; - boards.put(board.worldName, board); - Log.debug("loaded board "+name); - } catch (Exception e) { - e.printStackTrace(); - Log.warn("failed to load board "+name); - } - } - } - - public static Collection boardGetAll() { - return boards.values(); - } - - /** - * This method returns the board object for a world - * A new Board will be created if the world did not have one - */ - public static Board boardGet(World world) { - if (boards.containsKey(world.getName())) { - return boards.get(world.getName()); - } - - return boardCreate(world); - } - - public static boolean boardSave(String worldName) { - Object obj = boards.get(worldName); - if (obj == null) { - Log.warn("Could not save board "+worldName+" as it was not loaded"); - return false; - } - folderBoard.mkdirs(); - File file = new File(folderBoard, worldName+ext); - try { - DiscUtil.write(file, gson.toJson(obj)); - Log.debug("Saved the board "+worldName); - return true; - } catch (IOException e) { - e.printStackTrace(); - Log.warn("Failed to save the board "+worldName); - return false; - } - } - - protected static Board boardCreate(World world) { - Log.debug("Creating new board for "+world.getName()); - Board board = new Board(); - board.worldName = world.getName(); - boards.put(board.worldName, board); - board.save(); - return board; - } - - //----------------------------------------------// - // Follower methods (loadAll, get, save) - //----------------------------------------------// - - /** - * This method loads all followers from disc into memory. - */ - public static void followerLoadAll() { - Log.info("Loading all followers from disk..."); - folderFollower.mkdirs(); - - class jsonFileFilter implements FileFilter { - @Override - public boolean accept(File file) { - return (file.getName().toLowerCase().endsWith(ext) && file.isFile()); - } - } - - File[] jsonFiles = folderFollower.listFiles(new jsonFileFilter()); - - for (File jsonFile : jsonFiles) { - // Extract the name from the filename. The name is filename minus ".json" - String name = jsonFile.getName(); - name = name.substring(0, name.length() - ext.length()); - try { - FPlayer follower = gson.fromJson(DiscUtil.read(jsonFile), FPlayer.class); - follower.id = name; - followers.put(follower.id, follower); - //Log.debug("loaded follower "+name); - } catch (Exception e) { - e.printStackTrace(); - Log.warn("failed to load follower "+name); - } - } - } - - public static Collection followerGetAll() { - return followers.values(); - } - - /** - * This method returns the follower object for a player - * A new Follower will be created if the player did not have one - */ - public static FPlayer followerGet(Player player) { - String key = followerKey(player); - - if (followers.containsKey(key)) { - return followers.get(key); - } - - return followerCreate(player); - } - - public static boolean followerSave(String id) { - Object obj = followers.get(id); - if (obj == null) { - Log.warn("Could not save follower "+id+" as it was not loaded"); - return false; - } - folderFollower.mkdirs(); - File file = new File(folderFollower, id+ext); - try { - DiscUtil.write(file, gson.toJson(obj)); - Log.debug("Saved the follower "+id); - return true; - } catch (IOException e) { - e.printStackTrace(); - Log.warn("Failed to save the follower "+id); - return false; - } - - - } - - protected static String followerKey(Player player) { - return player.getName(); - } - - protected static FPlayer followerCreate(Player player) { - Log.debug("Creating new follower "+followerKey(player)); - FPlayer follower = new FPlayer(); - follower.id = followerKey(player); - followers.put(follower.id, follower); - follower.save(); - return follower; - } - - //----------------------------------------------// - // Faction methods (loadAll, get, create, delete, save) - //----------------------------------------------// - - /** - * This method loads all followers from disc into memory. - */ - public static void factionLoadAll() { - Log.info("Loading all factions from disk..."); - folderFaction.mkdirs(); - - class jsonFileFilter implements FileFilter - { - @Override - public boolean accept(File file) { - return (file.getName().toLowerCase().endsWith(ext) && file.isFile()); - } - } - - nextFactionId = 0; - File[] jsonFiles = folderFaction.listFiles(new jsonFileFilter()); - for (File jsonFile : jsonFiles) { - // Extract the name from the filename. The name is filename minus ".json" - String name = jsonFile.getName(); - name = name.substring(0, name.length() - ext.length()); - int id = Integer.parseInt(name); - - // Eventually push next id forward - if (nextFactionId < id) { - nextFactionId = id; - } - - try { - Faction faction = gson.fromJson(DiscUtil.read(jsonFile), Faction.class); - faction.id = id; - factions.put(faction.id, faction); - //Log.debug("loaded faction "+id); - } catch (Exception e) { - e.printStackTrace(); - Log.warn("Failed to load faction "+id); - } - } - - nextFactionId += 1; // make it the next id and not the current highest. - - // Make sure the default neutral faction exists - if ( ! factions.containsKey(0)) { - Faction faction = new Faction(); - faction.tag = "*No faction*"; - faction.description = "\"The faction for the factionless :P\""; - faction.id = 0; - factions.put(faction.id, faction); - } - } - - public static Faction factionGet(Integer factionId) { - if ( ! factions.containsKey(factionId)) { - Log.warn("Non existing factionId "+factionId+" requested from EM! Issuing board cleaning!"); - Board.cleanAll(); - } - return factions.get(factionId); - } - - public static boolean factionExists(Integer factionId) { - return factions.containsKey(factionId); - } - - public static Collection factionGetAll() { - return factions.values(); - } - - public static Faction factionCreate() { - Faction faction = new Faction(); - faction.id = nextFactionId; - nextFactionId += 1; - factions.put(faction.id, faction); - Log.debug("created new faction "+faction.id); - faction.save(); - return faction; - } - - public static boolean factionDelete(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 - factions.remove(id); - - return true; // TODO - } - - public static boolean factionSave(Integer id) { - Object obj = factions.get(id); - if (obj == null) { - Log.warn("Could not save faction "+id+" as it was not loaded"); - return false; - } - folderFaction.mkdirs(); - File file = new File(folderFaction, id+ext); - try { - DiscUtil.write(file, gson.toJson(obj)); - Log.debug("saved the faction "+id); - return true; - } catch (IOException e) { - e.printStackTrace(); - Log.warn("failed to save the faction "+id); - return false; - } - } -} diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java index 0c436fd0..82138375 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java @@ -1,6 +1,9 @@ package com.bukkit.mcteam.factions.listeners; -import java.util.*; +import java.util.Arrays; +import java.util.Set; +import java.util.HashSet; + import org.bukkit.Material; import org.bukkit.block.Block; @@ -13,27 +16,19 @@ 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.FLocation; 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.*; +import com.bukkit.mcteam.factions.util.TextUtil; public class FactionsBlockListener extends BlockListener { - public Factions plugin; - public FactionsBlockListener(Factions plugin) { - this.plugin = plugin; - } @Override public void onBlockPlace(BlockPlaceEvent event) { - // debug - //event.getPlayer().sendMessage("Block placed: " + event.getBlockPlaced().getTypeId() + " Block clicked: " + event.getBlock().getTypeId() + "(" + event.getBlock().getType().toString() + ")"); - if (event.isCancelled()) { - return; // Alright. lets listen to that. + return; } + if ( ! this.playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "build")) { event.setCancelled(true); } @@ -60,10 +55,9 @@ public class FactionsBlockListener extends BlockListener { } public boolean playerCanBuildDestroyBlock(Player player, Block block, String action) { - Coord coord = Coord.parseCoord(block); - Faction otherFaction = Board.get(player.getWorld()).getFactionAt(coord); + Faction otherFaction = Board.getFactionAt(new FLocation(block)); - if (otherFaction == null || otherFaction.id == 0) { + if (otherFaction == null || otherFaction.getId() == 0) { return true; // This is no faction territory. You may build or break stuff here. } @@ -72,8 +66,7 @@ public class FactionsBlockListener extends BlockListener { // Cancel if we are not in our own territory if (myFaction != otherFaction) { - me.sendMessage(Conf.colorSystem+"You can't "+action+" in the territory of "+otherFaction.getTag(myFaction)); - //otherFaction.sendMessage(me.getNameAndRelevant(otherFaction)+Conf.colorSystem+" tried to "+action+" "+TextUtil.getMaterialName(block.getType())+" in your territory"); + me.sendMessage("You can't "+action+" in the territory of "+otherFaction.getTag(myFaction)); return false; } @@ -83,7 +76,7 @@ public class FactionsBlockListener extends BlockListener { @Override public void onBlockInteract(BlockInteractEvent event) { if (event.isCancelled()) { - return; // Alright. lets listen to that. + return; } if ( ! (event.getEntity() instanceof Player)) { @@ -93,9 +86,6 @@ public class FactionsBlockListener extends BlockListener { Block block = event.getBlock(); Player player = (Player) event.getEntity(); - - // debug - //player.sendMessage("Block interacted: " + event.getBlock().getTypeId() + "(" + event.getBlock().getType().toString() + ")"); if ( ! canPlayerUseRightclickBlock(player, block)) { event.setCancelled(true); @@ -112,10 +102,9 @@ public class FactionsBlockListener extends BlockListener { FPlayer me = FPlayer.get(player); Faction myFaction = me.getFaction(); - Coord blockCoord = Coord.from(block.getLocation()); - Faction otherFaction = Board.get(player.getWorld()).getFactionAt(blockCoord); + Faction otherFaction = Board.getFactionAt(new FLocation(block)); - if (otherFaction != null && otherFaction.id != 0 && myFaction != otherFaction) { + if (otherFaction != null && otherFaction.getId() != 0 && myFaction != otherFaction) { me.sendMessage(Conf.colorSystem+"You can't use "+TextUtil.getMaterialName(material)+" in the territory of "+otherFaction.getTag(myFaction)); //otherFaction.sendMessage(me.getNameAndRelevant(otherFaction)+Conf.colorSystem+" tried to use "+TextUtil.getMaterialName(material)+" in your territory"); return false; diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsEntityListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsEntityListener.java index 1b366940..adee1031 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsEntityListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsEntityListener.java @@ -3,9 +3,9 @@ package com.bukkit.mcteam.factions.listeners; import java.text.MessageFormat; import org.bukkit.ChatColor; +import org.bukkit.entity.Creeper; import org.bukkit.entity.Entity; import org.bukkit.entity.Fireball; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByProjectileEvent; @@ -16,16 +16,11 @@ 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.FLocation; import com.bukkit.mcteam.factions.FPlayer; -import com.bukkit.mcteam.factions.Factions; import com.bukkit.mcteam.factions.struct.Relation; public class FactionsEntityListener extends EntityListener { - public Factions plugin; - public FactionsEntityListener(Factions plugin) { - this.plugin = plugin; - } @Override public void onEntityDeath(EntityDeathEvent event) { @@ -37,7 +32,7 @@ public class FactionsEntityListener extends EntityListener { Player player = (Player) entity; FPlayer follower = FPlayer.get(player); follower.onDeath(); - follower.sendMessage(Conf.colorSystem+"Your power is now "+follower.getPowerRounded()+" / "+follower.getPowerMaxRounded()); + follower.sendMessage("Your power is now "+follower.getPowerRounded()+" / "+follower.getPowerMaxRounded()); } /** @@ -49,7 +44,7 @@ public class FactionsEntityListener extends EntityListener { @Override public void onEntityDamage(EntityDamageEvent event) { if ( event.isCancelled()) { - return; // Some other plugin decided. Alright then. + return; } if (event instanceof EntityDamageByEntityEvent) { @@ -65,24 +60,26 @@ public class FactionsEntityListener extends EntityListener { } } + + // TODO what happens with the creeper or fireball then? @Override public void onEntityExplode(EntityExplodeEvent event) { - if (Conf.territoryBlockCreepers && event.getEntity() instanceof LivingEntity) - { // creeper which might need prevention, if inside faction territory - if (Board.get(event.getLocation().getWorld()).getFactionIdAt(Coord.from(event.getLocation())) > 0) - { - event.setCancelled(true); - return; - } + if ( event.isCancelled()) { + return; } - else if (Conf.territoryBlockFireballs && event.getEntity() instanceof Fireball) - { // ghast fireball which might need prevention, if inside faction territory - if (Board.get(event.getLocation().getWorld()).getFactionIdAt(Coord.from(event.getLocation())) > 0) - { - event.setCancelled(true); - return; - } + + // Explosions may happen in the wilderness + if (Board.getIdAt(new FLocation(event.getLocation())) == 0) { + return; + } + + if (Conf.territoryBlockCreepers && event.getEntity() instanceof Creeper) { + // creeper which might need prevention, if inside faction territory + event.setCancelled(true); + } else if (Conf.territoryBlockFireballs && event.getEntity() instanceof Fireball) { + // ghast fireball which might need prevention, if inside faction territory + event.setCancelled(true); } } @@ -105,7 +102,7 @@ public class FactionsEntityListener extends EntityListener { //Log.debug(attacker.getName() + " attacked " + defender.getName()); // Players without faction may be hurt anywhere - if (defender.factionId == 0) { + if (defender.getFaction().getId() == 0) { return true; } diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java index 86639aba..62b36bba 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java @@ -12,22 +12,14 @@ 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; -import com.bukkit.mcteam.factions.util.Log; public class FactionsPlayerListener extends PlayerListener{ - public Factions plugin; - public FactionsPlayerListener(Factions plugin) { - this.plugin = plugin; - } /** * If someone says something that starts with the factions base command