Better chat integration with other plugins. In fact the whole chat system has been reprogrammed. Fix ascii compass typo. Fix claim at the border of enemy territory bug. New faction chat. Some refactoring

This commit is contained in:
Olof Larsson
2011-02-12 18:05:05 +01:00
parent d554bc18ec
commit da89ff4ecd
15 changed files with 417 additions and 329 deletions

View File

@@ -38,13 +38,16 @@ public class Board {
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 static boolean isBorderCoord(Coord coord) {
Faction faction = Board.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 != a.getFaction() && faction != b.getFaction() && faction != c.getFaction() && faction != d.getFaction();
return faction != a.getFaction() || faction != b.getFaction() || faction != c.getFaction() || faction != d.getFaction();
}
public static void purgeFaction(Faction faction) {
@@ -84,7 +87,7 @@ public class Board {
*/
public static ArrayList<String> getMap(Faction faction, Coord coord, double inDegrees) {
ArrayList<String> ret = new ArrayList<String>();
ret.add(TextUtil.titleize("("+coord+") "+coord.getFaction().getName(faction)));
ret.add(TextUtil.titleize("("+coord+") "+coord.getFaction().getTag(faction)));
int halfWidth = Conf.mapWidth / 2;
int halfHeight = Conf.mapHeight / 2;

View File

@@ -1,38 +1,47 @@
package com.bukkit.mcteam.factions.entities;
import java.util.*;
import org.bukkit.*;
import com.bukkit.mcteam.factions.struct.Relation;
public class Conf {
public static Integer logThreshold;
public static String prefixAdmin;
public static String prefixMod;
public static int factionNameMinLength;
public static int factionNameMaxLength;
public static int mapHeight;
public static int mapWidth;
public static double territoryShieldFactor;
// Chat control:
public static boolean useRelationColoredChat; // This can interfere with other chat formatting plugins. Test to turn it on or off.
// TODO experiment with displayname feature of bukkit
// TODO test to set format instead of overriding and offer a non colored mut **Title alternative...
// Colors
public static ChatColor colorMember;
public static ChatColor colorAlly;
public static ChatColor colorNeutral;
public static ChatColor colorEnemy;
public static ChatColor colorMember = ChatColor.GREEN;
public static ChatColor colorAlly = ChatColor.LIGHT_PURPLE;
public static ChatColor colorNeutral = ChatColor.WHITE;
public static ChatColor colorEnemy = ChatColor.RED;
public static ChatColor colorSystem;
public static ChatColor colorChrome;
public static ChatColor colorCommand;
public static ChatColor colorParameter;
public static ChatColor colorSystem = ChatColor.YELLOW;
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;
public static double powerPlayerMin = -10;
public static double powerPerMinute = 0.2; // Default health rate... it takes 5 min to heal one power
public static double powerPerDeath = 2; //A death makes you loose 2 power
public static String prefixAdmin = "**";
public static String prefixMod = "*";
public static int factionTagLengthMin = 3;
public static int factionTagLengthMax = 3;
// Configuration on the Faction tag in chat messages.
public static boolean chatTagEnabled = true;
public static boolean chatTagRelationColored = true;
public static int chatTagInsertIndex = 1;
public static String chatTagFormat = "%s"+ChatColor.WHITE+" ";
public static String factionChatFormat = colorMember+"%s"+ChatColor.WHITE+" %s";
public static int mapHeight = 8;
public static int mapWidth = 49;
public static double territoryShieldFactor = 0.5;
public static List<Material> territoryProtectedMaterials = new ArrayList<Material>();
// Command names / aliases
public static List<String> aliasBase = new ArrayList<String>();
@@ -47,7 +56,9 @@ public class Conf {
public static List<String> aliasLeave = new ArrayList<String>();
public static List<String> aliasCreate = new ArrayList<String>();
public static List<String> aliasName = new ArrayList<String>();
public static List<String> aliasTag = new ArrayList<String>();
public static List<String> aliasDescription = new ArrayList<String>();
public static List<String> aliasChat = new ArrayList<String>();
public static List<String> aliasTitle = new ArrayList<String>();
public static List<String> aliasInvite = new ArrayList<String>();
@@ -65,48 +76,17 @@ public class Conf {
public static List<String> aliasRelationNeutral = new ArrayList<String>();
public static List<String> aliasRelationEnemy = new ArrayList<String>();
public static List<String> aliasDescription = new ArrayList<String>();
public static List<String> aliasVersion = new ArrayList<String>();
// Value aliases
public static List<String> aliasTrue = new ArrayList<String>();
// Power
public static double powerPerPlayer;
public static double powerPerMinute; // Default health rate
public static double powerPerDeath;
// Protected blocks
public static List<Material> territoryProtectedMaterials = new ArrayList<Material>();
static {
logThreshold = 10;
prefixAdmin = "**";
prefixMod = "*";
useRelationColoredChat = true;
powerPerPlayer = 10; // One player has 10 power
powerPerMinute = 0.2; // Default health rate... it takes 5 min to heal one power
powerPerDeath = 2; //A death makes you loose 2 power
territoryShieldFactor = 0.5;
territoryProtectedMaterials.add(Material.WOODEN_DOOR);
territoryProtectedMaterials.add(Material.DISPENSER);
territoryProtectedMaterials.add(Material.CHEST);
territoryProtectedMaterials.add(Material.FURNACE);
colorMember = ChatColor.GREEN;
colorAlly = ChatColor.LIGHT_PURPLE;
colorNeutral = ChatColor.WHITE;
colorEnemy = ChatColor.RED;
colorSystem = ChatColor.YELLOW;
colorChrome = ChatColor.GOLD;
colorCommand = ChatColor.AQUA;
colorParameter = ChatColor.DARK_AQUA;
aliasBase.add("/f");
aliasBase.add("f");
aliasBase.add("/faction");
@@ -132,9 +112,11 @@ public class Conf {
aliasCreate.add("create");
aliasCreate.add("new");
aliasTag.add("tag");
aliasDescription.add("desc");
aliasName.add("name");
aliasName.add("rename");
aliasChat.add("chat");
aliasChat.add("c");
aliasTitle.add("title");
@@ -162,8 +144,6 @@ public class Conf {
aliasRelationNeutral.add("neutral");
aliasRelationEnemy.add("enemy");
aliasDescription.add("desc");
aliasVersion.add("version");
aliasTrue.add("true");
@@ -172,28 +152,6 @@ public class Conf {
aliasTrue.add("ok");
aliasTrue.add("on");
aliasTrue.add("+");
factionNameMinLength = 3;
factionNameMaxLength = 40;
mapHeight = 8;
mapWidth = 49;
}
//----------------------------------------------//
// Color picking and stuff
//----------------------------------------------//
public static ChatColor relationColor(Relation relation) {
if (relation == Relation.MEMBER) {
return colorMember;
} else if (relation == Relation.ALLY) {
return colorAlly;
} else if (relation == Relation.NEUTRAL) {
return colorNeutral;
} else { //if (relation == FactionRelation.ENEMY) {
return colorEnemy;
}
}
//----------------------------------------------//

View File

@@ -114,18 +114,6 @@ public class EM {
// Follower methods (loadAll, get, save)
//----------------------------------------------//
/**
* This method will create a follower entity and assign the link to the corresponding player.
*/
public static void onPlayerLogin(Player player) {
Follower follower = followerGet(player);
follower.player = player;
}
public static void onPlayerLogout(Player player) {
followers.get(player.getName()).player = null;
}
/**
* This method loads all followers from disc into memory.
*/
@@ -256,7 +244,7 @@ public class EM {
// Make sure the default neutral faction exists
if ( ! factions.containsKey(0)) {
Faction faction = new Faction();
faction.name = "*No faction*";
faction.tag = "*No faction*";
faction.description = "\"The faction for the factionless :P\"";
faction.id = 0;
factions.put(faction.id, faction);
@@ -271,7 +259,7 @@ public class EM {
return factions.values();
}
public static Faction factionCreate(){
public static Faction factionCreate() {
Faction faction = new Faction();
faction.id = nextFactionId;
nextFactionId += 1;

View File

@@ -9,6 +9,7 @@ import com.bukkit.mcteam.factions.Factions;
import com.bukkit.mcteam.factions.struct.Relation;
import com.bukkit.mcteam.factions.struct.Role;
import com.bukkit.mcteam.factions.util.Log;
import com.bukkit.mcteam.factions.util.TextUtil;
import com.bukkit.mcteam.util.ChatFixUtil;
public class Faction {
@@ -17,36 +18,34 @@ public class Faction {
protected Map<Integer, Relation> relationWish;
protected Set<String> invites; // Where string is a follower id (lower case name)
protected boolean open;
protected String name;
protected String tag;
protected String description;
public Faction() {
this.relationWish = new HashMap<Integer, Relation>();
this.invites = new HashSet<String>();
this.open = true;
this.name = "Untitled Faction :(";
this.tag = "???";
this.description = "Default faction description :(";
}
// -------------------------------
// Information
// -------------------------------
public String getName() {
return this.getName("");
public String getTag() {
return this.getTag("");
}
public String getName(String prefix) {
return prefix+this.name;
public String getTag(String prefix) {
return prefix+this.tag;
}
public String getName(Faction otherFaction) {
return this.getName(otherFaction.getRelationColor(this).toString());
public String getTag(Faction otherFaction) {
return this.getTag(otherFaction.getRelationColor(this).toString());
}
public String getName(Follower otherFollower) {
return this.getName(otherFollower.getRelationColor(this).toString());
public String getTag(Follower otherFollower) {
return this.getTag(otherFollower.getRelationColor(this).toString());
}
public void setName(String newName) {
this.name = newName;
public void setTag(String str) {
this.tag = str.toUpperCase();
this.save();
}
@@ -99,16 +98,8 @@ public class Faction {
return Board.getFactionCoordCount(this);
}
public double getLandMax() {
return this.getPower();
}
public int getLandMaxRounded() {
return (int) Math.round(this.getLandMax());
}
public boolean hasLandInflation() {
return Board.getFactionCoordCount(this) > this.getLandMaxRounded();
return this.getLandRounded() > this.getPowerRounded();
}
// -------------------------------
@@ -116,14 +107,14 @@ public class Faction {
// -------------------------------
public ArrayList<String> invite(Follower follower) {
public ArrayList<String> invite(Follower follower) { // TODO Move out
ArrayList<String> errors = new ArrayList<String>();
Log.debug("follower.getFaction().id"+follower.getFaction().id);
Log.debug("this.id"+this.id);
if (follower.getFaction().equals(this)) { // error här?
errors.add(Conf.colorSystem+follower.getFullName()+" is already a member of "+this.getName());
errors.add(Conf.colorSystem+follower.getName()+" is already a member of "+this.getTag());
}
if(errors.size() > 0) {
@@ -135,11 +126,11 @@ public class Faction {
return errors;
}
public ArrayList<String> deinvite(Follower follower) {
public ArrayList<String> deinvite(Follower follower) { // TODO move out!
ArrayList<String> errors = new ArrayList<String>();
if (follower.getFaction() == this) {
errors.add(Conf.colorSystem+follower.getFullName()+" is already a member of "+this.getName());
errors.add(Conf.colorSystem+follower.getName()+" is already a member of "+this.getTag());
errors.add(Conf.colorSystem+"You might want to "+Conf.colorCommand+Conf.aliasBase.get(0)+" "+Conf.aliasKick.get(0)+Conf.colorParameter+" "+follower.getName());
}
@@ -222,58 +213,45 @@ public class Faction {
}
//----------------------------------------------//
// Faction name
// Faction tag
//----------------------------------------------//
private transient static ArrayList<String> nameWhitelist = new ArrayList<String>(Arrays.asList(new String []{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z"
}));
public static String toComparisonName(String name) {
String ret = "";
for (char c : name.toCharArray()) {
if (nameWhitelist.contains(String.valueOf(c))) {
ret += c;
}
}
return ret.toLowerCase();
public String getComparisonTag() {
return TextUtil.getComparisonString(this.tag);
}
public static ArrayList<String> validateName(String name) {
public static ArrayList<String> validateTag(String str) {
ArrayList<String> errors = new ArrayList<String>();
if(Faction.toComparisonName(name).length() < Conf.factionNameMinLength) {
errors.add(Conf.colorSystem+"That name is to short");
if(TextUtil.getComparisonString(str).length() < Conf.factionTagLengthMin) {
errors.add(Conf.colorSystem+"The faction tag can't be shorter than "+Conf.factionTagLengthMin+ " chars.");
}
if(name.length() > Conf.factionNameMaxLength) {
errors.add(Conf.colorSystem+"That name is to long");
if(str.length() > Conf.factionTagLengthMax) {
errors.add(Conf.colorSystem+"The faction tag can't be longer than "+Conf.factionTagLengthMax+ " chars.");
}
for (char c : str.toCharArray()) {
if ( ! TextUtil.substanceChars.contains(String.valueOf(c))) {
errors.add(Conf.colorSystem+"Faction tag must be alphanumeric. \""+c+"\" is not allowed.");
}
}
return errors;
}
public String getComparisonName() {
return Faction.toComparisonName(this.name);
}
public static Faction find(String name) {
String compName = Faction.toComparisonName(name);
public static Faction findByTag(String str) {
String compStr = TextUtil.getComparisonString(str);
for (Faction faction : Faction.getAll()) {
if (faction.getComparisonName().equals(compName)) {
if (faction.getComparisonTag().equals(compStr)) {
return faction;
}
}
return null;
}
public static boolean isNameTaken(String name) {
return Faction.find(name) != null;
public static boolean isTagTaken(String str) {
return Faction.findByTag(str) != null;
}
//----------------------------------------------//
@@ -306,9 +284,10 @@ public class Faction {
public void setRelationWish(Faction otherFaction, Relation relation) {
if (this.relationWish.containsKey(otherFaction.id) && relation.equals(Relation.NEUTRAL)){
this.relationWish.remove(otherFaction.id);
return;
} else {
this.relationWish.put(otherFaction.id, relation);
}
this.relationWish.put(otherFaction.id, relation);
this.save();
}
public Relation getRelation(Faction otherFaction) {

View File

@@ -5,13 +5,13 @@ import java.util.*;
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.factions.util.Log;
import com.bukkit.mcteam.util.ChatFixUtil;
public class Follower {
public transient String id; // The is the name of the player
public transient Player player; // The is the name of the player
public int factionId;
public Role role;
@@ -34,7 +34,7 @@ public class Follower {
}
public Player getPlayer() {
return this.player;
return Factions.server.getPlayer(this.getName());
}
public boolean isOnline() {
@@ -48,7 +48,13 @@ public class Follower {
public void setMapAutoUpdating(boolean mapAutoUpdating) {
this.mapAutoUpdating = mapAutoUpdating;
}
//----------------------------------------------//
// Title, Name, Faction Tag and Chat
//----------------------------------------------//
// Base:
public String getTitle() {
return title;
}
@@ -58,6 +64,117 @@ public class Follower {
this.save();
}
public String getName() {
return this.id;
}
public String getTag() {
if (this.withoutFaction()) {
return "";
}
return this.getFaction().getTag();
}
// Base concatenations:
public String getNameAndSomething(String something) {
String ret = this.role.getPrefix();
if (something.length() > 0) {
ret += something+" ";
}
ret += this.getName();
return ret;
}
public String getNameAndTitle() {
return this.getNameAndSomething(this.getTitle());
}
public String getNameAndTag() {
return this.getNameAndSomething(this.getTag());
}
// Colored concatenations:
// These are used in information messages
public String getNameAndTitle(Faction faction) {
return this.getRelationColor(faction)+this.getNameAndTitle();
}
public String getNameAndTitle(Follower follower) {
return this.getRelationColor(follower)+this.getNameAndTitle();
}
public String getNameAndTag(Faction faction) {
return this.getRelationColor(faction)+this.getNameAndTag();
}
public String getNameAndTag(Follower follower) {
return this.getRelationColor(follower)+this.getNameAndTag();
}
public String getNameAndRelevant(Faction faction) {
// Which relation?
Relation rel = this.getRelation(faction);
// For member we show title
if (rel == Relation.MEMBER) {
return rel.getColor() + this.getNameAndTitle();
}
// For non members we show tag
return rel.getColor() + this.getNameAndTag();
}
public String getNameAndRelevant(Follower follower) {
return getNameAndRelevant(follower.getFaction());
}
// Chat Tag:
// These are injected into the format of global chat messages.
public String getChatTag() {
if (this.withoutFaction()) {
return "";
}
return String.format(Conf.chatTagFormat, this.role.getPrefix()+this.getTag());
}
// Colored Chat Tag
public String getChatTag(Faction faction) {
if (this.withoutFaction()) {
return "";
}
return this.getRelation(faction).getColor()+getChatTag();
}
public String getChatTag(Follower follower) {
if (this.withoutFaction()) {
return "";
}
return this.getRelation(follower).getColor()+getChatTag();
}
// -------------------------------
// Relation and relation colors
// -------------------------------
public Relation getRelation(Faction faction) {
return faction.getRelation(this);
}
public Relation getRelation(Follower follower) {
return this.getFaction().getRelation(follower);
}
public ChatColor getRelationColor(Faction faction) {
return faction.getRelationColor(this);
}
public ChatColor getRelationColor(Follower follower) {
return this.getRelation(follower).getColor();
}
//----------------------------------------------//
// Health
//----------------------------------------------//
@@ -85,15 +202,15 @@ public class Follower {
} else if (this.power < this.getPowerMin()) {
this.power = this.getPowerMin();
}
Log.debug("Power of "+this.getFullName()+" is now: "+this.power);
Log.debug("Power of "+this.getName()+" is now: "+this.power);
}
public double getPowerMax() {
return Conf.powerPerPlayer;
return Conf.powerPlayerMax;
}
public double getPowerMin() {
return -Conf.powerPerPlayer;
return Conf.powerPlayerMin;
}
public int getPowerRounded() {
@@ -141,7 +258,10 @@ public class Follower {
public void sendFactionHereMessage() {
Faction factionHere = Board.getFactionAt(this.getCoord());
String msg = Conf.colorSystem+" ~ "+factionHere.getName(this);
String msg = Conf.colorSystem+" ~ "+factionHere.getTag(this);
if (factionHere.id != 0) {
msg += " - "+factionHere.getDescription();
}
this.sendMessage(msg);
}
@@ -152,17 +272,24 @@ public class Follower {
return EM.factionGet(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.getName());
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.factionId != 0) {
if (this.hasFaction()) {
errors.add(Conf.colorSystem+"You must leave your current faction first.");
}
@@ -189,7 +316,7 @@ public class Follower {
errors.add(Conf.colorSystem+"You must give the admin role to someone else first.");
}
if(this.factionId == 0) {
if(this.withoutFaction()) {
errors.add(Conf.colorSystem+"You are not member of any faction.");
}
@@ -239,7 +366,7 @@ public class Follower {
ArrayList<String> errors = new ArrayList<String>();
if ( ! follower.getFaction().equals(this.getFaction())) {
errors.add(this.getRelationColor(follower)+follower.getFullName()+Conf.colorSystem+" is not a member of "+Conf.colorMember+this.getFaction().getName());
errors.add(follower.getNameAndRelevant(this)+Conf.colorSystem+" is not a member of "+Conf.colorMember+this.getFaction().getTag());
} else if (follower.equals(this)) {
errors.add(Conf.colorSystem+"You can not kick yourself.");
errors.add(Conf.colorSystem+"You might want to "+Conf.colorCommand+Conf.aliasBase.get(0)+" "+Conf.aliasLeave.get(0));
@@ -296,65 +423,6 @@ public class Follower {
return null;
}
// -------------------------------
// Relation and relation colors
// -------------------------------
public Relation getRelation(Faction faction) {
return faction.getRelation(this);
}
public Relation getRelation(Follower follower) {
return this.getFaction().getRelation(follower);
}
public ChatColor getRelationColor(Faction faction) {
return faction.getRelationColor(this);
}
public ChatColor getRelationColor(Follower follower) {
return this.getRelation(follower).getColor();
}
//----------------------------------------------//
// Display the name of this follower
//----------------------------------------------//
public String getName() {
return this.id;
}
public String getFullName() {
return getFullName("");
}
public String getFullName(Faction otherFaction) {
return getFullName(otherFaction.getRelationColor(this).toString());
}
public String getFullName(Follower otherFollower) {
return getFullName(otherFollower.getRelationColor(this).toString());
}
public String getFullName(String prefix) {
String ret = prefix;
if (this.role.equals(Role.ADMIN)) {
ret += Conf.prefixAdmin;
} else if (this.role.equals(Role.MODERATOR)) {
ret += Conf.prefixMod;
}
if (this.title.length() > 0) {
ret += this.title + " ";
}
ret += this.getName();
return ret;
}
//----------------------------------------------//
// Persistance and entity management
//----------------------------------------------//