More refactoring

This commit is contained in:
Olof Larsson 2011-03-19 13:00:03 +01:00
parent 1da8b6b30a
commit 0b96a821ce
14 changed files with 455 additions and 305 deletions

View File

@ -4,7 +4,6 @@ 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;
@ -12,7 +11,6 @@ import java.util.Map.Entry;
import org.bukkit.ChatColor;
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;
@ -21,103 +19,100 @@ import com.bukkit.mcteam.util.DiscUtil;
//import com.bukkit.mcteam.factions.util.*;
public class Board {
protected static transient Map<String, Board> instances = new HashMap<String, Board>();
protected static transient File file = new File(Factions.instance.getDataFolder(), "boards.json");
protected static transient File file = new File(Factions.instance.getDataFolder(), "board.json");
private static Map<String,Map<String,Integer>> worldCoordIds = new HashMap<String,Map<String,Integer>>();
public transient String worldName;
protected Map<Coord, Integer> coordFactionIds = new HashMap<Coord, Integer>();
public Board() {
}
public Board(String worldName) {
this.worldName = worldName;
}
public Faction getFactionAt(Coord coord) {
return Faction.get(getFactionIdAt(coord));
}
public int getFactionIdAt(Coord coord) {
Integer factionId = coordFactionIds.get(coord);
if (factionId == null) {
return 0; // No faction
//----------------------------------------------//
// Get and Set
//----------------------------------------------//
public static int getIdAt(FLocation flocation) {
if ( ! worldCoordIds.containsKey(flocation.getWorldName())) {
return 0;
}
return factionId;
if ( ! worldCoordIds.get(flocation.getWorldName()).containsKey(flocation.getCoordString()) ) {
return 0;
}
return worldCoordIds.get(flocation.getWorldName()).get(flocation.getCoordString());
}
public void unclaim(Coord coord) {
coordFactionIds.remove(coord);
public static Faction getFactionAt(FLocation flocation) {
return Faction.get(getIdAt(flocation));
}
public static void setIdAt(int id, FLocation flocation) {
if (id == 0) {
removeAt(flocation);
}
if ( ! worldCoordIds.containsKey(flocation.getWorldName())) {
worldCoordIds.put(flocation.getWorldName(), new HashMap<String,Integer>());
}
worldCoordIds.get(flocation.getWorldName()).put(flocation.getCoordString(), id);
save();
}
public void claim(Coord coord, Faction faction) {
coordFactionIds.put(coord, faction.id);
save();
public static void setFactionAt(Faction faction, FLocation flocation) {
setIdAt(faction.id, flocation);
}
public static void removeAt(FLocation flocation) {
if ( ! worldCoordIds.containsKey(flocation.getWorldName())) {
return;
}
worldCoordIds.get(flocation.getWorldName()).remove(flocation.getCoordString());
save();
}
// Is this coord NOT completely surrounded by coords claimed by the same faction?
// Simpler: Is there any nearby coord with a faction other than the faction here?
public boolean isBorderCoord(Coord coord) {
Faction faction = getFactionAt(coord);
Coord a = coord.getRelative(1, 0);
Coord b = coord.getRelative(-1, 0);
Coord c = coord.getRelative(0, 1);
Coord d = coord.getRelative(0, -1);
return faction != this.getFactionAt(a) || faction != this.getFactionAt(b) || faction != this.getFactionAt(c) || faction != this.getFactionAt(d);
public static boolean isBorderLocation(FLocation flocation) {
Faction faction = getFactionAt(flocation);
FLocation a = flocation.getRelative(1, 0);
FLocation b = flocation.getRelative(-1, 0);
FLocation c = flocation.getRelative(0, 1);
FLocation d = flocation.getRelative(0, -1);
return faction != getFactionAt(a) || faction != getFactionAt(b) || faction != getFactionAt(c) || faction != getFactionAt(d);
}
//----------------------------------------------//
// Clean boards
// Cleaner. Remove orphaned foreign keys
//----------------------------------------------//
// These functions search boards for orphaned foreign keys
public void clean() {
Iterator<Entry<Coord, Integer>> iter = coordFactionIds.entrySet().iterator();
while (iter.hasNext()) {
Entry<Coord, Integer> entry = iter.next();
if ( ! EM.factionExists(entry.getValue())) {
Factions.log("Cleaner removed coord with non existing factionId "+entry.getValue());
iter.remove();
public static void clean() {
for (String worldName : worldCoordIds.keySet()) {
Iterator<Entry<String, Integer>> iter = worldCoordIds.get(worldName).entrySet().iterator();
while (iter.hasNext()) {
Entry<String, Integer> entry = iter.next();
if ( ! Faction.exists(entry.getValue())) {
Factions.log("Board cleaner removed non existing faction id "+entry.getValue()+" from "+worldName+" "+entry.getKey());
iter.remove();
}
}
}
}
public static void cleanAll() {
for (Board board : getAll()) {
Factions.log("Cleaning board for world "+board.worldName);
board.clean();
}
}
//----------------------------------------------//
// Coord count
//----------------------------------------------//
public int getFactionCoordCount(int factionId) {
public static int getFactionCoordCount(int factionId) {
int ret = 0;
for (int thatFactionId : coordFactionIds.values()) {
if(thatFactionId == factionId) {
ret += 1;
for (Map<String, Integer> coordIds : worldCoordIds.values()) {
for (int thatFactionId : coordIds.values()) {
if(thatFactionId == factionId) {
ret += 1;
}
}
}
return ret;
}
public int getFactionCoordCount(Faction faction) {
return getFactionCoordCount(faction.id);
}
public static int getFactionCoordCountAllBoards(int factionId) {
int ret = 0;
for (Board board : getAll()) {
ret += board.getFactionCoordCount(factionId);
}
return ret;
}
public static int getFactionCoordCountAllBoards(Faction faction) {
return getFactionCoordCountAllBoards(faction.id);
public static int getFactionCoordCount(Faction faction) {
return getFactionCoordCount(faction.id);
}
//----------------------------------------------//
@ -129,13 +124,13 @@ public class Board {
* north is in the direction of decreasing x
* east is in the direction of decreasing z
*/
public ArrayList<String> getMap(Faction faction, Coord coord, double inDegrees) {
public ArrayList<String> getMap(Faction faction, FLocation flocation, double inDegrees) {
ArrayList<String> ret = new ArrayList<String>();
ret.add(TextUtil.titleize("("+coord+") "+this.getFactionAt(coord).getTag(faction)));
ret.add(TextUtil.titleize("("+flocation+") "+getFactionAt(flocation).getTag(faction)));
int halfWidth = Conf.mapWidth / 2;
int halfHeight = Conf.mapHeight / 2;
Coord topLeft = coord.getRelative(-halfHeight, halfWidth);
FLocation topLeft = flocation.getRelative(-halfHeight, halfWidth);
int width = halfWidth * 2 + 1;
int height = halfHeight * 2 + 1;
@ -147,8 +142,8 @@ public class Board {
if(dz == -(halfWidth) && dx == halfHeight) {
row += ChatColor.AQUA+"+";
} else {
Coord coordHere = topLeft.getRelative(dx, dz);
Faction factionHere = this.getFactionAt(coordHere);
FLocation flocationHere = topLeft.getRelative(dx, dz);
Faction factionHere = getFactionAt(flocationHere);
if (factionHere.id == 0) {
row += ChatColor.GRAY+"-";
} else {
@ -171,45 +166,17 @@ public class Board {
}
//----------------------------------------------//
// Persistance
//----------------------------------------------//
/*public boolean save() {
return EM.boardSave(this.worldName);
}
public static Board get(World world) {
return EM.boardGet(world);
}
public static Collection<Board> getAll() {
return EM.boardGetAll();
}*/
// -------------------------------------------- //
// Persistance
// -------------------------------------------- //
public boolean shouldBeSaved() {
return this.coordFactionIds.size() > 0;
}
public static boolean save() {
Factions.log("Saving boards to disk");
Map<String, Board> instancesToSave = new HashMap<String, Board>();
for (Entry<String, Board> entry : instances.entrySet()) {
if (entry.getValue().shouldBeSaved()) {
instancesToSave.put(entry.getKey(), entry.getValue());
}
}
Factions.log("Saving board to disk");
try {
DiscUtil.write(file, Factions.gson.toJson(instancesToSave));
DiscUtil.write(file, Factions.gson.toJson(worldCoordIds));
} catch (IOException e) {
Factions.log("Failed to save the boards to disk.");
Factions.log("Failed to save the board to disk.");
e.printStackTrace();
return false;
}
@ -218,48 +185,22 @@ public class Board {
public static boolean load() {
if ( ! file.exists()) {
Factions.log("No boards to load from disk. Creating new file.");
Factions.log("No board to load from disk. Creating new file.");
save();
return true;
}
try {
Type type = new TypeToken<Map<String, Board>>(){}.getType();
instances = Factions.gson.fromJson(DiscUtil.read(file), type);
Type type = new TypeToken<Map<String,Map<String,Integer>>>(){}.getType();
worldCoordIds = Factions.gson.fromJson(DiscUtil.read(file), type);
} catch (IOException e) {
Factions.log("Failed to load the board from disk.");
e.printStackTrace();
return false;
}
fillNames();
return true;
}
public static void fillNames() {
for(Entry<String, Board> 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<Board> getAll() {
return instances.values();
}
}

View File

@ -243,7 +243,7 @@ public class Commands {
}
me.sendMessage(helpPages.get(page), false);
}
/*
public static void leave(FPlayer me) {
Faction faction = me.getFaction();
@ -255,7 +255,7 @@ public class Commands {
me.sendMessage("You left "+faction.getTag(me));
}
if (faction.getFollowersAll().size() == 0) {
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.");
@ -313,6 +313,7 @@ public class Commands {
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<String> errors = new ArrayList<String>();
@ -369,9 +370,9 @@ public class Commands {
return 1;
else if (f2.id == 0)
return -1;
else if (f1.getFollowersAll().size() < f2.getFollowersAll().size())
else if (f1.getFPlayers().size() < f2.getFPlayers().size())
return 1;
else if (f1.getFollowersAll().size() > f2.getFollowersAll().size())
else if (f1.getFPlayers().size() > f2.getFPlayers().size())
return -1;
return 0;
}
@ -381,9 +382,9 @@ public class Commands {
Collections.sort(FactionList, new Comparator<Faction>(){
@Override
public int compare(Faction f1, Faction f2) {
if (f1.getFollowersWhereOnline(true).size() < f2.getFollowersWhereOnline(true).size())
if (f1.getFPlayersWhereOnline(true).size() < f2.getFPlayersWhereOnline(true).size())
return 1;
else if (f1.getFollowersWhereOnline(true).size() > f2.getFollowersWhereOnline(true).size())
else if (f1.getFPlayersWhereOnline(true).size() > f2.getFPlayersWhereOnline(true).size())
return -1;
return 0;
}
@ -404,9 +405,9 @@ public class Commands {
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.getFollowersWhereOnline(true).size() + " online");
me.sendMessage(faction.getTag(me)+Conf.colorSystem+" "+faction.getFPlayersWhereOnline(true).size() + " online");
} else {
me.sendMessage(faction.getTag(me)+Conf.colorSystem+" "+faction.getFollowersWhereOnline(true).size()+"/"+faction.getFollowersAll().size()+" online, "+faction.getLandRounded()+"/"+faction.getPowerRounded()+"/"+faction.getPowerMaxRounded());
me.sendMessage(faction.getTag(me)+Conf.colorSystem+" "+faction.getFPlayersWhereOnline(true).size()+"/"+faction.getFPlayers().size()+" online, "+faction.getLandRounded()+"/"+faction.getPowerRounded()+"/"+faction.getPowerMaxRounded());
}
}
}
@ -416,9 +417,9 @@ public class Commands {
if (faction == null) {
return;
}
Collection<FPlayer> admins = faction.getFollowersWhereRole(Role.ADMIN);
Collection<FPlayer> mods = faction.getFollowersWhereRole(Role.MODERATOR);
Collection<FPlayer> normals = faction.getFollowersWhereRole(Role.NORMAL);
Collection<FPlayer> admins = faction.getFPlayersWhereRole(Role.ADMIN);
Collection<FPlayer> mods = faction.getFPlayersWhereRole(Role.MODERATOR);
Collection<FPlayer> normals = faction.getFPlayersWhereRole(Role.NORMAL);
me.sendMessage(TextUtil.titleize(faction.getTag(me)), false);
me.sendMessage(Conf.colorChrome+"Description: "+Conf.colorSystem+faction.getDescription());

View File

@ -5,22 +5,22 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Player;
public class Coord {
public class CoordOld {
protected static transient int cellSize = 16;
public int x, z;
public Coord(int x, int z) {
public CoordOld(int x, int z) {
this.x = x;
this.z = z;
}
// TODO implements cloneable
public Coord(Coord coord) {
public CoordOld(Coord coord) {
this.x = coord.x;
this.z = coord.z;
}
public Coord() {
public CoordOld() {
// Noarg constructor for google gson.
}

View File

@ -0,0 +1,101 @@
package com.bukkit.mcteam.factions;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
public class FLocation {
private String worldName = "world";
private long x = 0;
private long z = 0;
private final static transient double cellSize = 16;
//----------------------------------------------//
// Constructors
//----------------------------------------------//
public FLocation() {
}
public FLocation(String worldName, long x, long 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));
}
public FLocation(Player player) {
this(player.getLocation());
}
public FLocation(FPlayer fplayer) {
this(fplayer.getPlayer());
}
public FLocation(Block block) {
this(block.getLocation());
}
//----------------------------------------------//
// Getters and Setters
//----------------------------------------------//
public String getWorldName() {
return worldName;
}
public void setWorldName(String worldName) {
this.worldName = worldName;
}
public long getX() {
return x;
}
public void setX(long x) {
this.x = x;
}
public long getZ() {
return z;
}
public void setZ(long z) {
this.z = z;
}
public String getCoordString() {
return ""+x+","+z;
}
//----------------------------------------------//
// Misc
//----------------------------------------------//
public FLocation getRelative(int dx, int dz) {
return new FLocation(this.worldName, this.x + dx, this.z + dz);
}
//----------------------------------------------//
// Comparison
//----------------------------------------------//
// TODO hash code
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof FLocation))
return false;
FLocation o = (FLocation) obj;
return this.x == o.x && this.z == o.z && this.worldName.equals(o.worldName);
}
}

View File

@ -9,7 +9,8 @@ import java.util.Map.Entry;
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.gson.reflect.TypeToken;
import com.bukkit.mcteam.util.DiscUtil;
@ -18,7 +19,7 @@ public class FPlayer {
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 transient FLocation lastStoodAt = new FLocation(); // Where did this player stand the last time we checked?
public int factionId;
public Role role;
@ -38,7 +39,17 @@ public class FPlayer {
// GSON need this noarg constructor.
public FPlayer() {
this.resetFactionData();
this.power = this.getPowerMax();
this.lastPowerUpdateTime = System.currentTimeMillis();
this.mapAutoUpdating = false;
}
public void resetFactionData() {
this.factionId = 0; // The default neutral faction
this.factionChatting = false;
this.role = Role.NORMAL;
this.title = "";
}
public Player getPlayer() {
@ -61,10 +72,6 @@ public class FPlayer {
return ! isOnline();
}
public boolean isFactionChatting() {
if (this.factionId == 0) {
return false;
@ -76,19 +83,7 @@ public class FPlayer {
this.factionChatting = factionChatting;
}
public FPlayer() {
this.resetFactionData();
this.power = this.getPowerMax();
this.lastPowerUpdateTime = System.currentTimeMillis();
this.mapAutoUpdating = false;
}
protected void resetFactionData() {
this.factionId = 0; // The default neutral faction
this.factionChatting = false;
this.role = Role.NORMAL;
this.title = "";
}
public boolean isMapAutoUpdating() {
return mapAutoUpdating;
@ -110,15 +105,15 @@ public class FPlayer {
public void setTitle(String title) {
this.title = title;
this.save();
save();
}
public String getName() {
return this.id;
return this.playername;
}
public String getTag() {
if (this.withoutFaction()) {
if ( ! this.hasFaction()) {
return "";
}
return this.getFaction().getTag();
@ -180,7 +175,7 @@ public class FPlayer {
// These are injected into the format of global chat messages.
public String getChatTag() {
if (this.withoutFaction()) {
if ( ! this.hasFaction()) {
return "";
}
@ -189,14 +184,14 @@ public class FPlayer {
// Colored Chat Tag
public String getChatTag(Faction faction) {
if (this.withoutFaction()) {
if ( ! this.hasFaction()) {
return "";
}
return this.getRelation(faction).getColor()+getChatTag();
}
public String getChatTag(FPlayer follower) {
if (this.withoutFaction()) {
if ( ! this.hasFaction()) {
return "";
}
@ -293,20 +288,16 @@ public class FPlayer {
// Territory
//----------------------------------------------//
public boolean isInOwnTerritory() {
return Board.get(this.getPlayer().getWorld()).getFactionAt(this.getCoord()) == this.getFaction();
return Board.getIdAt(new FLocation(this)) == this.factionId;
}
public boolean isInOthersTerritory() {
Faction factionHere = Board.get(this.getPlayer().getWorld()).getFactionAt(this.getCoord());
return factionHere.id != 0 && factionHere != this.getFaction();
}
public Coord getCoord() {
return Coord.from(this);
int idHere = Board.getIdAt(new FLocation(this));
return idHere != 0 && idHere != this.factionId;
}
public void sendFactionHereMessage() {
Faction factionHere = Board.get(this.getPlayer().getWorld()).getFactionAt(this.getCoord());
Faction factionHere = Board.getFactionAt(new FLocation(this));
String msg = Conf.colorSystem+" ~ "+factionHere.getTag(this);
if (factionHere.id != 0) {
msg += " - "+factionHere.getDescription();
@ -318,66 +309,12 @@ public class FPlayer {
// Faction management
//----------------------------------------------//
public Faction getFaction() {
return EM.factionGet(factionId);
return Faction.get(factionId);
}
public boolean hasFaction() {
return factionId != 0;
}
public boolean withoutFaction() {
return factionId == 0;
}
public ArrayList<String> join(Faction faction) {
ArrayList<String> errors = new ArrayList<String>();
if (faction.id == this.factionId) {
errors.add(Conf.colorSystem+"You are already a member of "+faction.getRelationColor(this)+faction.getTag());
}
if( ! faction.getOpen() && ! faction.isInvited(this)) {
errors.add(Conf.colorSystem+"This guild requires invitation.");
}
if (this.hasFaction()) {
errors.add(Conf.colorSystem+"You must leave your current faction first.");
}
if (errors.size() > 0) {
return errors;
}
this.resetFactionData();
if(faction.getFollowersAll().size() == 0) {
this.role = Role.ADMIN;
} else {
this.role = Role.NORMAL;
}
this.factionId = faction.id;
faction.deinvite(this);
this.save();
return errors;
}
public ArrayList<String> leave() {
ArrayList<String> errors = new ArrayList<String>();
if (this.role == Role.ADMIN && this.getFaction().getFollowersAll().size() > 1) {
errors.add(Conf.colorSystem+"You must give the admin role to someone else first.");
}
if(this.withoutFaction()) {
errors.add(Conf.colorSystem+"You are not member of any faction.");
}
if (errors.size() > 0) {
return errors;
}
this.resetFactionData();
this.save();
return errors;
}
public ArrayList<String> invite(FPlayer follower) {
ArrayList<String> errors = new ArrayList<String>();
@ -430,30 +367,8 @@ public class FPlayer {
return follower.getFaction().kick(follower);
}
//----------------------------------------------//
// Login info
//----------------------------------------------//
public void sendJoinInfo() { // TODO Missplaced!?
// Do we even whant to use message of the day...
// Perhaps that is up to another plugin...
//this.getPlayer().sendMessage(ChatColor.GREEN + "This is a faction server! Type "+Conf.colorCommand+"/f"+ChatColor.GREEN +" for more info :D");
}
//----------------------------------------------//
// Search
//----------------------------------------------//
public static FPlayer find(String name) { // TODO felaktig!
for (FPlayer follower : EM.followerGetAll()) {
if (follower.getName().equalsIgnoreCase(name.trim())) {
return follower;
}
}
return null;
}
// -------------------------------------------- //
// Get
// Get and search
// 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.
@ -473,6 +388,27 @@ public class FPlayer {
return get(player.getName());
}
public static Set<FPlayer> getAllOnline() {
Set<FPlayer> fplayers = new HashSet<FPlayer>();
for (Player player : Factions.instance.getServer().getOnlinePlayers()) {
fplayers.add(FPlayer.get(player));
}
return fplayers;
}
public static Collection<FPlayer> getAll() {
return instances.values();
}
public static FPlayer find(String playername) {
for (Entry<String, FPlayer> entry : instances.entrySet()) {
if (entry.getKey().equalsIgnoreCase(playername)) {
return entry.getValue();
}
}
return null;
}
// -------------------------------------------- //
// Messages
// -------------------------------------------- //
@ -486,23 +422,6 @@ public class FPlayer {
}
}
//----------------------------------------------//
// Persistance and entity management
//----------------------------------------------//
/*
public boolean save() {
return EM.followerSave(this.id);
}
public static FPlayer get(Player player) {
return EM.followerGet(player);
}
public static Collection<FPlayer> getAll() {
return EM.followerGetAll();
}
*/
// -------------------------------------------- //
// Persistance
// -------------------------------------------- //

View File

@ -83,7 +83,7 @@ public class Faction {
//----------------------------------------------//
public double getPower() {
double ret = 0;
for (FPlayer follower : this.getFollowersAll()) {
for (FPlayer follower : this.getFPlayers()) {
ret += follower.getPower();
}
return ret;
@ -91,7 +91,7 @@ public class Faction {
public double getPowerMax() {
double ret = 0;
for (FPlayer follower : this.getFollowersAll()) {
for (FPlayer follower : this.getFPlayers()) {
ret += follower.getPowerMax();
}
return ret;
@ -166,7 +166,7 @@ public class Faction {
// Followers
// -------------------------------
public ArrayList<FPlayer> getFollowersAll() {
public ArrayList<FPlayer> getFPlayers() {
ArrayList<FPlayer> ret = new ArrayList<FPlayer>();
for (FPlayer follower : FPlayer.getAll()) {
if (follower.factionId == this.id) {
@ -176,7 +176,7 @@ public class Faction {
return ret;
}
public ArrayList<FPlayer> getFollowersWhereOnline(boolean online) {
public ArrayList<FPlayer> getFPlayersWhereOnline(boolean online) {
ArrayList<FPlayer> ret = new ArrayList<FPlayer>();
for (FPlayer follower : FPlayer.getAll()) {
if (follower.factionId == this.id && follower.isOnline() == online) {
@ -186,7 +186,7 @@ public class Faction {
return ret;
}
public ArrayList<FPlayer> getFollowersWhereRole(Role role) {
public ArrayList<FPlayer> getFPlayersWhereRole(Role role) {
ArrayList<FPlayer> ret = new ArrayList<FPlayer>();
for (FPlayer follower : FPlayer.getAll()) {

View File

@ -4,7 +4,6 @@ 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;
@ -23,7 +22,6 @@ import com.bukkit.mcteam.factions.listeners.FactionsEntityListener;
import com.bukkit.mcteam.factions.listeners.FactionsPlayerListener;
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;
@ -39,7 +37,6 @@ public class Factions extends JavaPlugin {
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);

View File

@ -8,7 +8,9 @@ import org.bukkit.entity.Player;
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.struct.Role;
public class FCommand {
public List<String> requiredParameters;
@ -22,7 +24,7 @@ public class FCommand {
public CommandSender sender;
public boolean senderMustBePlayer;
public Player player;
public FPlayer fplayer;
public FPlayer me;
public List<String> parameters;
@ -69,7 +71,7 @@ public class FCommand {
if (this.senderMustBePlayer) {
this.player = (Player)sender;
this.fplayer = FPlayer.get(this.player);
this.me = FPlayer.get(this.player);
}
perform();
@ -139,4 +141,72 @@ public class FCommand {
Player player = (Player)sender;
return Factions.Permissions.has(player, this.permissions);
}
// -------------------------------------------- //
// Commonly used logic
// -------------------------------------------- //
public FPlayer findFPlayer(String playerName, boolean defaultToMe) {
FPlayer fp = FPlayer.find(playerName);
if (fp == null) {
if (defaultToMe) {
return me;
}
sendMessage("The player \""+playerName+"\" could not be found");
}
return fp;
}
public FPlayer findFPlayer(String playerName) {
return findFPlayer(playerName, false);
}
public Faction findFaction(String factionName, boolean defaultToMine) {
// First we search player names
FPlayer fp = FPlayer.find(factionName);
if (fp != null) {
return fp.getFaction();
}
// Secondly we search faction names
Faction faction = Faction.findByTag(factionName);
if (faction != null) {
return faction;
}
if (defaultToMine) {
return me.getFaction();
}
me.sendMessage(Conf.colorSystem+"No faction or player \""+factionName+"\" was found");
return null;
}
public Faction findFaction(String factionName) {
return findFaction(factionName, false);
}
public 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;
}
}

View File

@ -0,0 +1,59 @@
package com.bukkit.mcteam.factions.commands;
import java.util.ArrayList;
import com.bukkit.mcteam.factions.Conf;
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Faction;
import com.bukkit.mcteam.factions.struct.Role;
public class FCommandCreate extends FCommand {
public FCommandCreate() {
requiredParameters = new ArrayList<String>();
optionalParameters = new ArrayList<String>();
requiredParameters.add("faction tag");
permissions = "";
senderMustBePlayer = true;
helpNameAndParams = "create [faction tag]";
helpDescription = "Create new faction";
}
public void perform() {
String tag = parameters.get(0);
if (me.hasFaction()) {
sendMessage("You must leave your current faction first.");
return;
}
if (Faction.isTagTaken(tag)) {
sendMessage("That tag is already in use.");
return;
}
ArrayList<String> tagValidationErrors = Faction.validateTag(tag);
if (tagValidationErrors.size() > 0) {
sendMessage(tagValidationErrors);
return;
}
Faction faction = Faction.create();
faction.setTag(tag);
me.role = Role.ADMIN;
me.factionId = faction.id;
Faction.save();
FPlayer.save();
for (FPlayer follower : FPlayer.getAllOnline()) {
follower.sendMessage(me.getNameAndRelevant(follower)+Conf.colorSystem+" created a new faction "+faction.getTag(follower));
}
sendMessage("Now update your faction description. Use:");
sendMessage(Conf.colorCommand+Conf.aliasBase.get(0)+" "+Conf.aliasDescription.get(0)+" "+"[description]");
}
}

View File

@ -0,0 +1,57 @@
package com.bukkit.mcteam.factions.commands;
import java.util.ArrayList;
import com.bukkit.mcteam.factions.Conf;
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Faction;
public class FCommandJoin extends FCommand {
public FCommandJoin() {
requiredParameters = new ArrayList<String>();
optionalParameters = new ArrayList<String>();
requiredParameters.add("faction name");
permissions = "";
senderMustBePlayer = true;
helpNameAndParams = "join [faction name]";
helpDescription = "Join a faction";
}
public void perform() {
String factionName = parameters.get(0);
Faction faction = findFaction(factionName);
if (faction == null) {
return;
}
if (faction.id == me.factionId) {
sendMessage("You are already a member of "+faction.getTag(me));
return;
}
if (me.hasFaction()) {
sendMessage("You must leave your current faction first.");
return;
}
if( ! faction.getOpen() && ! faction.isInvited(me)) {
sendMessage("This guild requires invitation.");
faction.sendMessage(me.getNameAndRelevant(faction)+Conf.colorSystem+" tried to join your faction.");
return;
}
me.sendMessage(Conf.colorSystem+"You successfully joined "+faction.getTag(me));
faction.sendMessage(me.getNameAndRelevant(faction)+Conf.colorSystem+" joined your faction.");
me.resetFactionData();
me.factionId = faction.id;
faction.deinvite(me);
FPlayer.save();
}
}

View File

@ -2,11 +2,10 @@ 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;
import com.bukkit.mcteam.factions.struct.Role;
public class FCommandLeave extends FCommand {
@ -23,20 +22,26 @@ public class FCommandLeave extends FCommand {
}
public void perform() {
Faction faction = fplayer.getFaction();
ArrayList<String> 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 ( ! me.hasFaction()) {
sendMessage("You are not member of any faction.");
return;
}
if (faction.getFollowersAll().size() == 0) {
Faction faction = me.getFaction();
if (me.role == Role.ADMIN && faction.getFPlayers().size() > 1) {
sendMessage("You must give the admin role to someone else first.");
return;
}
faction.sendMessage(me.getNameAndRelevant(faction) + Conf.colorSystem + " left your faction.");
me.resetFactionData();
FPlayer.save();
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.");
for (FPlayer fplayer : FPlayer.getAllOnline()) {
fplayer.sendMessage("The faction "+faction.getTag(fplayer)+Conf.colorSystem+" was disbanded.");
}
Faction.delete(faction.id);
}

View File

@ -21,7 +21,7 @@ import com.bukkit.mcteam.gson.*;
* Before using the the EM you should always EM.loadAll().
* The methods assume that all on disc is loaded into memory.
*/
public class EM {
public class EMOld {
protected static Map<String, FPlayer> followers = new HashMap<String, FPlayer>(); // Where String is a lowercase playername
protected static Map<Integer, Faction> factions = new HashMap<Integer, Faction>(); // Where Integer is a primary auto increment key
protected static Map<String, Board> boards = new HashMap<String, Board>(); // Where Long is the semi (sadly) unique world id.

View File

@ -131,14 +131,14 @@ public class FactionsPlayerListener extends PlayerListener{
FPlayer me = FPlayer.get(event.getPlayer());
// Did we change coord?
Coord coordFrom = me.lastStoodInCoord;
Coord coordFrom = me.lastStoodAt;
Coord coordTo = Coord.from(event.getTo());
if (coordFrom.equals(coordTo)) {
return;
}
// Yes we did change coord (:
me.lastStoodInCoord = coordTo;
me.lastStoodAt = coordTo;
Board board = Board.get(event.getPlayer().getWorld());
if (me.isMapAutoUpdating()) {

0
test.jpage Normal file
View File