Started the refactoring

This commit is contained in:
Olof Larsson 2011-03-18 17:33:23 +01:00
parent 35d7985fb2
commit 1da8b6b30a
18 changed files with 849 additions and 471 deletions

View File

@ -1,23 +1,38 @@
package com.bukkit.mcteam.factions.entities;
package com.bukkit.mcteam.factions;
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.ChatColor;
import org.bukkit.World;
import com.bukkit.mcteam.factions.util.Log;
import com.bukkit.mcteam.factions.entities.EM;
import com.bukkit.mcteam.factions.util.TextUtil;
import com.bukkit.mcteam.gson.reflect.TypeToken;
import com.bukkit.mcteam.util.AsciiCompass;
import com.bukkit.mcteam.util.DiscUtil;
//import com.bukkit.mcteam.factions.util.*;
public class Board {
protected static transient Map<String, Board> instances = new HashMap<String, Board>();
protected static transient File file = new File(Factions.instance.getDataFolder(), "boards.json");
public transient String worldName;
protected Map<Coord, Integer> coordFactionIds;
protected Map<Coord, Integer> coordFactionIds = new HashMap<Coord, Integer>();
public Board() {
coordFactionIds = new HashMap<Coord, Integer>();
}
public Board(String worldName) {
this.worldName = worldName;
}
public Faction getFactionAt(Coord coord) {
@ -64,7 +79,7 @@ public class Board {
while (iter.hasNext()) {
Entry<Coord, Integer> entry = iter.next();
if ( ! EM.factionExists(entry.getValue())) {
Log.debug("Cleaner removed coord with non existing factionId "+entry.getValue());
Factions.log("Cleaner removed coord with non existing factionId "+entry.getValue());
iter.remove();
}
}
@ -72,37 +87,11 @@ public class Board {
public static void cleanAll() {
for (Board board : getAll()) {
Log.debug("Cleaning board for world "+board.worldName);
Factions.log("Cleaning board for world "+board.worldName);
board.clean();
}
}
//----------------------------------------------//
// Purge faction Currently skipped and we use clean instead as that will solve orphaned keys to :)
//----------------------------------------------//
/*
public void purgeFaction(int factionId) {
Iterator<Entry<Coord, Integer>> iter = coordFactionIds.entrySet().iterator();
while (iter.hasNext()) {
Entry<Coord, Integer> entry = iter.next();
if (entry.getValue().equals(factionId)) {
iter.remove();
}
}
}
public void purgeFaction(Faction faction) {
purgeFaction(faction.id);
}
public static void purgeFactionFromAllBoards(int factionId) {
for (Board board : getAll()) {
board.purgeFaction(factionId);
}
}
public static void purgeFactionFromAllBoards(Faction faction) {
purgeFactionFromAllBoards(faction.id);
}*/
//----------------------------------------------//
// Coord count
//----------------------------------------------//
@ -186,7 +175,7 @@ public class Board {
// Persistance
//----------------------------------------------//
public boolean save() {
/*public boolean save() {
return EM.boardSave(this.worldName);
}
@ -196,6 +185,80 @@ public class Board {
public static Collection<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());
}
}
try {
DiscUtil.write(file, Factions.gson.toJson(instancesToSave));
} catch (IOException e) {
Factions.log("Failed to save the boards to disk.");
e.printStackTrace();
return false;
}
return true;
}
public static boolean load() {
if ( ! file.exists()) {
Factions.log("No boards to load from disk. Creating new file.");
save();
return true;
}
try {
Type type = new TypeToken<Map<String, Board>>(){}.getType();
instances = Factions.gson.fromJson(DiscUtil.read(file), type);
} catch (IOException e) {
e.printStackTrace();
return false;
}
fillNames();
return true;
}
public static void fillNames() {
for(Entry<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

@ -98,12 +98,12 @@ public class Commands {
// Update to work with tag and follower names
public static Follower findFollower(Follower me, String name, boolean defaultsToMe) {
public static FPlayer findFollower(FPlayer me, String name, boolean defaultsToMe) {
if (name.length() == 0 && defaultsToMe) {
return me;
}
Follower follower = Follower.find(name);
FPlayer follower = FPlayer.find(name);
if (follower != null) {
return follower;
}
@ -112,13 +112,13 @@ public class Commands {
return null;
}
public static Faction findFaction(Follower me, String name, boolean defaultsToMe) {
public static Faction findFaction(FPlayer me, String name, boolean defaultsToMe) {
if (name.length() == 0 && defaultsToMe) {
return me.getFaction();
}
// Search player names
Follower follower = Follower.find(name);
FPlayer follower = FPlayer.find(name);
if (follower != null) {
return follower.getFaction();
}
@ -133,7 +133,7 @@ public class Commands {
return null;
}
public static boolean canIAdministerYou(Follower i, Follower you) {
public static boolean canIAdministerYou(FPlayer i, FPlayer you) {
if ( ! i.getFaction().equals(you.getFaction())) {
i.sendMessage(you.getNameAndRelevant(i)+Conf.colorSystem+" is not in the same faction as you.");
return false;
@ -158,7 +158,7 @@ public class Commands {
// The base command
//----------------------------------------------//
public static void base(Follower me, ArrayList<String> tokens) {
public static void base(FPlayer me, ArrayList<String> tokens) {
if (tokens.size() == 0) {
help(me);
return;
@ -230,11 +230,11 @@ public class Commands {
//----------------------------------------------//
// The other commands
//----------------------------------------------//
public static void help(Follower me) {
public static void help(FPlayer me) {
help(me, 1);
}
public static void help(Follower me, Integer page) {
public static void help(FPlayer me, Integer page) {
me.sendMessage(TextUtil.titleize("Factions Help ("+page+"/"+helpPages.size()+")"), false);
page -= 1;
if (page < 0 || page >= helpPages.size()) {
@ -244,7 +244,7 @@ public class Commands {
me.sendMessage(helpPages.get(page), false);
}
public static void leave(Follower me) {
public static void leave(FPlayer me) {
Faction faction = me.getFaction();
ArrayList<String> errors = me.leave();
@ -257,14 +257,14 @@ public class Commands {
if (faction.getFollowersAll().size() == 0) {
// Remove this faction
for (Follower follower : Follower.getAll()) {
for (FPlayer follower : FPlayer.getAll()) {
follower.sendMessage(Conf.colorSystem+"The faction "+faction.getTag(follower)+Conf.colorSystem+" was disbanded.");
}
EM.factionDelete(faction.id);
}
}
public static void join(Follower me, String name) {
public static void join(FPlayer me, String name) {
Faction faction = findFaction(me, name, false);
if (faction == null) {
return;
@ -281,7 +281,7 @@ public class Commands {
}
}
public static void create(Follower me, String tag) {
public static void create(FPlayer me, String tag) {
ArrayList<String> errors = new ArrayList<String>();
if (me.hasFaction()) {
@ -306,7 +306,7 @@ public class Commands {
me.role = Role.ADMIN;
me.save();
for (Follower follower : Follower.getAll()) {
for (FPlayer follower : FPlayer.getAll()) {
follower.sendMessage(me.getNameAndRelevant(follower)+Conf.colorSystem+" created a new faction "+faction.getTag(follower));
}
@ -314,7 +314,7 @@ public class Commands {
me.sendMessage(Conf.colorCommand+Conf.aliasBase.get(0)+" "+Conf.aliasDescription.get(0)+" "+"[description]");
}
public static void tag(Follower me, String tag) {
public static void tag(FPlayer me, String tag) {
ArrayList<String> errors = new ArrayList<String>();
if (me.withoutFaction()) {
@ -349,7 +349,7 @@ public class Commands {
}
}
public static void list(Follower me, String inPage) {
public static void list(FPlayer me, String inPage) {
ArrayList<Faction> FactionList = new ArrayList<Faction>(Faction.getAll());
int page = 1;
@ -411,14 +411,14 @@ public class Commands {
}
}
public static void showFaction(Follower me, String name) {
public static void showFaction(FPlayer me, String name) {
Faction faction = findFaction(me, name, true);
if (faction == null) {
return;
}
Collection<Follower> admins = faction.getFollowersWhereRole(Role.ADMIN);
Collection<Follower> mods = faction.getFollowersWhereRole(Role.MODERATOR);
Collection<Follower> normals = faction.getFollowersWhereRole(Role.NORMAL);
Collection<FPlayer> admins = faction.getFollowersWhereRole(Role.ADMIN);
Collection<FPlayer> mods = faction.getFollowersWhereRole(Role.MODERATOR);
Collection<FPlayer> normals = faction.getFollowersWhereRole(Role.NORMAL);
me.sendMessage(TextUtil.titleize(faction.getTag(me)), false);
me.sendMessage(Conf.colorChrome+"Description: "+Conf.colorSystem+faction.getDescription());
@ -462,7 +462,7 @@ public class Commands {
// List the members...
String onlineList = Conf.colorChrome+"Members online: ";
String offlineList = Conf.colorChrome+"Members offline: ";
for (Follower follower : admins) {
for (FPlayer follower : admins) {
listpart = follower.getNameAndTitle(me)+Conf.colorSystem+", ";
if (follower.isOnline()) {
onlineList += listpart;
@ -470,7 +470,7 @@ public class Commands {
offlineList += listpart;
}
}
for (Follower follower : mods) {
for (FPlayer follower : mods) {
listpart = follower.getNameAndTitle(me)+Conf.colorSystem+", ";
if (follower.isOnline()) {
onlineList += listpart;
@ -478,7 +478,7 @@ public class Commands {
offlineList += listpart;
}
}
for (Follower follower : normals) {
for (FPlayer follower : normals) {
listpart = follower.getNameAndTitle(me)+Conf.colorSystem+", ";
if (follower.isOnline()) {
onlineList += listpart;
@ -499,7 +499,7 @@ public class Commands {
}
public static void showMap(Follower me, String mapAutoUpdating) {
public static void showMap(FPlayer me, String mapAutoUpdating) {
Board board = Board.get(me.getPlayer().getWorld());
if (mapAutoUpdating.length() > 0) {
if (Conf.aliasTrue.contains(mapAutoUpdating.toLowerCase())) {
@ -519,8 +519,8 @@ public class Commands {
}
}
public static void invite(Follower me, String name) {
Follower follower = findFollower(me, name, false);
public static void invite(FPlayer me, String name) {
FPlayer follower = findFollower(me, name, false);
if (follower == null) {
return;
}
@ -534,8 +534,8 @@ public class Commands {
}
}
public static void deinvite(Follower me, String name) { // TODO Move out!
Follower follower = findFollower(me, name, false);
public static void deinvite(FPlayer me, String name) { // TODO Move out!
FPlayer follower = findFollower(me, name, false);
if (follower == null) {
return;
}
@ -549,7 +549,7 @@ public class Commands {
}
}
public static void open(Follower me) {
public static void open(FPlayer me) {
if (me.role.value < Role.MODERATOR.value) {
me.sendMessage(Conf.colorSystem+"You must be moderator to do this");
return;
@ -569,7 +569,7 @@ public class Commands {
}
}
public static void title(Follower me, ArrayList<String> tokens) {
public static void title(FPlayer me, ArrayList<String> tokens) {
if (tokens.size() == 0) {
me.sendMessage(Conf.colorSystem+"You must specify a player name");
return;
@ -578,7 +578,7 @@ public class Commands {
String name = tokens.get(0);
tokens.remove(0);
Follower you = findFollower(me, name, true);
FPlayer you = findFollower(me, name, true);
if (you == null) {
return;
}
@ -596,13 +596,13 @@ public class Commands {
myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" changed a title: "+you.getNameAndRelevant(myFaction));
}
public static void kick(Follower me, String name) {
public static void kick(FPlayer me, String name) {
if (name.length() == 0) {
me.sendMessage(Conf.colorSystem+"You must specify a player name.");
return;
}
Follower you = findFollower(me, name, false);
FPlayer you = findFollower(me, name, false);
if (you == null) {
return;
}
@ -617,7 +617,7 @@ public class Commands {
}
}
public static void roleChange(Follower me, Role targetRole, String name) {
public static void roleChange(FPlayer me, Role targetRole, String name) {
if (me.role.value < Role.ADMIN.value) {
me.sendMessage(Conf.colorSystem+"You must be faction admin to do this");
return;
@ -628,7 +628,7 @@ public class Commands {
return;
}
Follower targetFollower = findFollower(me, name, false);
FPlayer targetFollower = findFollower(me, name, false);
if (targetFollower == null) {
return;
}
@ -648,7 +648,7 @@ public class Commands {
targetFollower.role = Role.ADMIN;
// Inform all players
for (Follower follower : Follower.getAll()) {
for (FPlayer follower : FPlayer.getAll()) {
if (follower.factionId == me.factionId) {
follower.sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" gave "+targetFollower.getNameAndRelevant(me)+Conf.colorSystem+" the leadership of your faction.");
} else {
@ -668,7 +668,7 @@ public class Commands {
}
}
public static void claim(Follower me) {
public static void claim(FPlayer me) {
if (me.withoutFaction()) {
me.sendMessage(Conf.colorSystem+"You are not part of any faction.");
return;
@ -723,7 +723,7 @@ public class Commands {
board.claim(coord, myFaction);
}
public static void unclaim(Follower me) {
public static void unclaim(FPlayer me) {
if (me.withoutFaction()) {
me.sendMessage(Conf.colorSystem+"You are not part of any faction");
return;
@ -746,7 +746,7 @@ public class Commands {
me.getFaction().sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" unclaimed some land.");
}
public static void relation(Follower me, Relation whishedRelation, String otherFactionName) {
public static void relation(FPlayer me, Relation whishedRelation, String otherFactionName) {
if (me.withoutFaction()) {
me.sendMessage(Conf.colorSystem+"You are not part of any faction.");
return;
@ -791,7 +791,7 @@ public class Commands {
}
}
public static void description(Follower me, String desc) {
public static void description(FPlayer me, String desc) {
if (me.withoutFaction()) {
me.sendMessage(Conf.colorSystem+"You are not part of any faction");
return;
@ -807,13 +807,13 @@ public class Commands {
me.sendMessage(Conf.colorSystem+"The new description was set :D");
// Broadcast the description to everyone
for (Follower follower : EM.followerGetAll()) {
for (FPlayer follower : EM.followerGetAll()) {
follower.sendMessage(Conf.colorSystem+"The faction "+follower.getRelationColor(me)+me.getFaction().getTag()+Conf.colorSystem+" changed their description to:");
follower.sendMessage(Conf.colorSystem+desc);
}
}
public static void chat(Follower me, String msg) {
public static void chat(FPlayer me, String msg) {
if (me.withoutFaction()) {
me.sendMessage(Conf.colorSystem+"You are not part of any faction");
return;
@ -830,8 +830,8 @@ public class Commands {
}
}
public static void version(Follower me) {
me.sendMessage(Conf.colorSystem+"You are running "+Factions.factions.getDescription().getFullName());
public static void version(FPlayer me) {
me.sendMessage(Conf.colorSystem+"You are running "+Factions.instance.getDescription().getFullName());
}
}

View File

@ -1,9 +1,15 @@
package com.bukkit.mcteam.factions.entities;
package com.bukkit.mcteam.factions;
import java.io.File;
import java.io.IOException;
import java.util.*;
import org.bukkit.*;
import com.bukkit.mcteam.util.DiscUtil;
public class Conf {
public static transient File file = new File(Factions.instance.getDataFolder(), "conf.json");
// Colors
public static ChatColor colorMember = ChatColor.GREEN;
public static ChatColor colorAlly = ChatColor.LIGHT_PURPLE;
@ -14,8 +20,6 @@ public class Conf {
public static ChatColor colorChrome = ChatColor.GOLD;
public static ChatColor colorCommand = ChatColor.AQUA;
public static ChatColor colorParameter = ChatColor.DARK_AQUA;
public static Integer logThreshold = 10;
// Power
public static double powerPlayerMax = 10;
@ -154,11 +158,37 @@ public class Conf {
aliasTrue.add("+");
}
//----------------------------------------------//
// -------------------------------------------- //
// Persistance
//----------------------------------------------//
// -------------------------------------------- //
public static boolean save() {
return EM.configSave();
Factions.log("Saving config to disk.");
try {
DiscUtil.write(file, Factions.gson.toJson(new Conf()));
} catch (IOException e) {
e.printStackTrace();
Factions.log("Failed to save the config to disk.");
return false;
}
return true;
}
public static boolean load() {
if ( ! file.exists()) {
Factions.log("No conf to load from disk. Creating new file.");
save();
return true;
}
try {
Factions.gson.fromJson(DiscUtil.read(file), Conf.class);
} catch (IOException e) {
e.printStackTrace();
Factions.log("Failed to load the config from disk.");
return false;
}
return true;
}
}

View File

@ -1,9 +1,10 @@
package com.bukkit.mcteam.factions.entities;
package com.bukkit.mcteam.factions;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
public class Coord {
protected static transient int cellSize = 16;
public int x, z;
@ -35,7 +36,7 @@ public class Coord {
return from(player.getLocation());
}
public static Coord from(Follower follower) {
public static Coord from(FPlayer follower) {
return from(follower.getPlayer());
}

View File

@ -1,16 +1,23 @@
package com.bukkit.mcteam.factions.entities;
package com.bukkit.mcteam.factions;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
import java.util.Map.Entry;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import com.bukkit.mcteam.factions.Factions;
import com.bukkit.mcteam.factions.struct.*;
import com.bukkit.mcteam.util.ChatFixUtil;
import com.bukkit.mcteam.factions.entities.EM;
import com.bukkit.mcteam.gson.reflect.TypeToken;
import com.bukkit.mcteam.util.DiscUtil;
public class Follower {
public transient String id; // The is the name of the player
public class FPlayer {
public static transient Map<String, FPlayer> instances = new HashMap<String, 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 int factionId;
@ -21,6 +28,43 @@ public class Follower {
private boolean mapAutoUpdating;
private boolean factionChatting;
public FPlayer(Player player) {
this.playername = player.getName();
}
public FPlayer(String playername) {
this.playername = playername;
}
// GSON need this noarg constructor.
public FPlayer() {
}
public Player getPlayer() {
return Factions.instance.getServer().getPlayer(playername);
}
public String getPlayerName() {
return this.playername;
}
// -------------------------------------------- //
// Online / Offline State Checking
// -------------------------------------------- //
public boolean isOnline() {
return Factions.instance.getServer().getPlayer(playername) != null;
}
public boolean isOffline() {
return ! isOnline();
}
public boolean isFactionChatting() {
if (this.factionId == 0) {
return false;
@ -32,7 +76,7 @@ public class Follower {
this.factionChatting = factionChatting;
}
public Follower() {
public FPlayer() {
this.resetFactionData();
this.power = this.getPowerMax();
this.lastPowerUpdateTime = System.currentTimeMillis();
@ -46,14 +90,6 @@ public class Follower {
this.title = "";
}
public Player getPlayer() {
return Factions.factions.getServer().getPlayer(this.getName());
}
public boolean isOnline() {
return this.getPlayer() != null;
}
public boolean isMapAutoUpdating() {
return mapAutoUpdating;
}
@ -113,14 +149,14 @@ public class Follower {
public String getNameAndTitle(Faction faction) {
return this.getRelationColor(faction)+this.getNameAndTitle();
}
public String getNameAndTitle(Follower follower) {
public String getNameAndTitle(FPlayer follower) {
return this.getRelationColor(follower)+this.getNameAndTitle();
}
public String getNameAndTag(Faction faction) {
return this.getRelationColor(faction)+this.getNameAndTag();
}
public String getNameAndTag(Follower follower) {
public String getNameAndTag(FPlayer follower) {
return this.getRelationColor(follower)+this.getNameAndTag();
}
@ -136,7 +172,7 @@ public class Follower {
// For non members we show tag
return rel.getColor() + this.getNameAndTag();
}
public String getNameAndRelevant(Follower follower) {
public String getNameAndRelevant(FPlayer follower) {
return getNameAndRelevant(follower.getFaction());
}
@ -159,7 +195,7 @@ public class Follower {
return this.getRelation(faction).getColor()+getChatTag();
}
public String getChatTag(Follower follower) {
public String getChatTag(FPlayer follower) {
if (this.withoutFaction()) {
return "";
}
@ -175,7 +211,7 @@ public class Follower {
return faction.getRelation(this);
}
public Relation getRelation(Follower follower) {
public Relation getRelation(FPlayer follower) {
return this.getFaction().getRelation(follower);
}
@ -183,7 +219,7 @@ public class Follower {
return faction.getRelationColor(this);
}
public ChatColor getRelationColor(Follower follower) {
public ChatColor getRelationColor(FPlayer follower) {
return this.getRelation(follower).getColor();
}
@ -343,7 +379,7 @@ public class Follower {
return errors;
}
public ArrayList<String> invite(Follower follower) {
public ArrayList<String> invite(FPlayer follower) {
ArrayList<String> errors = new ArrayList<String>();
//Log.debug("this.role: "+this.role);
@ -361,7 +397,7 @@ public class Follower {
return this.getFaction().invite(follower);
}
public ArrayList<String> deinvite(Follower follower) {
public ArrayList<String> deinvite(FPlayer follower) {
ArrayList<String> errors = new ArrayList<String>();
if (this.role.value < Role.MODERATOR.value) {
@ -375,7 +411,7 @@ public class Follower {
return this.getFaction().deinvite(follower);
}
public ArrayList<String> kick(Follower follower) {
public ArrayList<String> kick(FPlayer follower) {
ArrayList<String> errors = new ArrayList<String>();
if ( ! follower.getFaction().equals(this.getFaction())) {
@ -403,31 +439,11 @@ public class Follower {
//this.getPlayer().sendMessage(ChatColor.GREEN + "This is a faction server! Type "+Conf.colorCommand+"/f"+ChatColor.GREEN +" for more info :D");
}
//----------------------------------------------//
// Messages - Directly connected to ChatFixUtil
//----------------------------------------------//
public void sendMessage(String message, boolean fix) {
Player player = this.getPlayer();
ChatFixUtil.sendMessage(player, message, fix);
}
public void sendMessage(List<String> messages, boolean fix) {
Player player = this.getPlayer();
ChatFixUtil.sendMessage(player, messages, fix);
}
public void sendMessage(String message) {
Player player = this.getPlayer();
ChatFixUtil.sendMessage(player, message, true);
}
public void sendMessage(List<String> messages) {
Player player = this.getPlayer();
ChatFixUtil.sendMessage(player, messages, true);
}
//----------------------------------------------//
// Search
//----------------------------------------------//
public static Follower find(String name) {
for (Follower follower : EM.followerGetAll()) {
public static FPlayer find(String name) { // TODO felaktig!
for (FPlayer follower : EM.followerGetAll()) {
if (follower.getName().equalsIgnoreCase(name.trim())) {
return follower;
}
@ -436,19 +452,110 @@ public class Follower {
return null;
}
// -------------------------------------------- //
// Get
// You can only get a "skin" for online players.
// The same object is always returned for the same player.
// This means you can use the == operator. No .equals method necessary.
// -------------------------------------------- //
public static FPlayer get(String playername) {
if (instances.containsKey(playername)) {
return instances.get(playername);
}
FPlayer vplayer = new FPlayer(playername);
instances.put(playername, vplayer);
return vplayer;
}
// You should use this one to be sure you do not spell the player name wrong.
public static FPlayer get(Player player) {
return get(player.getName());
}
// -------------------------------------------- //
// Messages
// -------------------------------------------- //
public void sendMessage(String message) {
this.getPlayer().sendMessage(Conf.colorSystem + message);
}
public void sendMessage(List<String> messages) {
for(String message : messages) {
this.sendMessage(message);
}
}
//----------------------------------------------//
// Persistance and entity management
//----------------------------------------------//
/*
public boolean save() {
return EM.followerSave(this.id);
}
public static Follower get(Player player) {
public static FPlayer get(Player player) {
return EM.followerGet(player);
}
public static Collection<Follower> getAll() {
public static Collection<FPlayer> getAll() {
return EM.followerGetAll();
}
*/
// -------------------------------------------- //
// Persistance
// -------------------------------------------- //
public boolean shouldBeSaved() {
return this.factionId != 0;
}
public static boolean save() {
Factions.log("Saving players to disk");
// We only wan't to save the vplayers with non default values
Map<String, FPlayer> vplayersToSave = new HashMap<String, FPlayer>();
for (Entry<String, FPlayer> entry : instances.entrySet()) {
if (entry.getValue().shouldBeSaved()) {
vplayersToSave.put(entry.getKey(), entry.getValue());
}
}
try {
DiscUtil.write(file, Factions.gson.toJson(vplayersToSave));
} catch (IOException e) {
Factions.log("Failed to save the players to disk.");
e.printStackTrace();
return false;
}
return true;
}
public static boolean load() {
if ( ! file.exists()) {
Factions.log("No players to load from disk. Creating new file.");
save();
return true;
}
try {
Type type = new TypeToken<Map<String, FPlayer>>(){}.getType();
instances = Factions.gson.fromJson(DiscUtil.read(file), type);
} catch (IOException e) {
e.printStackTrace();
return false;
}
fillPlayernames();
return true;
}
public static void fillPlayernames() {
for(Entry<String, FPlayer> entry : instances.entrySet()) {
entry.getValue().playername = entry.getKey();
}
}
}

View File

@ -1,17 +1,26 @@
package com.bukkit.mcteam.factions.entities;
package com.bukkit.mcteam.factions;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
import java.util.Map.Entry;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import com.bukkit.mcteam.factions.Factions;
import com.bukkit.mcteam.factions.entities.EM;
import com.bukkit.mcteam.factions.struct.Relation;
import com.bukkit.mcteam.factions.struct.Role;
import com.bukkit.mcteam.factions.util.*;
import com.bukkit.mcteam.util.ChatFixUtil;
import com.bukkit.mcteam.gson.reflect.TypeToken;
import com.bukkit.mcteam.util.DiscUtil;
public class Faction {
public static transient Map<Integer, Faction> instances = new HashMap<Integer, Faction>();
public static transient File file = new File(Factions.instance.getDataFolder(), "factions.json");
public static transient int nextId;
public transient int id;
protected Map<Integer, Relation> relationWish;
@ -40,7 +49,7 @@ public class Faction {
public String getTag(Faction otherFaction) {
return this.getTag(otherFaction.getRelationColor(this).toString());
}
public String getTag(Follower otherFollower) {
public String getTag(FPlayer otherFollower) {
return this.getTag(otherFollower.getRelationColor(this).toString());
}
public void setTag(String str) {
@ -74,7 +83,7 @@ public class Faction {
//----------------------------------------------//
public double getPower() {
double ret = 0;
for (Follower follower : this.getFollowersAll()) {
for (FPlayer follower : this.getFollowersAll()) {
ret += follower.getPower();
}
return ret;
@ -82,7 +91,7 @@ public class Faction {
public double getPowerMax() {
double ret = 0;
for (Follower follower : this.getFollowersAll()) {
for (FPlayer follower : this.getFollowersAll()) {
ret += follower.getPowerMax();
}
return ret;
@ -109,7 +118,7 @@ public class Faction {
// -------------------------------
public ArrayList<String> invite(Follower follower) { // TODO Move out
public ArrayList<String> invite(FPlayer follower) { // TODO Move out
ArrayList<String> errors = new ArrayList<String>();
if (follower.getFaction().equals(this)) { // error här?
@ -125,7 +134,7 @@ public class Faction {
return errors;
}
public ArrayList<String> deinvite(Follower follower) { // TODO move out!
public ArrayList<String> deinvite(FPlayer follower) { // TODO move out!
ArrayList<String> errors = new ArrayList<String>();
if (follower.getFaction() == this) {
@ -142,14 +151,14 @@ public class Faction {
return errors;
}
public ArrayList<String> kick(Follower follower) {
public ArrayList<String> kick(FPlayer follower) {
ArrayList<String> errors = new ArrayList<String>();
removeFollower(follower);
return errors;
}
public boolean isInvited(Follower follower) {
public boolean isInvited(FPlayer follower) {
return invites.contains(follower.id);
}
@ -157,9 +166,9 @@ public class Faction {
// Followers
// -------------------------------
public ArrayList<Follower> getFollowersAll() {
ArrayList<Follower> ret = new ArrayList<Follower>();
for (Follower follower : Follower.getAll()) {
public ArrayList<FPlayer> getFollowersAll() {
ArrayList<FPlayer> ret = new ArrayList<FPlayer>();
for (FPlayer follower : FPlayer.getAll()) {
if (follower.factionId == this.id) {
ret.add(follower);
}
@ -167,9 +176,9 @@ public class Faction {
return ret;
}
public ArrayList<Follower> getFollowersWhereOnline(boolean online) {
ArrayList<Follower> ret = new ArrayList<Follower>();
for (Follower follower : Follower.getAll()) {
public ArrayList<FPlayer> getFollowersWhereOnline(boolean online) {
ArrayList<FPlayer> ret = new ArrayList<FPlayer>();
for (FPlayer follower : FPlayer.getAll()) {
if (follower.factionId == this.id && follower.isOnline() == online) {
ret.add(follower);
}
@ -177,10 +186,10 @@ public class Faction {
return ret;
}
public ArrayList<Follower> getFollowersWhereRole(Role role) {
ArrayList<Follower> ret = new ArrayList<Follower>();
public ArrayList<FPlayer> getFollowersWhereRole(Role role) {
ArrayList<FPlayer> ret = new ArrayList<FPlayer>();
for (Follower follower : Follower.getAll()) {
for (FPlayer follower : FPlayer.getAll()) {
if (follower.factionId == this.id && follower.role.equals(role)) {
ret.add(follower);
}
@ -189,7 +198,7 @@ public class Faction {
return ret;
}
public void removeFollower(Follower follower) {
public void removeFollower(FPlayer follower) {
if (this.id != follower.factionId) {
return; // safety check
}
@ -202,8 +211,8 @@ public class Faction {
public ArrayList<Player> getOnlinePlayers() {
ArrayList<Player> ret = new ArrayList<Player>();
for (Player player: Factions.factions.getServer().getOnlinePlayers()) {
Follower follower = Follower.get(player);
for (Player player: Factions.instance.getServer().getOnlinePlayers()) {
FPlayer follower = FPlayer.get(player);
if (follower.factionId == this.id) {
ret.add(player);
}
@ -302,7 +311,7 @@ public class Faction {
return this.getRelationWish(otherFaction);
}
public Relation getRelation(Follower follower) {
public Relation getRelation(FPlayer follower) {
return getRelation(follower.getFaction());
}
@ -310,7 +319,7 @@ public class Faction {
return this.getRelation(otherFaction).getColor();
}
public ChatColor getRelationColor(Follower follower) {
public ChatColor getRelationColor(FPlayer follower) {
return this.getRelation(follower).getColor();
}
@ -318,6 +327,109 @@ public class Faction {
// Persistance and entity management
//----------------------------------------------//
public static boolean save() {
Factions.log("Saving factions to disk");
try {
DiscUtil.write(file, Factions.gson.toJson(instances));
} catch (IOException e) {
Factions.log("Failed to save the factions to disk.");
e.printStackTrace();
return false;
}
return true;
}
public static boolean load() {
if ( ! file.exists()) {
Factions.log("No factions to load from disk. Creating new file.");
save();
return true;
}
try {
Type type = new TypeToken<Map<String, Faction>>(){}.getType();
instances = Factions.gson.fromJson(DiscUtil.read(file), type);
} catch (IOException e) {
e.printStackTrace();
return false;
}
fillIds();
// Make sure the default neutral faction exists
if ( ! instances.containsKey(0)) {
Faction faction = new Faction();
faction.tag = "*No faction*";
faction.description = "\"The faction for the factionless :P\"";
faction.id = 0;
instances.put(faction.id, faction);
}
return true;
}
public static void fillIds() {
nextId = 1;
for(Entry<Integer, Faction> entry : instances.entrySet()) {
entry.getValue().id = entry.getKey();
if (nextId < entry.getKey()) {
nextId = entry.getKey();
}
}
nextId += 1; // make it the next id and not the current highest.
}
public static Faction get(Integer factionId) {
if ( ! instances.containsKey(factionId)) {
Factions.log(Level.WARNING, "Non existing factionId "+factionId+" requested! Issuing board cleaning!");
Board.cleanAll();
}
return instances.get(factionId);
}
public static boolean exists(Integer factionId) {
return instances.containsKey(factionId);
}
public static Collection<Faction> getAll() {
return instances.values();
}
//TODO ta parametrar här. All info som behövs ska matas in här och sparar vi i denna method.
public static Faction create() {
Faction faction = new Faction();
faction.id = nextId;
nextId += 1;
instances.put(faction.id, faction);
Factions.log("created new faction "+faction.id);
//faction.save();
return faction;
}
public static boolean delete(Integer id) {
// NOTE that this does not do any security checks.
// Follower might get orphaned foreign id's
// purge from all boards
// Board.purgeFactionFromAllBoards(id);
Board.cleanAll();
// Remove the file
//File file = new File(folderFaction, id+ext);
//file.delete();
// Remove the faction
instances.remove(id);
// TODO REMOVE ALL MEMBERS!
// TODO SAVE files
return true; // TODO
}
/*
public static Faction create() {
return EM.factionCreate();
}
@ -333,5 +445,5 @@ public class Faction {
public boolean save() {
return EM.factionSave(this.id);
}
*/
}

View File

@ -1,37 +1,84 @@
package com.bukkit.mcteam.factions;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Event;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import com.bukkit.mcteam.factions.entities.*;
import com.bukkit.mcteam.factions.commands.FCommand;
import com.bukkit.mcteam.factions.listeners.FactionsBlockListener;
import com.bukkit.mcteam.factions.listeners.FactionsEntityListener;
import com.bukkit.mcteam.factions.listeners.FactionsPlayerListener;
import com.bukkit.mcteam.factions.util.Log;
import com.bukkit.mcteam.gson.Gson;
import com.bukkit.mcteam.gson.GsonBuilder;
import com.bukkit.mcteam.gson.MapAsArrayTypeAdapter;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import me.taylorkelly.help.Help;
public class Factions extends JavaPlugin {
public static Factions factions;
// -------------------------------------------- //
// Fields
// -------------------------------------------- //
public static Factions instance;
public final static Gson gson = new GsonBuilder()
.setPrettyPrinting()
.excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE)
.registerTypeAdapter(Map.class, new MapAsArrayTypeAdapter()) // a "must have" adapter for GSON
.create();
private final FactionsPlayerListener playerListener = new FactionsPlayerListener(this);
private final FactionsEntityListener entityListener = new FactionsEntityListener(this);
private final FactionsBlockListener blockListener = new FactionsBlockListener(this);
public static PermissionHandler Permissions;
public static Help helpPlugin;
@Override
public void onDisable() {
// TODO Auto-generated method stub
// Commands
public List<FCommand> commands = new ArrayList<FCommand>();
public Factions() {
Factions.instance = this;
}
@Override
public void onEnable() {
Factions.factions = this;
// Add the commands
/*commands.add(new VCommandBlood());
commands.add(new VCommandInfect());
commands.add(new VCommandLoad());
commands.add(new VCommandSave());
commands.add(new VCommandTime());
commands.add(new VCommandTurn());
commands.add(new VCommandCure());
commands.add(new VCommandList());
commands.add(new VCommandVersion());*/
Log.info("=== INIT START ===");
setupPermissions();
setupHelp();
log("=== INIT START ===");
long timeInitStart = System.currentTimeMillis();
Log.info("You are running version: "+this.getDescription().getVersion());
EM.loadAll();
FPlayer.load();
Faction.load();
Board.load();
// Register events
PluginManager pm = this.getServer().getPluginManager();
@ -48,8 +95,94 @@ public class Factions extends JavaPlugin {
pm.registerEvent(Event.Type.BLOCK_PLACED, this.blockListener, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.BLOCK_INTERACT, this.blockListener, Event.Priority.Normal, this);
Log.info("=== INIT DONE (Took "+(System.currentTimeMillis()-timeInitStart)+"ms) ===");
Log.threshold = Conf.logThreshold;
log("=== INIT DONE (Took "+(System.currentTimeMillis()-timeInitStart)+"ms) ===");
}
@Override
public void onDisable() {
// TODO Auto-generated method stub
}
// -------------------------------------------- //
// Integration with other plugins
// -------------------------------------------- //
private void setupPermissions() {
Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
if (Permissions != null) {
return;
}
if (test != null) {
Permissions = ((Permissions)test).getHandler();
Factions.log("Found and will use plugin "+((Permissions)test).getDescription().getFullName());
} else {
Factions.log("Permission system not detected, defaulting to OP");
}
}
private void setupHelp() {
Plugin test = this.getServer().getPluginManager().getPlugin("Help");
if (helpPlugin != null) {
return;
}
if (test != null) {
helpPlugin = ((Help) test);
Factions.log("Found and will use plugin "+helpPlugin.getDescription().getFullName());
for(FCommand fcommand : commands) {
fcommand.helpRegister();
}
helpPlugin.registerCommand("help vampire", "help for the vampire plugin.", helpPlugin, true);
} else {
Factions.log(Level.WARNING, "'Help' plugin isn't detected. No /help support.");
}
}
// -------------------------------------------- //
// Commands
// -------------------------------------------- //
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
List<String> parameters = new ArrayList<String>(Arrays.asList(args));
this.handleCommand(sender, parameters);
return true;
}
public void handleCommand(CommandSender sender, List<String> parameters) {
if (parameters.size() == 0) {
this.commands.get(0).execute(sender, parameters);
return;
}
String commandName = parameters.get(0).toLowerCase();
parameters.remove(0);
for (FCommand fcommand : this.commands) {
if (fcommand.getAliases().contains(commandName)) {
fcommand.execute(sender, parameters);
return;
}
}
sender.sendMessage(Conf.colorSystem+"Unknown faction command \""+commandName+"\". Try /help faction"); // TODO test help messages exists....
//TODO should we use internal help system instead?
}
// -------------------------------------------- //
// Logging
// -------------------------------------------- //
public static void log(String msg) {
log(Level.INFO, msg);
}
public static void log(Level level, String msg) {
Logger.getLogger("Minecraft").log(level, "["+instance.getDescription().getFullName()+"] "+msg);
}
}

View File

@ -0,0 +1,142 @@
package com.bukkit.mcteam.factions.commands;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.bukkit.mcteam.factions.Conf;
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Factions;
public class FCommand {
public List<String> requiredParameters;
public List<String> optionalParameters;
public String permissions;
public String helpNameAndParams;
public String helpDescription;
public CommandSender sender;
public boolean senderMustBePlayer;
public Player player;
public FPlayer fplayer;
public List<String> parameters;
public FCommand() {
requiredParameters = new ArrayList<String>();
optionalParameters = new ArrayList<String>();
permissions = "";
senderMustBePlayer = false;
helpNameAndParams = "fail!";
helpDescription = "no description";
}
public ArrayList<String> getAliases() {
String name = this.getClass().getName().toLowerCase();
if (name.lastIndexOf('.') > 0) {
name = name.substring(name.lastIndexOf('.')+1);
}
name = name.substring(8);
ArrayList<String> aliases = new ArrayList<String>();
aliases.add(name);
return aliases;
}
public String getBaseName() {
// TODO fetch from the plugin.yaml or something...
return "f";
}
public void execute(CommandSender sender, List<String> parameters) {
this.sender = sender;
this.parameters = parameters;
if ( ! validateCall()) {
sendMessage("try /help factions");
return;
}
if (this.senderMustBePlayer) {
this.player = (Player)sender;
this.fplayer = FPlayer.get(this.player);
}
perform();
}
public void perform() {
}
public void helpRegister() {
Factions.helpPlugin.registerCommand(this.getBaseName()+ " " +this.helpNameAndParams, this.helpDescription, Factions.instance, false, permissions);
}
public void sendMessage(String message) {
sender.sendMessage(Conf.colorSystem+message);
}
public void sendMessage(List<String> messages) {
for(String message : messages) {
this.sendMessage(message);
}
}
// Test if the number of params is correct.
public boolean validateCall() {
if( ! testPermission(sender)) {
sendMessage("You do not have sufficient permissions to use this command.");
return false;
}
if ( this.senderMustBePlayer && ! (sender instanceof Player)) {
sendMessage("This command can only be used by ingame players.");
return false;
}
if (parameters.size() < requiredParameters.size()) {
int missing = requiredParameters.size() - parameters.size();
sendMessage("Missing parameters. You must enter "+missing+" more.");
return false;
}
if (parameters.size() > requiredParameters.size() + optionalParameters.size()) {
sendMessage("To many parameters.");
return false;
}
return true;
}
public boolean testPermission(CommandSender sender) {
if (sender.isOp()) {
return true;
}
if (this.permissions.length() == 0) {
return true;
}
if ( ! (sender instanceof Player)) {
return false;
}
if (Factions.Permissions == null) {
return false;
}
Player player = (Player)sender;
return Factions.Permissions.has(player, this.permissions);
}
}

View File

@ -0,0 +1,45 @@
package com.bukkit.mcteam.factions.commands;
import java.util.ArrayList;
import org.bukkit.entity.Player;
import com.bukkit.mcteam.factions.Conf;
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Faction;
public class FCommandLeave extends FCommand {
public FCommandLeave() {
requiredParameters = new ArrayList<String>();
optionalParameters = new ArrayList<String>();
permissions = "";
senderMustBePlayer = true;
helpNameAndParams = "leave";
helpDescription = "Leave your faction";
}
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 (faction.getFollowersAll().size() == 0) {
// Remove this faction
for (FPlayer follower : FPlayer.getAll()) {
follower.sendMessage(Conf.colorSystem+"The faction "+faction.getTag(follower)+Conf.colorSystem+" was disbanded.");
}
Faction.delete(faction.id);
}
}
}

View File

@ -7,6 +7,10 @@ import java.util.*;
import org.bukkit.World;
import org.bukkit.entity.Player;
import com.bukkit.mcteam.factions.Board;
import com.bukkit.mcteam.factions.Conf;
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Faction;
import com.bukkit.mcteam.factions.Factions;
import com.bukkit.mcteam.factions.util.*;
import com.bukkit.mcteam.util.DiscUtil;
@ -18,14 +22,14 @@ import com.bukkit.mcteam.gson.*;
* The methods assume that all on disc is loaded into memory.
*/
public class EM {
protected static Map<String, Follower> followers = new HashMap<String, Follower>(); // Where String is a lowercase playername
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.
protected static int nextFactionId;
// hardcoded config
protected final static String ext = ".json";
protected final static File folderBase = Factions.factions.getDataFolder();
protected final static File folderBase = Factions.instance.getDataFolder();
protected final static File folderFaction = new File(folderBase, "faction");
protected final static File folderFollower = new File(folderBase, "follower");
protected final static File folderBoard = new File(folderBase, "board");
@ -218,7 +222,7 @@ public class EM {
String name = jsonFile.getName();
name = name.substring(0, name.length() - ext.length());
try {
Follower follower = gson.fromJson(DiscUtil.read(jsonFile), Follower.class);
FPlayer follower = gson.fromJson(DiscUtil.read(jsonFile), FPlayer.class);
follower.id = name;
followers.put(follower.id, follower);
//Log.debug("loaded follower "+name);
@ -229,7 +233,7 @@ public class EM {
}
}
public static Collection<Follower> followerGetAll() {
public static Collection<FPlayer> followerGetAll() {
return followers.values();
}
@ -237,7 +241,7 @@ public class EM {
* This method returns the follower object for a player
* A new Follower will be created if the player did not have one
*/
public static Follower followerGet(Player player) {
public static FPlayer followerGet(Player player) {
String key = followerKey(player);
if (followers.containsKey(key)) {
@ -272,9 +276,9 @@ public class EM {
return player.getName();
}
protected static Follower followerCreate(Player player) {
protected static FPlayer followerCreate(Player player) {
Log.debug("Creating new follower "+followerKey(player));
Follower follower = new Follower();
FPlayer follower = new FPlayer();
follower.id = followerKey(player);
followers.put(follower.id, follower);
follower.save();

View File

@ -11,6 +11,11 @@ import org.bukkit.event.block.BlockInteractEvent;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPlaceEvent;
import com.bukkit.mcteam.factions.Board;
import com.bukkit.mcteam.factions.Conf;
import com.bukkit.mcteam.factions.Coord;
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Faction;
import com.bukkit.mcteam.factions.Factions;
import com.bukkit.mcteam.factions.entities.*;
import com.bukkit.mcteam.factions.util.*;
@ -35,7 +40,7 @@ public class FactionsBlockListener extends BlockListener {
}
//special cases, check for destruction of: torch, redstone torch (on & off), repeater (on & off), redstonewire, sapling, crops, sugar cane
private static Set<Integer> specialBlocks = new HashSet(Arrays.asList(
private static Set<Integer> specialBlocks = new HashSet<Integer>(Arrays.asList(
new Integer[] {50, 75, 76, 93, 94, 55, 6, 59, 83}
));
@ -62,7 +67,7 @@ public class FactionsBlockListener extends BlockListener {
return true; // This is no faction territory. You may build or break stuff here.
}
Follower me = Follower.get(player);
FPlayer me = FPlayer.get(player);
Faction myFaction = me.getFaction();
// Cancel if we are not in our own territory
@ -105,7 +110,7 @@ public class FactionsBlockListener extends BlockListener {
return true;
}
Follower me = Follower.get(player);
FPlayer me = FPlayer.get(player);
Faction myFaction = me.getFaction();
Coord blockCoord = Coord.from(block.getLocation());
Faction otherFaction = Board.get(player.getWorld()).getFactionAt(blockCoord);

View File

@ -14,11 +14,11 @@ import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityListener;
import com.bukkit.mcteam.factions.Board;
import com.bukkit.mcteam.factions.Conf;
import com.bukkit.mcteam.factions.Coord;
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Factions;
import com.bukkit.mcteam.factions.entities.Board;
import com.bukkit.mcteam.factions.entities.Conf;
import com.bukkit.mcteam.factions.entities.Coord;
import com.bukkit.mcteam.factions.entities.Follower;
import com.bukkit.mcteam.factions.struct.Relation;
public class FactionsEntityListener extends EntityListener {
@ -35,7 +35,7 @@ public class FactionsEntityListener extends EntityListener {
}
Player player = (Player) entity;
Follower follower = Follower.get(player);
FPlayer follower = FPlayer.get(player);
follower.onDeath();
follower.sendMessage(Conf.colorSystem+"Your power is now "+follower.getPowerRounded()+" / "+follower.getPowerMaxRounded());
}
@ -98,8 +98,8 @@ public class FactionsEntityListener extends EntityListener {
return true;
}
Follower defender = Follower.get((Player)damagee);
Follower attacker = Follower.get((Player)damager);
FPlayer defender = FPlayer.get((Player)damagee);
FPlayer attacker = FPlayer.get((Player)damager);
Relation relation = defender.getRelation(attacker);
//Log.debug(attacker.getName() + " attacked " + defender.getName());

View File

@ -11,7 +11,12 @@ import org.bukkit.event.player.PlayerItemEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;
import com.bukkit.mcteam.factions.Board;
import com.bukkit.mcteam.factions.Commands;
import com.bukkit.mcteam.factions.Conf;
import com.bukkit.mcteam.factions.Coord;
import com.bukkit.mcteam.factions.FPlayer;
import com.bukkit.mcteam.factions.Faction;
import com.bukkit.mcteam.factions.Factions;
import com.bukkit.mcteam.factions.entities.*;
import com.bukkit.mcteam.factions.util.TextUtil;
@ -55,7 +60,7 @@ public class FactionsPlayerListener extends PlayerListener{
}
// ... it was not a command. This means that it is a chat message!
Follower me = Follower.get(talkingPlayer);
FPlayer me = FPlayer.get(talkingPlayer);
// Is it a faction chat message?
if (me.isFactionChatting()) {
@ -83,8 +88,8 @@ public class FactionsPlayerListener extends PlayerListener{
// Why? Because the relations will differ.
event.setCancelled(true);
for (Player listeningPlayer : Factions.factions.getServer().getOnlinePlayers()) {
Follower you = Follower.get(listeningPlayer);
for (Player listeningPlayer : Factions.instance.getServer().getOnlinePlayers()) {
FPlayer you = FPlayer.get(listeningPlayer);
String yourFormat = formatStart + me.getChatTag(you) + formatEnd;
listeningPlayer.sendMessage(String.format(yourFormat, talkingPlayer.getDisplayName(), msg));
}
@ -102,7 +107,7 @@ public class FactionsPlayerListener extends PlayerListener{
ArrayList<String> tokens = TextUtil.split(msg.trim());
if (Conf.aliasBase.contains(tokens.get(0))) {
tokens.remove(0);
Follower follower = Follower.get(player);
FPlayer follower = FPlayer.get(player);
Commands.base(follower, tokens);
return true;
}
@ -116,14 +121,14 @@ public class FactionsPlayerListener extends PlayerListener{
@Override
public void onPlayerQuit(PlayerEvent event) {
Follower follower = Follower.get(event.getPlayer());
FPlayer follower = FPlayer.get(event.getPlayer());
Log.debug("Saved follower on player quit: "+follower.getName());
follower.save(); // We save the followers on logout in order to save their non autosaved state like power.
}
@Override
public void onPlayerMove(PlayerMoveEvent event) {
Follower me = Follower.get(event.getPlayer());
FPlayer me = FPlayer.get(event.getPlayer());
// Did we change coord?
Coord coordFrom = me.lastStoodInCoord;
@ -168,7 +173,7 @@ public class FactionsPlayerListener extends PlayerListener{
}
//currently checking placement/use of: redstone, sign, flint&steel, beds (not currently detected by Bukkit), buckets (empty, water, lava), repeater (not currently detected by Bukkit)
private static Set<Integer> badItems = new HashSet(Arrays.asList(
private static Set<Integer> badItems = new HashSet<Integer>(Arrays.asList(
new Integer[] {331, 323, 259, 355, 325, 326, 327, 356}
));
@ -185,7 +190,7 @@ public class FactionsPlayerListener extends PlayerListener{
return true; // This is not faction territory. Use whatever you like here.
}
Follower me = Follower.get(player);
FPlayer me = FPlayer.get(player);
Faction myFaction = me.getFaction();
// Cancel if we are not in our own territory

View File

@ -2,7 +2,7 @@ package com.bukkit.mcteam.factions.struct;
import org.bukkit.ChatColor;
import com.bukkit.mcteam.factions.entities.*;
import com.bukkit.mcteam.factions.Conf;
public enum Relation {
MEMBER(3, "member"),

View File

@ -1,6 +1,6 @@
package com.bukkit.mcteam.factions.struct;
import com.bukkit.mcteam.factions.entities.Conf;
import com.bukkit.mcteam.factions.Conf;
public enum Role {
ADMIN(2, "admin"),

View File

@ -1,39 +0,0 @@
package com.bukkit.mcteam.factions.util;
import java.util.*;
import org.bukkit.entity.Player;
import com.bukkit.mcteam.factions.Factions;
public class Log {
public static String prefix = Factions.factions.getDescription().getName();
public static ArrayList<Player> debuggers = new ArrayList<Player>();
public static int threshold = 10;
public static void log(int level, String prefix, String msg) {
if (threshold <= level) {
msg = Log.prefix+prefix+msg;
System.out.println(msg);
for(Player debugger : debuggers) {
debugger.sendMessage(msg);
}
}
}
public static void debug (String msg) {
log(10, " debug: ", msg);
}
public static void info (String msg) {
log(20, " info: ", msg);
}
public static void warn (String msg) {
log(30, " warn: ", msg);
}
public static void severe (String msg) {
log(40, " severe: ", msg);
}
}

View File

@ -3,7 +3,7 @@ import java.util.*;
import org.bukkit.Material;
import com.bukkit.mcteam.factions.entities.*;
import com.bukkit.mcteam.factions.Conf;
public class TextUtil {
public static String titleize(String str) {

View File

@ -1,230 +0,0 @@
package com.bukkit.mcteam.util;
import java.util.*;
import org.bukkit.entity.Player;
/**
* The purpose of this tool is twofold:
* 1: Avoid client crashes due to bad color formating.
* 2: Make color continue on word wrapping
*
* In minecraft the degree sign is used as a prefix to another char to create a color.
* For example the code for white is "\u00A7f".
* The "\u00A7" is the unicode notation for the degree sign and the "f" means white.
*
* When does minecraft wrap the text? After how many chars?
* Answer:
* Because the font isn't monospace this differs depending on what you write.
* However we can fit 53 "M" without wrapping and the 54th char would then wrap (be at the beginning of the next line instead)
* As there is no broader char than "M" we can know for sure the minimum line length is 53.
* Note that this means the number of DISPLAYED chars per row is 53.
* A degree sign and the char after will NOT count, as they will not be displayed as chars.
*
* Good to know: Numbers have the same font width as an M.
*
* When does the client crash?
* Answer:
* When a row ends with a degree char and optionally another sign after.
* Another way to say the same: When a line ends with either a broken or valid color notation.
* AND
* The client will ALWAYS crash if the sign after the last displayed char in a row is a degree char.
* A goofy way to explatin it:
* For a line with only "M" and numbers, the fiftyfourth "displayed char" musn't be a degree sign.
*
* WARNING:
* Above is a hypothesis I have created based on what my experiments have shown.
* I am fairly sure it is correct but please help me test it further.
*/
public class ChatFixUtil {
public final static char deg = '\u00A7';
public final static int lineLength = 53;
/**
* This method wraps the msg for you at row lengths of 53,
* avoids client crash scenarios and makes the previous color continue on
* the next line.
*
* The upsides with filtering your messages through this method are:
* - No client crashes.
* - Line wrapping with preserved color.
*
* The downsides are:
* - The width of the chat window will not be used to it's fullest.
* For example you can fit more that 53 commas (,) in a chatwindow row
* but the line would break after 53 displayed chars.
*
* Suggested usage:
* NO NEED TO USE the fix method for static help pages in your plugin.
* As the text is static you can make sure there is no client crash yourself
* and be able to use the full line length.
*
* DO USE in cases like where you output colored messages with playernames in your
* plugin. As the player names have different length there is potential for client crash.
*/
public static ArrayList<String> fix(String msg) {
// Make sure the end of msg is good
msg = cleanMsgEnding(msg);
ArrayList<String> ret = new ArrayList<String>();
int displen = 0; // The number of displayed chars in row so far.
String row = "";
String latestColor = null;
for (int i = 0; i < msg.length(); i++) {
if (displen == lineLength) {
// it is time to start on the next row!
ret.add(row);
displen = 0;
row = "";
if (latestColor != null) {
row += deg+latestColor;
}
}
char c = msg.charAt(i);
if (c == deg) {
latestColor = String.valueOf(msg.charAt(i+1));
row += deg+latestColor;
i++;
} else {
displen += 1;
row += c;
}
}
ret.add(row);
return ret;
}
public static ArrayList<String> fix(List<String> messages) {
ArrayList<String> ret = new ArrayList<String>();
for(String message : messages) {
ret.addAll(fix(message));
}
return ret;
}
/**
* Removes the ending chars as long as they are deg or deg+'anychar' or a space
* As I see it we would never want those chars at the end of a msg.
*/
protected static String cleanMsgEnding (String msg) {
while (msg.length() > 0) {
if (msg.endsWith(String.valueOf(deg)) || msg.endsWith(" ")) {
msg = msg.substring(0, msg.length()-1);
} else if (msg.length() >= 2 && msg.charAt(msg.length() - 2) == deg) {
msg = msg.substring(0, msg.length()-2);
} else {
break;
}
}
return msg;
}
/**
* This test util assumes line break after 53 displayed chars.
* The fix method above breaks like that so this method should
* be a valid way to test if a message row would crash a client.
*/
public static String thisMsgWouldCrashClient(String str) {
// There would always be crash if we end with deg or deg+'anychar'
if (str.length() >= 1 && str.charAt(str.length() - 1) == deg) {
return "Crash: The str ends with deg.";
} else if (str.length() >= 2 && str.charAt(str.length() - 2) == deg) {
return "Crash: The str ends with deg+'anychar'.";
}
int displayedChars = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == deg && displayedChars == lineLength) {
return "Crash: Deg as fiftyforth \"displayed\" char";
} else if (c == deg) {
i++; // this and next: they are not displayed... skip them...
} else {
displayedChars += 1;
}
}
return "all ok";
}
//----------------------------------------------//
// Methods for effectively sending messages
//----------------------------------------------//
//----------------------------------------------//
// One player
//----------------------------------------------//
public static void sendMessage(Player player, String message, boolean fix) {
if (fix) {
List<String> messages = ChatFixUtil.fix(message);
sendMessage(player, messages, false);
} else {
if (player != null) {
player.sendMessage(message);
}
}
}
public static void sendMessage(Player player, List<String> messages, boolean fix) {
if (fix) {
messages = ChatFixUtil.fix(messages);
}
for (String message : messages) {
sendMessage(player, message, false);
}
}
public static void sendMessage(Player player, String message) {
sendMessage(player, message, true);
}
public static void sendMessage(Player player, List<String> messages) {
sendMessage(player, messages, true);
}
//----------------------------------------------//
// Many Players
//----------------------------------------------//
public static void sendMessage(Collection<Player> players, String message, boolean fix) {
if (fix) {
List<String> messages = ChatFixUtil.fix(message);
sendMessage(players, messages, false);
} else {
for (Player player : players) {
sendMessage(player, message, false);
}
}
}
public static void sendMessage(Collection<Player> players, List<String> messages, boolean fix) {
if (fix) {
messages = ChatFixUtil.fix(messages);
}
for (String message : messages) {
sendMessage(players, message, false);
}
}
public static void sendMessage(Collection<Player> players, String message) {
sendMessage(players, message, true);
}
public static void sendMessage(Collection<Player> players, List<String> messages) {
sendMessage(players, messages, true);
}
}