what?
This commit is contained in:
704
src/com/bukkit/mcteam/factions/Commands.java
Normal file
704
src/com/bukkit/mcteam/factions/Commands.java
Normal file
@ -0,0 +1,704 @@
|
||||
package com.bukkit.mcteam.factions;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import com.bukkit.mcteam.factions.entities.*;
|
||||
import com.bukkit.mcteam.factions.struct.*;
|
||||
import com.bukkit.mcteam.factions.util.*;
|
||||
|
||||
public class Commands {
|
||||
public static ArrayList<ArrayList<String>> helpPages;
|
||||
|
||||
//----------------------------------------------//
|
||||
// Build the help pages
|
||||
//----------------------------------------------//
|
||||
|
||||
static {
|
||||
helpPages = new ArrayList<ArrayList<String>>();
|
||||
ArrayList<String> pageLines;
|
||||
|
||||
|
||||
pageLines = new ArrayList<String>();
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasHelp, "[page]", "Display this, or the next help page"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasList, "", "List all factions"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasShow, "*[faction name]", "Show faction information")); // TODO display relations!
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasMap, "*[on|off]", "Show territory map, set optional auto update.")); // TODO COMPASS
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasJoin, "[faction name]", "Join a faction"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasLeave, "", "Leave your faction"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasCreate, "[faction name]", "Create new faction"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasName, "[faction name]", "Rename your faction"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasDescription, "[description]", "Set the description for your faction"));
|
||||
|
||||
helpPages.add(pageLines);
|
||||
pageLines = new ArrayList<String>();
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasOpen, "", "Switch if invitation is required to join"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasTitle, "[player name] *[title]", "Set or remove a players title"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasInvite, "[player name]", "Invite player"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasDeinvite, "[player name]", "Remove a pending invitation"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasKick, "[player name]", "Kick a player from the faction"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasModerator, "[player name]", "Give or revoke moderator rights")); // TODO
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasAdmin, "[player name]", "Hand over your admin rights")); // TODO
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasClaim, "", "Claim the land where you are standing"));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasUnclaim, "", "Unclaim the land where you are standing"));
|
||||
|
||||
helpPages.add(pageLines);
|
||||
pageLines = new ArrayList<String>();
|
||||
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasRelationAlly, "[faction name]", " "));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasRelationNeutral, "[faction name]", " "));
|
||||
pageLines.add(TextUtil.commandHelp(Conf.aliasRelationEnemy, "[faction name]", " "));
|
||||
pageLines.add("");
|
||||
pageLines.add(Conf.colorSystem+"Set which relation your WHISH you had to another faction.");
|
||||
pageLines.add(Conf.colorSystem+"Per default your relation to another faction will be neutral.");
|
||||
pageLines.add("");
|
||||
pageLines.add(Conf.colorSystem+"If BOTH factions whishes \"ally\" you will be allies.");
|
||||
pageLines.add(Conf.colorSystem+"If ONE faction whishes \"enemy\" you will be enemies.");
|
||||
|
||||
helpPages.add(pageLines);
|
||||
pageLines = new ArrayList<String>();
|
||||
|
||||
pageLines.add(Conf.colorSystem+"You can never hurt members or allies.");
|
||||
pageLines.add(Conf.colorSystem+"You can not hurt neutrals in their own territory.");
|
||||
pageLines.add(Conf.colorSystem+"You can always hurt enemies and players without faction.");
|
||||
pageLines.add("");
|
||||
pageLines.add(Conf.colorSystem+"Damage from enemies are reduced in your own territory.");
|
||||
pageLines.add(Conf.colorSystem+"When you die you loose power. It is restored over time.");
|
||||
pageLines.add(Conf.colorSystem+"The power of a faction is the sum of all member power.");
|
||||
pageLines.add(Conf.colorSystem+"The power of a faction determines how much land it can hold.");
|
||||
pageLines.add(Conf.colorSystem+"You can claim land from a faction if it has to low power.");
|
||||
|
||||
helpPages.add(pageLines);
|
||||
pageLines = new ArrayList<String>();
|
||||
|
||||
pageLines.add(Conf.colorSystem+"Only faction members can build and destroy in their own");
|
||||
pageLines.add(Conf.colorSystem+"territory. Usage of the following items is also restricted:");
|
||||
pageLines.add(Conf.colorSystem+"Door, Chest, Furnace and Dispenser.");
|
||||
pageLines.add(" ");
|
||||
pageLines.add(Conf.colorSystem+"Make sure to put pressure plates in front of doors for your");
|
||||
pageLines.add(Conf.colorSystem+"guest visitors. Otherwise they can't get through. You can ");
|
||||
pageLines.add(Conf.colorSystem+"also use this to create member only areas.");
|
||||
pageLines.add(Conf.colorSystem+"As dispensers are protected you can create traps without");
|
||||
pageLines.add(Conf.colorSystem+"worrying about those arrows getting stolen.");
|
||||
helpPages.add(pageLines);
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------//
|
||||
// Some utils
|
||||
//----------------------------------------------//
|
||||
|
||||
public static Follower findFollower(Follower me, String name, boolean defaultsToMe) {
|
||||
if (name.length() == 0 && defaultsToMe) {
|
||||
return me;
|
||||
}
|
||||
|
||||
Follower follower = Follower.find(name);
|
||||
if (follower != null) {
|
||||
return follower;
|
||||
}
|
||||
|
||||
me.sendMessage(Conf.colorSystem+"The player \""+name+"\" could not be found");
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Faction findFaction(Follower me, String name, boolean defaultsToMe) {
|
||||
if (name.length() == 0 && defaultsToMe) {
|
||||
return me.getFaction();
|
||||
}
|
||||
|
||||
// Search player names
|
||||
Follower follower = Follower.find(name);
|
||||
if (follower != null) {
|
||||
return follower.getFaction();
|
||||
}
|
||||
|
||||
// Then faction names
|
||||
Faction faction = Faction.find(name);
|
||||
if (faction != null) {
|
||||
return faction;
|
||||
}
|
||||
|
||||
me.sendMessage(Conf.colorSystem+"No faction or player \""+name+"\" was found");
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean canIAdministerYou(Follower i, Follower you) {
|
||||
if ( ! i.getFaction().equals(you.getFaction())) {
|
||||
i.sendMessage(you.getFullName(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 controll eachother...");
|
||||
} else {
|
||||
i.sendMessage(Conf.colorSystem+"You must be a faction moderator to do that.");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// The base command
|
||||
//----------------------------------------------//
|
||||
|
||||
public static void base(Follower me, ArrayList<String> tokens) {
|
||||
if (tokens.size() == 0) {
|
||||
help(me);
|
||||
return;
|
||||
}
|
||||
|
||||
String command = tokens.get(0).toLowerCase();
|
||||
tokens.remove(0);
|
||||
|
||||
if (Conf.aliasHelp.contains(command)) {
|
||||
int page = 1;
|
||||
if (tokens.size() > 0) {
|
||||
page = Integer.parseInt(tokens.get(0));
|
||||
}
|
||||
help(me, page);
|
||||
} else if (Conf.aliasLeave.contains(command)) {
|
||||
leave(me);
|
||||
} else if (Conf.aliasJoin.contains(command)) {
|
||||
join(me, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasCreate.contains(command)) {
|
||||
create(me, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasName.contains(command)) {
|
||||
name(me, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasList.contains(command)) {
|
||||
list(me);
|
||||
} else if (Conf.aliasShow.contains(command)) {
|
||||
showFaction(me, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasMap.contains(command)) {
|
||||
showMap(me, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasInvite.contains(command)) {
|
||||
invite(me, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasDeinvite.contains(command)) {
|
||||
deinvite(me, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasOpen.contains(command)) {
|
||||
open(me);
|
||||
} else if (Conf.aliasTitle.contains(command)) {
|
||||
title(me, tokens);
|
||||
} else if (Conf.aliasKick.contains(command)) {
|
||||
kick(me, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasModerator.contains(command)) {
|
||||
roleChange(me, Role.MODERATOR, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasAdmin.contains(command)) {
|
||||
roleChange(me, Role.ADMIN, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasClaim.contains(command)) {
|
||||
claim(me);
|
||||
} else if (Conf.aliasUnclaim.contains(command)) {
|
||||
unclaim(me);
|
||||
} else if (Conf.aliasRelationAlly.contains(command)) {
|
||||
relation(me, Relation.ALLY, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasRelationNeutral.contains(command)) {
|
||||
relation(me, Relation.NEUTRAL, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasRelationEnemy.contains(command)) {
|
||||
relation(me, Relation.ENEMY, TextUtil.implode(tokens));
|
||||
} else if (Conf.aliasDescription.contains(command)) {
|
||||
description(me, TextUtil.implode(tokens));
|
||||
} else {
|
||||
//me.sendMessage(Conf.colorSystem+"Unknown faction command"+Conf.colorCommand+" "+command);
|
||||
me.sendMessage(Conf.colorSystem+"Unknown faction command"+Conf.colorCommand+" "+command);
|
||||
//me.getPlayer().sendMessage(TextUtil.repeat(tokens.get(0), Integer.parseInt(tokens.get(1))));
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// The other commands
|
||||
//----------------------------------------------//
|
||||
public static void help(Follower me) {
|
||||
help(me, 1);
|
||||
}
|
||||
|
||||
public static void help(Follower me, Integer page) {
|
||||
me.sendMessage(TextUtil.titleize("Factions Help ("+page+"/"+helpPages.size()+")"), false);
|
||||
page -= 1;
|
||||
if (page < 0 || page >= helpPages.size()) {
|
||||
me.sendMessage(Conf.colorSystem+"That page does not exist");
|
||||
return;
|
||||
}
|
||||
me.sendMessage(helpPages.get(page), false);
|
||||
}
|
||||
|
||||
public static void leave(Follower me) {
|
||||
Faction faction = me.getFaction();
|
||||
|
||||
ArrayList<String> errors = me.leave();
|
||||
me.sendMessage(errors);
|
||||
|
||||
if (errors.size() == 0) {
|
||||
faction.sendMessage(me.getFullName(faction)+Conf.colorAction+" left your faction.");
|
||||
me.sendMessage("You left "+faction.getName(me));
|
||||
}
|
||||
|
||||
if (faction.getFollowersAll().size() == 0) {
|
||||
// Remove this faction
|
||||
for (Follower follower : Follower.getAll()) {
|
||||
follower.sendMessage(Conf.colorAction+"The faction "+faction.getName(follower)+Conf.colorAction+" was disbandoned.");
|
||||
}
|
||||
EM.factionDelete(faction.id);
|
||||
}
|
||||
}
|
||||
|
||||
public static void join(Follower me, String name) {
|
||||
Faction faction = findFaction(me, name, false);
|
||||
if (faction == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayList<String> errors = me.join(faction);
|
||||
me.sendMessage(errors);
|
||||
|
||||
if (errors.size() > 0) {
|
||||
faction.sendMessage(me.getFullName(faction)+Conf.colorSystem+" tried to join your faction.");
|
||||
} else {
|
||||
me.sendMessage(Conf.colorAction+"You successfully joined "+faction.getName(me));
|
||||
faction.sendMessage(me.getFullName(faction)+Conf.colorAction+" joined your faction.");
|
||||
}
|
||||
}
|
||||
|
||||
// TODO MOVE OUT
|
||||
public static void create(Follower me, String name) {
|
||||
ArrayList<String> errors = me.createFaction(name);
|
||||
me.sendMessage(errors);
|
||||
|
||||
if (errors.size() == 0) {
|
||||
me.sendMessage(Conf.colorAction+"Faction created!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void name(Follower me, String name) {
|
||||
ArrayList<String> errors = new ArrayList<String>();
|
||||
|
||||
if (me.factionId == 0) {
|
||||
errors.add(Conf.colorSystem+"You are not part of any faction");
|
||||
} else if (me.role.value < Role.MODERATOR.value) {
|
||||
errors.add(Conf.colorSystem+"You must be moderator to rename your faction");
|
||||
}
|
||||
|
||||
if (Faction.isNameTaken(name) && ! Faction.toComparisonName(name).equals(me.getFaction().getComparisonName())) {
|
||||
errors.add(Conf.colorSystem+"That name is already taken");
|
||||
}
|
||||
|
||||
errors.addAll(Faction.validateName(name));
|
||||
|
||||
if (errors.size() > 0) {
|
||||
me.sendMessage(errors);
|
||||
return;
|
||||
}
|
||||
|
||||
Faction myFaction = me.getFaction();
|
||||
|
||||
String oldname = myFaction.getName();
|
||||
myFaction.setName(name);
|
||||
|
||||
// Inform
|
||||
myFaction.sendMessage(me.getFullName(myFaction)+Conf.colorSystem+" changed the name of your faction to "+Conf.colorMember+name);
|
||||
for (Faction faction : Faction.getAll()) {
|
||||
if (faction.id == me.factionId) {
|
||||
continue;
|
||||
}
|
||||
faction.sendMessage(Conf.colorSystem+"The faction "+me.getRelationColor(faction)+oldname+Conf.colorSystem+" renamed themselves to "+me.getRelationColor(faction)+name);
|
||||
}
|
||||
}
|
||||
|
||||
public static void list(Follower me) {
|
||||
me.sendMessage(TextUtil.titleize("Faction List"), false);
|
||||
for (Faction faction : Faction.getAll()) {
|
||||
me.sendMessage(faction.getName(me)+Conf.colorSystem+" ("+faction.getFollowersWhereOnline(true).size()+" / "+faction.getFollowersAll().size()+" online)");
|
||||
}
|
||||
}
|
||||
|
||||
public static void showFaction(Follower 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);
|
||||
|
||||
me.sendMessage(TextUtil.titleize(faction.getName(me)), false);
|
||||
me.sendMessage(Conf.colorChrome+"Description: "+Conf.colorSystem+faction.getDescription());
|
||||
if (faction.id != 0) {
|
||||
me.sendMessage(Conf.colorChrome+"Power: "+Conf.colorSystem+faction.getPowerRounded()+" / "+faction.getPowerMaxRounded()); // TODO this is not so easy to understand
|
||||
me.sendMessage(Conf.colorChrome+"Land: "+Conf.colorSystem+faction.getLandRounded()+" / "+faction.getLandMaxRounded());
|
||||
|
||||
if(faction.getOpen()) {
|
||||
me.sendMessage(Conf.colorChrome+"Joining: "+Conf.colorSystem+"no invitation is needed");
|
||||
} else {
|
||||
me.sendMessage(Conf.colorChrome+"Joining: "+Conf.colorSystem+"invitation is required");
|
||||
}
|
||||
}
|
||||
|
||||
String onlineList = Conf.colorChrome+"Members online: ";
|
||||
String offlineList = Conf.colorChrome+"Members offline: ";
|
||||
String listpart;
|
||||
for (Follower follower : admins) {
|
||||
listpart = follower.getFullName(me)+Conf.colorSystem+", ";
|
||||
if (follower.isOnline()) {
|
||||
onlineList += listpart;
|
||||
} else {
|
||||
offlineList += listpart;
|
||||
}
|
||||
}
|
||||
for (Follower follower : mods) {
|
||||
listpart = follower.getFullName(me)+Conf.colorSystem+", ";
|
||||
if (follower.isOnline()) {
|
||||
onlineList += listpart;
|
||||
} else {
|
||||
offlineList += listpart;
|
||||
}
|
||||
}
|
||||
for (Follower follower : normals) {
|
||||
listpart = follower.getFullName(me)+Conf.colorSystem+", ";
|
||||
if (follower.isOnline()) {
|
||||
onlineList += listpart;
|
||||
} else {
|
||||
offlineList += listpart;
|
||||
}
|
||||
}
|
||||
|
||||
if (onlineList.endsWith(", ")) {
|
||||
onlineList = onlineList.substring(0, onlineList.length()-2);
|
||||
}
|
||||
if (offlineList.endsWith(", ")) {
|
||||
offlineList = offlineList.substring(0, offlineList.length()-2);
|
||||
}
|
||||
|
||||
me.sendMessage(onlineList);
|
||||
me.sendMessage(offlineList);
|
||||
}
|
||||
|
||||
|
||||
public static void showMap(Follower me, String mapAutoUpdating) {
|
||||
if (mapAutoUpdating.length() > 0) {
|
||||
if (Conf.aliasTrue.contains(mapAutoUpdating.toLowerCase())) {
|
||||
// Turn on
|
||||
me.setMapAutoUpdating(true);
|
||||
me.sendMessage(Conf.colorAction + "Map auto update ENABLED.");
|
||||
} else {
|
||||
// Turn off
|
||||
me.setMapAutoUpdating(false);
|
||||
me.sendMessage(Conf.colorAction + "Map auto update DISABLED.");
|
||||
}
|
||||
} else {
|
||||
me.sendMessage(Board.getMap(me.getFaction(), Coord.from(me), me.getPlayer().getLocation().getYaw()), false);
|
||||
}
|
||||
}
|
||||
|
||||
public static void invite(Follower me, String name) {
|
||||
Follower follower = findFollower(me, name, false);
|
||||
if (follower == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayList<String> errors = me.invite(follower);
|
||||
me.sendMessage(errors);
|
||||
|
||||
if (errors.size() == 0) {
|
||||
ChatColor relationColor = me.getRelationColor(follower);
|
||||
follower.sendMessage(relationColor+me.getFullName()+Conf.colorSystem+" invited you to "+relationColor+me.getFaction().getName());
|
||||
me.getFaction().sendMessage(me.getFullName(me)+Conf.colorSystem+" invited "+follower.getFullName(me)+Conf.colorSystem+" to your faction.");
|
||||
//me.sendMessage(Conf.colorAction+"You invited "+relationColor+follower.getFullName()+Conf.colorAction+" to "+Relation.MEMBER.getColor()+me.getFaction().getName());
|
||||
}
|
||||
}
|
||||
|
||||
public static void deinvite(Follower me, String name) { // TODO Move out!
|
||||
Follower follower = findFollower(me, name, false);
|
||||
if (follower == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayList<String> errors = me.deinvite(follower);
|
||||
me.sendMessage(errors);
|
||||
|
||||
if (errors.size() == 0) {
|
||||
follower.sendMessage(me.getFullName(follower)+Conf.colorSystem+" revoked your invitation to "+me.getFaction().getName(follower));
|
||||
me.getFaction().sendMessage(me.getFullName(me)+Conf.colorSystem+" revoked "+follower.getFullName(me)+"'s"+Conf.colorSystem+" invitation.");
|
||||
}
|
||||
}
|
||||
|
||||
public static void open(Follower me) {
|
||||
if (me.role.value < Role.MODERATOR.value) {
|
||||
me.sendMessage(Conf.colorSystem+"You must be moderator to do this");
|
||||
return;
|
||||
}
|
||||
Faction myFaction = me.getFaction();
|
||||
myFaction.setOpen( ! me.getFaction().getOpen());
|
||||
|
||||
String open = myFaction.getOpen() ? "open" : "closed";
|
||||
|
||||
// Inform
|
||||
myFaction.sendMessage(me.getFullName(myFaction)+Conf.colorSystem+" changed the faction to "+open);
|
||||
for (Faction faction : Faction.getAll()) {
|
||||
if (faction.id == me.factionId) {
|
||||
continue;
|
||||
}
|
||||
faction.sendMessage(Conf.colorSystem+"The faction "+myFaction.getName(faction)+Conf.colorSystem+" is now "+open);
|
||||
}
|
||||
}
|
||||
|
||||
public static void title(Follower me, ArrayList<String> tokens) {
|
||||
if (tokens.size() == 0) {
|
||||
me.sendMessage(Conf.colorSystem+"You must specify a player name");
|
||||
return;
|
||||
}
|
||||
|
||||
String name = tokens.get(0);
|
||||
tokens.remove(0);
|
||||
|
||||
Follower you = findFollower(me, name, true);
|
||||
if (you == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! canIAdministerYou(me, you)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// All ok! Set the title!
|
||||
String title = TextUtil.implode(tokens);
|
||||
you.setTitle(title);
|
||||
|
||||
// Inform
|
||||
Faction myFaction = me.getFaction();
|
||||
myFaction.sendMessage(me.getFullName(myFaction)+Conf.colorSystem+" changed a title: "+you.getFullName(myFaction));
|
||||
}
|
||||
|
||||
public static void kick(Follower me, String name) {
|
||||
if (name.length() == 0) {
|
||||
me.sendMessage(Conf.colorSystem+"You must specify a player name.");
|
||||
return;
|
||||
}
|
||||
|
||||
Follower you = findFollower(me, name, false);
|
||||
if (you == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayList<String> errors = me.kick(you);
|
||||
me.sendMessage(errors);
|
||||
|
||||
if (errors.size() == 0) {
|
||||
Faction myFaction = me.getFaction();
|
||||
myFaction.sendMessage(me.getFullName(myFaction)+Conf.colorSystem+" kicked "+you.getFullName(myFaction)+Conf.colorSystem+" from the faction! :O");
|
||||
you.sendMessage(me.getFullName(you)+Conf.colorSystem+" kicked you from "+myFaction.getName(you)+Conf.colorSystem+"! :O");
|
||||
}
|
||||
}
|
||||
|
||||
public static void roleChange(Follower me, Role targetRole, String name) {
|
||||
if (me.role.value < Role.ADMIN.value) {
|
||||
me.sendMessage(Conf.colorSystem+"You must be faction admin to do this");
|
||||
return;
|
||||
}
|
||||
|
||||
if (name.length() == 0) {
|
||||
me.sendMessage(Conf.colorSystem+"You must specify a player name.");
|
||||
return;
|
||||
}
|
||||
|
||||
Follower targetFollower = findFollower(me, name, false);
|
||||
if (targetFollower == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (targetFollower.factionId != me.factionId) {
|
||||
ChatColor relationColor = me.getRelationColor(targetFollower);
|
||||
me.sendMessage(relationColor+targetFollower.getFullName()+Conf.colorSystem+" is not a member in your faction.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (targetFollower == me) {
|
||||
me.sendMessage(Conf.colorSystem+"The target player musn't be yourself.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (targetRole == Role.ADMIN) {
|
||||
me.role = Role.MODERATOR;
|
||||
targetFollower.role = Role.ADMIN;
|
||||
|
||||
// Inform all players
|
||||
for (Follower follower : Follower.getAll()) {
|
||||
if (follower.factionId == me.factionId) {
|
||||
follower.sendMessage(Conf.colorMember+me.getFullName()+Conf.colorSystem+" gave "+Conf.colorMember+targetFollower.getFullName()+" the leadership of your faction.");
|
||||
} else {
|
||||
follower.sendMessage(me.getFullName(follower)+Conf.colorSystem+" gave "+targetFollower.getFullName(follower)+" the leadership of "+me.getFaction().getName(follower));
|
||||
}
|
||||
}
|
||||
} else if (targetRole == Role.MODERATOR) {
|
||||
if (targetFollower.role == Role.MODERATOR) {
|
||||
// Revoke
|
||||
targetFollower.role = Role.NORMAL;
|
||||
me.getFaction().sendMessage(Conf.colorMember+targetFollower.getName()+Conf.colorSystem+" is no longer moderator in your faction.");
|
||||
} else {
|
||||
// Give
|
||||
targetFollower.role = Role.MODERATOR;
|
||||
me.getFaction().sendMessage(Conf.colorMember+targetFollower.getName()+Conf.colorSystem+" was promoted to moderator in your faction.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void claim(Follower me) {
|
||||
if (me.factionId == 0) {
|
||||
me.sendMessage(Conf.colorSystem+"You are not part of any faction.");
|
||||
return;
|
||||
}
|
||||
|
||||
Coord coord = Coord.from(me);
|
||||
Faction otherFaction = coord.getFaction();
|
||||
Faction myFaction = me.getFaction();
|
||||
|
||||
if (myFaction.equals(otherFaction)) {
|
||||
me.sendMessage(Conf.colorSystem+"You already own this land.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (me.role.value < Role.MODERATOR.value) {
|
||||
me.sendMessage(Conf.colorSystem+"You must be moderator to claim land.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (myFaction.getLandRounded() >= myFaction.getLandMaxRounded()) {
|
||||
me.sendMessage(Conf.colorSystem+"You can't claim more land! You need more power!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (otherFaction.getRelation(me) == Relation.ALLY) {
|
||||
me.sendMessage(Conf.colorSystem+"You can't claim the land of your allies.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (otherFaction.id != 0) {
|
||||
if ( ! otherFaction.hasLandInflation()) { // TODO more messages
|
||||
me.sendMessage(me.getRelationColor(otherFaction)+otherFaction.getName()+Conf.colorSystem+" owns this land and are strong enough to keep it.");
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! Board.isBorderCoord(coord)) {
|
||||
me.sendMessage(Conf.colorSystem+"You must start claiming land at the border of the territory.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (otherFaction.id == 0) {
|
||||
myFaction.sendMessage(Conf.colorMember+me.getFullName()+Conf.colorSystem+" claimed some new land :D");
|
||||
} else {
|
||||
// ASDF claimed some of your land 450 blocks NNW of you.
|
||||
// ASDf claimed some land from FACTION NAME
|
||||
ChatColor relcolor = myFaction.getRelationColor(otherFaction);
|
||||
otherFaction.sendMessage(relcolor+me.getFullName()+Conf.colorSystem+" from "+relcolor+myFaction.getName()+Conf.colorSystem+" stole some of your land :O");
|
||||
myFaction.sendMessage(Conf.colorMember+me.getFullName()+Conf.colorSystem+" claimed some land from "+relcolor+otherFaction.getName());
|
||||
}
|
||||
|
||||
Board.claim(coord, myFaction);
|
||||
}
|
||||
|
||||
public static void unclaim(Follower me) {
|
||||
if (me.factionId == 0) {
|
||||
me.sendMessage(Conf.colorSystem+"You are not part of any faction");
|
||||
return;
|
||||
}
|
||||
|
||||
if (me.role.value < Role.MODERATOR.value) {
|
||||
me.sendMessage(Conf.colorSystem+"You must be moderator to unclaim land");
|
||||
return;
|
||||
}
|
||||
|
||||
Coord coord = Coord.from(me.getPlayer());
|
||||
|
||||
if ( ! me.getFaction().equals(coord.getFaction())) {
|
||||
me.sendMessage(Conf.colorSystem+"You don't own this land.");
|
||||
return;
|
||||
}
|
||||
|
||||
Board.unclaim(coord);
|
||||
me.sendMessage(Conf.colorAction+"Successfully unclaimed");
|
||||
}
|
||||
|
||||
public static void relation(Follower me, Relation whishedRelation, String otherFactionName) {
|
||||
if (me.factionId == 0) {
|
||||
me.sendMessage(Conf.colorSystem+"You are not part of any faction.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (me.role.value < Role.MODERATOR.value) {
|
||||
me.sendMessage(Conf.colorSystem+"You must be moderator to set relation to other factions.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (otherFactionName.length() == 0) {
|
||||
me.sendMessage(Conf.colorSystem+"You must specify another faction.");
|
||||
return;
|
||||
}
|
||||
|
||||
Faction otherFaction = findFaction(me, otherFactionName, false);
|
||||
if (otherFaction == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (otherFaction.id == 0) {
|
||||
me.sendMessage(Conf.colorSystem+"Nope! You can't :) The default faction is not a real faction.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (otherFaction.equals(me.getFaction())) {
|
||||
me.sendMessage(Conf.colorSystem+"Nope! You can't declare a relation to yourself :)");
|
||||
return;
|
||||
}
|
||||
|
||||
Faction myFaction = me.getFaction();
|
||||
myFaction.setRelationWish(otherFaction, whishedRelation);
|
||||
Relation currentRelation = myFaction.getRelation(otherFaction);
|
||||
ChatColor currentRelationColor = currentRelation.getColor();
|
||||
if (whishedRelation == currentRelation) {
|
||||
otherFaction.sendMessage(Conf.colorSystem+"Your faction is now "+currentRelationColor+whishedRelation.toString()+Conf.colorSystem+" to "+currentRelationColor+myFaction.getName());
|
||||
myFaction.sendMessage(Conf.colorSystem+"Your faction is now "+currentRelationColor+whishedRelation.toString()+Conf.colorSystem+" to "+currentRelationColor+otherFaction.getName());
|
||||
} else {
|
||||
otherFaction.sendMessage(currentRelationColor+myFaction.getName()+Conf.colorSystem+ " whishes to be your "+whishedRelation.getColor()+whishedRelation.toString());
|
||||
otherFaction.sendMessage(Conf.colorSystem+"Type "+Conf.colorCommand+Conf.aliasBase.get(0)+" "+whishedRelation+" "+myFaction.getName()+Conf.colorSystem+" to accept.");
|
||||
myFaction.sendMessage(currentRelationColor+otherFaction.getName()+Conf.colorSystem+ " were informed you wishes to be "+whishedRelation.getColor()+whishedRelation);
|
||||
}
|
||||
}
|
||||
|
||||
public static void description(Follower me, String desc) {
|
||||
if (me.factionId == 0) {
|
||||
me.sendMessage(Conf.colorSystem+"You are not part of any faction");
|
||||
return;
|
||||
}
|
||||
|
||||
if (me.role.value < Role.MODERATOR.value) {
|
||||
me.sendMessage(Conf.colorSystem+"You must be moderator to set the description");
|
||||
return;
|
||||
}
|
||||
|
||||
me.getFaction().setDescription(desc);
|
||||
|
||||
me.sendMessage(Conf.colorAction+"The new decription was set :D");
|
||||
|
||||
// Broadcast the description to everyone
|
||||
for (Follower follower : EM.followerGetAll()) {
|
||||
follower.sendMessage(Conf.colorSystem+"The faction "+follower.getRelationColor(me)+me.getFaction().getName()+Conf.colorSystem+" changed their description to:");
|
||||
follower.sendMessage(Conf.colorSystem+desc);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
72
src/com/bukkit/mcteam/factions/Factions.java
Normal file
72
src/com/bukkit/mcteam/factions/Factions.java
Normal file
@ -0,0 +1,72 @@
|
||||
package com.bukkit.mcteam.factions;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.PluginLoader;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.bukkit.mcteam.factions.entities.*;
|
||||
import com.bukkit.mcteam.factions.util.Log;
|
||||
|
||||
public class Factions extends JavaPlugin {
|
||||
public static PluginLoader pluginLoader;
|
||||
public static Server server;
|
||||
public static PluginDescriptionFile desc;
|
||||
public static File folder;
|
||||
public static File plugin;
|
||||
public static ClassLoader cLoader;
|
||||
|
||||
private final FactionsPlayerListener playerListener = new FactionsPlayerListener(this);
|
||||
private final FactionsEntityListener entityListener = new FactionsEntityListener(this);
|
||||
private final FactionsBlockListener blockListener = new FactionsBlockListener(this);
|
||||
|
||||
public Factions(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
|
||||
super(pluginLoader, instance, desc, folder, plugin, cLoader);
|
||||
|
||||
Factions.pluginLoader = pluginLoader;
|
||||
Factions.server = instance;
|
||||
Factions.desc = desc;
|
||||
Factions.folder = folder;
|
||||
Factions.plugin = plugin;
|
||||
Factions.cLoader = cLoader;
|
||||
|
||||
Log.info("=== INIT START ===");
|
||||
long timeInitStart = System.currentTimeMillis();
|
||||
Log.info("You are running version: "+desc.getVersion());
|
||||
|
||||
EM.loadAll();
|
||||
|
||||
// Register events
|
||||
PluginManager pm = instance.getPluginManager();
|
||||
pm.registerEvent(Event.Type.PLAYER_COMMAND, this.playerListener, Event.Priority.Normal, this);
|
||||
pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this);
|
||||
pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Normal, this);
|
||||
pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener, Event.Priority.Normal, this);
|
||||
pm.registerEvent(Event.Type.PLAYER_QUIT, this.playerListener, Event.Priority.Normal, this);
|
||||
pm.registerEvent(Event.Type.ENTITY_DEATH, this.entityListener, Event.Priority.Normal, this);
|
||||
pm.registerEvent(Event.Type.ENTITY_DAMAGEDBY_ENTITY, this.entityListener, Event.Priority.Normal, this);
|
||||
pm.registerEvent(Event.Type.BLOCK_DAMAGED, this.blockListener, Event.Priority.Normal, this);
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
101
src/com/bukkit/mcteam/factions/FactionsBlockListener.java
Normal file
101
src/com/bukkit/mcteam/factions/FactionsBlockListener.java
Normal file
@ -0,0 +1,101 @@
|
||||
package com.bukkit.mcteam.factions;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockDamageLevel;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.block.BlockDamageEvent;
|
||||
import org.bukkit.event.block.BlockInteractEvent;
|
||||
import org.bukkit.event.block.BlockListener;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
|
||||
import com.bukkit.mcteam.factions.entities.*;
|
||||
import com.bukkit.mcteam.factions.util.TextUtil;
|
||||
|
||||
public class FactionsBlockListener extends BlockListener {
|
||||
public Factions plugin;
|
||||
public FactionsBlockListener(Factions plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPlace(BlockPlaceEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return; // Alright. lets listen to that.
|
||||
}
|
||||
if ( ! this.playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "build")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockDamage(BlockDamageEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return; // Alright. lets listen to that.
|
||||
}
|
||||
if (event.getDamageLevel() == BlockDamageLevel.BROKEN && ! this.playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "destroy")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean playerCanBuildDestroyBlock(Player player, Block block, String action) {
|
||||
Coord coord = Coord.parseCoord(block);
|
||||
Faction otherFaction = coord.getFaction();
|
||||
|
||||
if (otherFaction.id == 0) {
|
||||
return true; // This is no faction territory. You may build or break stuff here.
|
||||
}
|
||||
|
||||
Follower me = Follower.get(player);
|
||||
Faction myFaction = me.getFaction();
|
||||
|
||||
// Cancel if we are not in our own territory
|
||||
if (myFaction != otherFaction) {
|
||||
me.sendMessage(Conf.colorSystem+"You can't "+action+" in the territory of "+otherFaction.getName(myFaction));
|
||||
otherFaction.sendMessage(me.getFullName(otherFaction)+Conf.colorSystem+" tried to "+action+" "+TextUtil.getMaterialName(block.getType())+" in your territory");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockInteract(BlockInteractEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return; // Alright. lets listen to that.
|
||||
}
|
||||
|
||||
if ( ! (event.getEntity() instanceof Player)) {
|
||||
// So far mobs does not interact with the environment :P
|
||||
return;
|
||||
}
|
||||
|
||||
Block block = event.getBlock();
|
||||
Player player = (Player) event.getEntity();
|
||||
|
||||
if ( ! canPlayerUseRightclickBlock(player, block)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canPlayerUseRightclickBlock(Player player, Block block) {
|
||||
Material material = block.getType();
|
||||
|
||||
// We only care about some material types.
|
||||
if ( ! Conf.territoryProtectedMaterials.contains(material)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Follower me = Follower.get(player);
|
||||
Faction myFaction = me.getFaction();
|
||||
Coord blockCoord = Coord.from(block.getLocation());
|
||||
Faction otherFaction = blockCoord.getFaction();
|
||||
|
||||
if (otherFaction.id != 0 && myFaction != otherFaction) {
|
||||
me.sendMessage(Conf.colorSystem+"You can't use "+TextUtil.getMaterialName(material)+" in the territory of "+otherFaction.getName(myFaction));
|
||||
otherFaction.sendMessage(me.getFullName(otherFaction)+Conf.colorSystem+" tried to use "+TextUtil.getMaterialName(material)+" in your territory");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
97
src/com/bukkit/mcteam/factions/FactionsEntityListener.java
Normal file
97
src/com/bukkit/mcteam/factions/FactionsEntityListener.java
Normal file
@ -0,0 +1,97 @@
|
||||
package com.bukkit.mcteam.factions;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.event.entity.EntityListener;
|
||||
|
||||
import com.bukkit.mcteam.factions.entities.Conf;
|
||||
import com.bukkit.mcteam.factions.entities.Follower;
|
||||
import com.bukkit.mcteam.factions.struct.Relation;
|
||||
import com.bukkit.mcteam.factions.util.Log;
|
||||
|
||||
public class FactionsEntityListener extends EntityListener {
|
||||
public Factions plugin;
|
||||
public FactionsEntityListener(Factions plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEntityDeath(EntityDeathEvent event) {
|
||||
Entity entity = event.getEntity();
|
||||
if ( ! (entity instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = (Player) entity;
|
||||
Follower follower = Follower.get(player);
|
||||
follower.onDeath();
|
||||
follower.sendMessage(Conf.colorSystem+"Your power is now "+follower.getPowerRounded()+" / "+follower.getPowerMaxRounded());
|
||||
}
|
||||
/**
|
||||
* Who can I hurt?
|
||||
* I can never hurt members or allies.
|
||||
* I can always hurt enemies.
|
||||
* I can hurt neutrals as long as they are outside their own territory.
|
||||
*/
|
||||
@Override
|
||||
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return; // Some other plugin decided. Alright then.
|
||||
}
|
||||
|
||||
Entity entity = event.getEntity();
|
||||
if ( ! (entity instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Entity damager = event.getDamager();
|
||||
if ( ! (damager instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log.debug(((Player)entity).getName()+ " is the defender");
|
||||
Log.debug(((Player)damager).getName()+ " is the damager");
|
||||
|
||||
Follower defender = Follower.get((Player)entity);
|
||||
Follower attacker = Follower.get((Player)damager);
|
||||
Relation relation = defender.getRelation(attacker);
|
||||
|
||||
// Players without faction may be hurt anywhere
|
||||
if (defender.factionId == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// You can never hurt faction members or allies
|
||||
if (relation == Relation.MEMBER || relation == Relation.ALLY) {
|
||||
attacker.sendMessage(Conf.colorSystem+"You can't hurt "+relation.getColor()+defender.getFullName());
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// You can not hurt neutrals in their own territory.
|
||||
if (relation == Relation.NEUTRAL && defender.isInOwnTerritory()) {
|
||||
attacker.sendMessage(Conf.colorSystem+"You can't hurt "+relation.getColor()+defender.getFullName()+" in their own territory.");
|
||||
defender.sendMessage(relation.getColor()+attacker.getFullName()+Conf.colorSystem+" tried to hurt you.");
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Damage will be dealt. However check if the damage should be reduced.
|
||||
if (defender.isInOwnTerritory()) {
|
||||
int damage = event.getDamage();
|
||||
int toHeal = (int)Math.round(damage * Conf.territoryShieldFactor);
|
||||
defender.heal(toHeal);
|
||||
|
||||
// Send message
|
||||
DecimalFormat formatter = new DecimalFormat("#.#");
|
||||
String hearts = formatter.format(toHeal / 2.0);
|
||||
defender.sendMessage(Conf.colorSystem+"Enemy damage reduced by "+ChatColor.RED+hearts+Conf.colorSystem+" hearts.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
103
src/com/bukkit/mcteam/factions/FactionsPlayerListener.java
Normal file
103
src/com/bukkit/mcteam/factions/FactionsPlayerListener.java
Normal file
@ -0,0 +1,103 @@
|
||||
package com.bukkit.mcteam.factions;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.event.player.PlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerEvent;
|
||||
import org.bukkit.event.player.PlayerListener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
import com.bukkit.mcteam.factions.entities.*;
|
||||
import com.bukkit.mcteam.factions.util.*;
|
||||
|
||||
public class FactionsPlayerListener extends PlayerListener{
|
||||
public Factions plugin;
|
||||
public FactionsPlayerListener(Factions plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* If someone says something that starts with the factions base command
|
||||
* we handle that command.
|
||||
*/
|
||||
@Override
|
||||
public void onPlayerCommand(PlayerChatEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
String msg = event.getMessage();
|
||||
|
||||
if (handleCommandOrChat(player, msg)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerChat(PlayerChatEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
String msg = event.getMessage();
|
||||
|
||||
// Process the command or
|
||||
if ( ! handleCommandOrChat(player, msg) && Conf.useRelationColoredChat) {
|
||||
for (Player receiver : Factions.server.getOnlinePlayers()) {
|
||||
Follower follower = Follower.get(player);
|
||||
receiver.sendMessage("<"+follower.getFullName(Follower.get(receiver))+ChatColor.WHITE+"> "+msg);
|
||||
}
|
||||
}
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
public boolean handleCommandOrChat(Player player, String msg) {
|
||||
ArrayList<String> tokens = TextUtil.split(msg.trim());
|
||||
if (Conf.aliasBase.contains(tokens.get(0))) {
|
||||
tokens.remove(0);
|
||||
Follower follower = Follower.get(player);
|
||||
Commands.base(follower, tokens);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerJoin(PlayerEvent event) {
|
||||
EM.onPlayerLogin(event.getPlayer());
|
||||
//Follower.get(event.getPlayer()).sendJoinInfo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerQuit(PlayerEvent event) {
|
||||
Follower follower = Follower.get(event.getPlayer());
|
||||
Log.debug("Saved follower on player quit: "+follower.getFullName());
|
||||
follower.save(); // We save the followers on logout in order to save their non autosaved state like power.
|
||||
EM.onPlayerLogout(event.getPlayer()); // Remove the player link.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerMove(PlayerMoveEvent event) {
|
||||
// Did we change coord?
|
||||
Location from = event.getFrom();
|
||||
Location to = event.getTo();
|
||||
Coord coordFrom = Coord.from(from);
|
||||
Coord coordTo = Coord.from(to);
|
||||
if (coordFrom.equals(coordTo)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Yes we did change coord (:
|
||||
Follower me = Follower.get(event.getPlayer());
|
||||
|
||||
if (me.isMapAutoUpdating()) {
|
||||
me.sendMessage(Board.getMap(me.getFaction(), Coord.from(me), me.getPlayer().getLocation().getYaw()), false);
|
||||
} else {
|
||||
// Did we change "host"(faction)?
|
||||
Faction factionFrom = Board.getFactionAt(coordFrom);
|
||||
Faction factionTo = Board.getFactionAt(coordTo);
|
||||
if ( factionFrom != factionTo) {
|
||||
me.sendFactionHereMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
156
src/com/bukkit/mcteam/factions/entities/Board.java
Normal file
156
src/com/bukkit/mcteam/factions/entities/Board.java
Normal file
@ -0,0 +1,156 @@
|
||||
package com.bukkit.mcteam.factions.entities;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import com.bukkit.mcteam.factions.util.TextUtil;
|
||||
import com.bukkit.mcteam.util.AsciiCompass;
|
||||
|
||||
//import com.bukkit.mcteam.factions.util.*;
|
||||
|
||||
public class Board {
|
||||
protected static Map<Coord, Integer> coordFactionIds;
|
||||
|
||||
static {
|
||||
coordFactionIds = new HashMap<Coord, Integer>();
|
||||
}
|
||||
|
||||
public static Faction getFactionAt(Coord coord) {
|
||||
return Faction.get(getFactionIdAt(coord));
|
||||
}
|
||||
public static int getFactionIdAt(Coord coord) {
|
||||
Integer factionId = coordFactionIds.get(coord);
|
||||
if (factionId == null) {
|
||||
return 0; // No faction
|
||||
}
|
||||
return factionId;
|
||||
}
|
||||
|
||||
public static void unclaim(Coord coord) {
|
||||
coordFactionIds.remove(coord);
|
||||
save();
|
||||
}
|
||||
|
||||
public static void claim(Coord coord, Faction faction) {
|
||||
coordFactionIds.put(coord, faction.id);
|
||||
save();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public static void purgeFaction(Faction faction) {
|
||||
purgeFaction(faction.id);
|
||||
}
|
||||
public static 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 static int getFactionCoordCount(Faction faction) {
|
||||
return getFactionCoordCount(faction.id);
|
||||
}
|
||||
public static int getFactionCoordCount(int factionId) {
|
||||
int ret = 0;
|
||||
for (int thatFactionId : coordFactionIds.values()) {
|
||||
if(thatFactionId == factionId) {
|
||||
ret += 1;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Map generation
|
||||
//----------------------------------------------//
|
||||
|
||||
/**
|
||||
* The map is relative to a coord and a faction
|
||||
* north is in the direction of decreasing x
|
||||
* east is in the direction of decreasing z
|
||||
*/
|
||||
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)));
|
||||
|
||||
int halfWidth = Conf.mapWidth / 2;
|
||||
int halfHeight = Conf.mapHeight / 2;
|
||||
Coord topLeft = coord.getRelative(-halfHeight, halfWidth);
|
||||
int width = halfWidth * 2 + 1;
|
||||
int height = halfHeight * 2 + 1;
|
||||
|
||||
// For each row
|
||||
for (int dx = 0; dx < height; dx++) {
|
||||
// Draw and add that row
|
||||
String row = "";
|
||||
for (int dz = 0; dz > -width; dz--) {
|
||||
if(dz == -(halfWidth) && dx == halfHeight) {
|
||||
row += ChatColor.AQUA+"+";
|
||||
} else {
|
||||
Coord coordHere = topLeft.getRelative(dx, dz);
|
||||
Faction factionHere = coordHere.getFaction();
|
||||
if (factionHere.id == 0) {
|
||||
row += ChatColor.GRAY+"-";
|
||||
} else {
|
||||
row += factionHere.getRelation(faction).getColor()+"+";
|
||||
}
|
||||
}
|
||||
}
|
||||
ret.add(row);
|
||||
}
|
||||
|
||||
// Get the compass
|
||||
ArrayList<String> asciiCompass = AsciiCompass.getAsciiCompass(inDegrees, ChatColor.RED, Conf.colorChrome);
|
||||
// Pad the compass some
|
||||
asciiCompass.set(0, asciiCompass.get(0));
|
||||
asciiCompass.set(1, asciiCompass.get(1));
|
||||
asciiCompass.set(2, asciiCompass.get(2));
|
||||
// Add the compass
|
||||
ret.set(1, asciiCompass.get(0)+ret.get(1).substring(3*3));
|
||||
ret.set(2, asciiCompass.get(1)+ret.get(2).substring(3*3));
|
||||
ret.set(3, asciiCompass.get(2)+ret.get(3).substring(3*3));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------//
|
||||
// Persistance
|
||||
//----------------------------------------------//
|
||||
|
||||
public static boolean save() {
|
||||
return EM.boardSave();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
211
src/com/bukkit/mcteam/factions/entities/Conf.java
Normal file
211
src/com/bukkit/mcteam/factions/entities/Conf.java
Normal file
@ -0,0 +1,211 @@
|
||||
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 colorSystem;
|
||||
public static ChatColor colorAction;
|
||||
public static ChatColor colorChrome;
|
||||
public static ChatColor colorCommand;
|
||||
public static ChatColor colorParameter;
|
||||
|
||||
// Command names / aliases
|
||||
public static List<String> aliasBase = new ArrayList<String>();
|
||||
public static List<String> aliasHelp = new ArrayList<String>();
|
||||
|
||||
public static List<String> aliasList = new ArrayList<String>();
|
||||
public static List<String> aliasShow = new ArrayList<String>();
|
||||
|
||||
public static List<String> aliasMap = new ArrayList<String>();
|
||||
public static List<String> aliasHere = new ArrayList<String>();
|
||||
|
||||
|
||||
public static List<String> aliasJoin = new ArrayList<String>();
|
||||
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> aliasTitle = new ArrayList<String>();
|
||||
public static List<String> aliasInvite = new ArrayList<String>();
|
||||
public static List<String> aliasDeinvite = new ArrayList<String>();
|
||||
public static List<String> aliasOpen = new ArrayList<String>();
|
||||
|
||||
public static List<String> aliasKick = new ArrayList<String>();
|
||||
public static List<String> aliasModerator = new ArrayList<String>();
|
||||
public static List<String> aliasAdmin = new ArrayList<String>();
|
||||
|
||||
public static List<String> aliasClaim = new ArrayList<String>();
|
||||
public static List<String> aliasUnclaim = new ArrayList<String>();
|
||||
|
||||
public static List<String> aliasRelationAlly = new ArrayList<String>();
|
||||
public static List<String> aliasRelationNeutral = new ArrayList<String>();
|
||||
public static List<String> aliasRelationEnemy = new ArrayList<String>();
|
||||
|
||||
public static List<String> aliasDescription = new ArrayList<String>();
|
||||
|
||||
// Value aliases
|
||||
public static List<String> aliasTrue = new ArrayList<String>();
|
||||
|
||||
// Power
|
||||
public static double powerPerLand;
|
||||
public static double powerPerPlayer;
|
||||
public static double powerPerMinute; // Default health rate
|
||||
public static double powerPerDeath;
|
||||
public static double powerDefaultBonus;
|
||||
|
||||
// Protected blocks
|
||||
public static List<Material> territoryProtectedMaterials = new ArrayList<Material>();
|
||||
|
||||
static {
|
||||
logThreshold = 10;
|
||||
prefixAdmin = "**";
|
||||
prefixMod = "*";
|
||||
factionNameMinLength = 3;
|
||||
factionNameMaxLength = 40;
|
||||
|
||||
mapHeight = 8;
|
||||
mapWidth = 49;
|
||||
|
||||
territoryShieldFactor = 0.33;
|
||||
|
||||
useRelationColoredChat = true;
|
||||
|
||||
colorMember = ChatColor.GREEN;
|
||||
colorAlly = ChatColor.LIGHT_PURPLE;
|
||||
colorNeutral = ChatColor.WHITE;
|
||||
colorEnemy = ChatColor.RED;
|
||||
|
||||
colorSystem = ChatColor.YELLOW;
|
||||
colorAction = ChatColor.LIGHT_PURPLE;
|
||||
colorChrome = ChatColor.GOLD;
|
||||
colorCommand = ChatColor.AQUA;
|
||||
colorParameter = ChatColor.DARK_AQUA;
|
||||
|
||||
aliasBase.add("/f");
|
||||
aliasBase.add("f");
|
||||
aliasBase.add("/faction");
|
||||
aliasBase.add("faction");
|
||||
aliasBase.add("/factions");
|
||||
aliasBase.add("factions");
|
||||
|
||||
aliasHelp.add("help");
|
||||
aliasHelp.add("h");
|
||||
aliasHelp.add("?");
|
||||
|
||||
aliasList.add("list");
|
||||
aliasList.add("ls");
|
||||
|
||||
aliasShow.add("show");
|
||||
aliasShow.add("who");
|
||||
|
||||
aliasMap.add("map");
|
||||
aliasHere.add("here");
|
||||
|
||||
aliasJoin.add("join");
|
||||
|
||||
aliasLeave.add("leave");
|
||||
|
||||
aliasCreate.add("create");
|
||||
aliasCreate.add("new");
|
||||
|
||||
aliasName.add("name");
|
||||
aliasName.add("rename");
|
||||
|
||||
aliasTitle.add("title");
|
||||
|
||||
aliasInvite.add("invite");
|
||||
aliasInvite.add("inv");
|
||||
|
||||
aliasDeinvite.add("deinvite");
|
||||
aliasDeinvite.add("deinv");
|
||||
|
||||
aliasOpen.add("open");
|
||||
aliasOpen.add("close");
|
||||
|
||||
aliasKick.add("kick");
|
||||
|
||||
aliasModerator.add("mod");
|
||||
|
||||
aliasAdmin.add("admin");
|
||||
|
||||
aliasClaim.add("claim");
|
||||
|
||||
aliasUnclaim.add("unclaim");
|
||||
aliasUnclaim.add("declaim");
|
||||
|
||||
aliasRelationAlly.add("ally");
|
||||
aliasRelationNeutral.add("neutral");
|
||||
aliasRelationEnemy.add("enemy");
|
||||
|
||||
aliasDescription.add("desc");
|
||||
|
||||
aliasTrue.add("true");
|
||||
aliasTrue.add("yes");
|
||||
aliasTrue.add("y");
|
||||
aliasTrue.add("ok");
|
||||
aliasTrue.add("on");
|
||||
aliasTrue.add("+");
|
||||
|
||||
powerPerLand = 1; // 1 power grants one land
|
||||
powerPerPlayer = 5; // One player has 5 power
|
||||
powerPerMinute = 0.2; // Default health rate... it takes 5 min to heal one death
|
||||
powerPerDeath = 1; //A death makes you loose 2 power
|
||||
powerDefaultBonus = 0; //A faction normally has a power bonus
|
||||
|
||||
territoryProtectedMaterials.add(Material.WOODEN_DOOR);
|
||||
territoryProtectedMaterials.add(Material.DISPENSER);
|
||||
territoryProtectedMaterials.add(Material.CHEST);
|
||||
territoryProtectedMaterials.add(Material.FURNACE);
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Persistance
|
||||
//----------------------------------------------//
|
||||
|
||||
public static boolean save() {
|
||||
return EM.configSave();
|
||||
}
|
||||
}
|
77
src/com/bukkit/mcteam/factions/entities/Coord.java
Normal file
77
src/com/bukkit/mcteam/factions/entities/Coord.java
Normal file
@ -0,0 +1,77 @@
|
||||
package com.bukkit.mcteam.factions.entities;
|
||||
|
||||
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;
|
||||
|
||||
public Coord(int x, int z) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
// TODO implements cloneable
|
||||
public Coord(Coord coord) {
|
||||
this.x = coord.x;
|
||||
this.z = coord.z;
|
||||
}
|
||||
|
||||
public Coord() {
|
||||
// Noarg constructor for google gson.
|
||||
}
|
||||
|
||||
public Coord getRelative(int dx, int dz) {
|
||||
return new Coord(this.x + dx, this.z + dz);
|
||||
}
|
||||
|
||||
public static Coord from(int x, int z) {
|
||||
return new Coord(x / cellSize - (x < 0 ? 1 : 0), z / cellSize - (z < 0 ? 1 : 0));
|
||||
}
|
||||
|
||||
public static Coord from(Player player) {
|
||||
return from(player.getLocation());
|
||||
}
|
||||
|
||||
public static Coord from(Follower follower) {
|
||||
return from(follower.getPlayer());
|
||||
}
|
||||
|
||||
public static Coord from(Location loc) {
|
||||
return from(loc.getBlockX(), loc.getBlockZ());
|
||||
}
|
||||
|
||||
public static Coord parseCoord(Block block) {
|
||||
return from(block.getX(), block.getZ());
|
||||
}
|
||||
|
||||
public Faction getFaction() {
|
||||
return Board.getFactionAt(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.x + "," + this.z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
result = 31 * result + x;
|
||||
result = 31 * result + z;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
if (!(obj instanceof Coord))
|
||||
return false;
|
||||
|
||||
Coord o = (Coord) obj;
|
||||
return this.x == o.x && this.z == o.z;
|
||||
}
|
||||
}
|
316
src/com/bukkit/mcteam/factions/entities/EM.java
Normal file
316
src/com/bukkit/mcteam/factions/entities/EM.java
Normal file
@ -0,0 +1,316 @@
|
||||
package com.bukkit.mcteam.factions.entities;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.bukkit.mcteam.factions.Factions;
|
||||
import com.bukkit.mcteam.factions.util.*;
|
||||
import com.bukkit.mcteam.util.DiscUtil;
|
||||
import com.google.gson.*;
|
||||
|
||||
/**
|
||||
* This is a entity manager that persists object to json.
|
||||
* Before using the the EM you should always EM.loadAll().
|
||||
* The methods assume that all on disc is loaded into memory.
|
||||
*/
|
||||
public class EM {
|
||||
protected static Map<String, Follower> followers = new HashMap<String, Follower>(); // 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 int nextFactionId;
|
||||
|
||||
// hardcoded config
|
||||
protected final static String ext = ".json";
|
||||
protected final static File folderBase = Factions.folder;
|
||||
protected final static File folderFaction = new File(folderBase, "faction");
|
||||
protected final static File folderFollower = new File(folderBase, "follower");
|
||||
protected final static File fileConfig = new File(folderBase, "conf"+ext);
|
||||
protected final static File fileBoard = new File(folderBase, "board"+ext);
|
||||
|
||||
public final static Gson gson = new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE)
|
||||
.registerTypeAdapter(Map.class, new MapAsArrayTypeAdapter()) // a "must have" adapter for GSON
|
||||
.create();
|
||||
|
||||
public static void loadAll() {
|
||||
configLoad();
|
||||
Log.threshold = Conf.logThreshold;
|
||||
boardLoad();
|
||||
followerLoadAll();
|
||||
factionLoadAll();
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Config methods (load, save)
|
||||
//----------------------------------------------//
|
||||
public static boolean configLoad() {
|
||||
if (fileConfig.exists()) {
|
||||
try {
|
||||
gson.fromJson(DiscUtil.read(fileConfig), Conf.class);
|
||||
Log.info("Config was loaded from disc");
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.warn("Failed to load the config");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Log.info("No conf.json found! Creating a new one with the default values");
|
||||
//configSave(); // FOR DEBUGGING...
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean configSave() {
|
||||
try {
|
||||
DiscUtil.write(fileConfig, gson.toJson(new Conf()));
|
||||
Log.debug("Config was saved to disc");
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.warn("Failed to save the config");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Board methods (load, save)
|
||||
//----------------------------------------------//
|
||||
public static boolean boardLoad() {
|
||||
if (fileBoard.exists()) {
|
||||
try {
|
||||
gson.fromJson(DiscUtil.read(fileBoard), Board.class);
|
||||
Log.info("Board was loaded from disc");
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.warn("Failed to load the board");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Log.info("No board.json found! Creating a new one with the default values");
|
||||
//boardSave(); // FOR DEBUGGING...
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean boardSave() {
|
||||
try {
|
||||
DiscUtil.write(fileBoard, gson.toJson(new Board()));
|
||||
Log.debug("Board was saved to disc");
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.warn("Failed to save the board");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// 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.
|
||||
*/
|
||||
public static void followerLoadAll() {
|
||||
Log.info("Loading all followers from disc...");
|
||||
folderFollower.mkdirs();
|
||||
|
||||
class jsonFileFilter implements FileFilter {
|
||||
public boolean accept(File file) {
|
||||
return (file.getName().toLowerCase().endsWith(ext) && file.isFile());
|
||||
}
|
||||
}
|
||||
|
||||
File[] jsonFiles = folderFollower.listFiles(new jsonFileFilter());
|
||||
|
||||
for (File jsonFile : jsonFiles) {
|
||||
// Extract the name from the filename. The name is filename minus ".json"
|
||||
String name = jsonFile.getName();
|
||||
name = name.substring(0, name.length() - ext.length());
|
||||
try {
|
||||
Follower follower = gson.fromJson(DiscUtil.read(jsonFile), Follower.class);
|
||||
follower.id = name;
|
||||
followers.put(follower.id, follower);
|
||||
Log.debug("loaded follower "+name);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.warn("failed to load follower "+name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Collection<Follower> followerGetAll() {
|
||||
return followers.values();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the follower object for a player
|
||||
* A new Follower will be created if the player did not have one
|
||||
*/
|
||||
public static Follower followerGet(Player player) {
|
||||
String key = followerKey(player);
|
||||
|
||||
if (followers.containsKey(key)) {
|
||||
return followers.get(key);
|
||||
}
|
||||
|
||||
return followerCreate(player);
|
||||
}
|
||||
|
||||
public static boolean followerSave(String id) {
|
||||
Object obj = followers.get(id);
|
||||
if (obj == null) {
|
||||
Log.warn("Could not save follower "+id+" as it was not loaded");
|
||||
return false;
|
||||
}
|
||||
folderFollower.mkdirs();
|
||||
File file = new File(folderFollower, id+ext);
|
||||
try {
|
||||
DiscUtil.write(file, gson.toJson(obj));
|
||||
Log.debug("Saved the follower "+id);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.warn("Failed to save the follower "+id);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected static String followerKey(Player player) {
|
||||
return player.getName();
|
||||
}
|
||||
|
||||
protected static Follower followerCreate(Player player) {
|
||||
Log.debug("Creating new follower "+followerKey(player));
|
||||
Follower follower = new Follower();
|
||||
follower.id = followerKey(player);
|
||||
followers.put(follower.id, follower);
|
||||
follower.save();
|
||||
return follower;
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Faction methods (loadAll, get, create, delete, save)
|
||||
//----------------------------------------------//
|
||||
|
||||
/**
|
||||
* This method loads all followers from disc into memory.
|
||||
*/
|
||||
public static void factionLoadAll() {
|
||||
Log.info("Loading all factions from disc...");
|
||||
folderFaction.mkdirs();
|
||||
|
||||
class jsonFileFilter implements FileFilter
|
||||
{
|
||||
public boolean accept(File file) {
|
||||
return (file.getName().toLowerCase().endsWith(ext) && file.isFile());
|
||||
}
|
||||
}
|
||||
|
||||
nextFactionId = 0;
|
||||
File[] jsonFiles = folderFaction.listFiles(new jsonFileFilter());
|
||||
for (File jsonFile : jsonFiles) {
|
||||
// Extract the name from the filename. The name is filename minus ".json"
|
||||
String name = jsonFile.getName();
|
||||
name = name.substring(0, name.length() - ext.length());
|
||||
int id = Integer.parseInt(name);
|
||||
|
||||
// Eventually push next id forward
|
||||
if (nextFactionId < id) {
|
||||
nextFactionId = id;
|
||||
}
|
||||
|
||||
try {
|
||||
Faction faction = gson.fromJson(DiscUtil.read(jsonFile), Faction.class);
|
||||
faction.id = id;
|
||||
factions.put(faction.id, faction);
|
||||
Log.debug("loaded faction "+id);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.warn("Failed to load faction "+id);
|
||||
}
|
||||
}
|
||||
|
||||
nextFactionId += 1; // make it the next id and not the current highest.
|
||||
|
||||
// Make sure the default neutral faction exists
|
||||
if ( ! factions.containsKey(0)) {
|
||||
Faction faction = new Faction();
|
||||
faction.name = "*No faction*";
|
||||
faction.description = "\"The faction for the factionless :P\"";
|
||||
faction.id = 0;
|
||||
factions.put(faction.id, faction);
|
||||
}
|
||||
}
|
||||
|
||||
public static Faction factionGet(Integer factionId) {
|
||||
return factions.get(factionId);
|
||||
}
|
||||
|
||||
public static Collection<Faction> factionGetAll() {
|
||||
return factions.values();
|
||||
}
|
||||
|
||||
public static Faction factionCreate(){
|
||||
Faction faction = new Faction();
|
||||
faction.id = nextFactionId;
|
||||
nextFactionId += 1;
|
||||
factions.put(faction.id, faction);
|
||||
Log.debug("created new faction "+faction.id);
|
||||
faction.save();
|
||||
return faction;
|
||||
}
|
||||
|
||||
public static boolean factionDelete(Integer id) {
|
||||
// NOTE that this does not do any security checks.
|
||||
// Follower might get orphaned foreign id's
|
||||
|
||||
// purge from board
|
||||
Board.purgeFaction(id);
|
||||
|
||||
// Remove the file
|
||||
File file = new File(folderFaction, id+ext);
|
||||
file.delete();
|
||||
|
||||
// Remove the faction
|
||||
factions.remove(id);
|
||||
|
||||
return true; // TODO
|
||||
}
|
||||
|
||||
public static boolean factionSave(Integer id) {
|
||||
Object obj = factions.get(id);
|
||||
if (obj == null) {
|
||||
Log.warn("Could not save faction "+id+" as it was not loaded");
|
||||
return false;
|
||||
}
|
||||
folderFaction.mkdirs();
|
||||
File file = new File(folderFaction, id+ext);
|
||||
try {
|
||||
DiscUtil.write(file, gson.toJson(obj));
|
||||
Log.debug("saved the faction "+id);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.warn("failed to save the faction "+id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
363
src/com/bukkit/mcteam/factions/entities/Faction.java
Normal file
363
src/com/bukkit/mcteam/factions/entities/Faction.java
Normal file
@ -0,0 +1,363 @@
|
||||
package com.bukkit.mcteam.factions.entities;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
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.util.ChatFixUtil;
|
||||
|
||||
public class Faction {
|
||||
|
||||
public transient int id;
|
||||
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 description;
|
||||
|
||||
public Faction() {
|
||||
this.relationWish = new HashMap<Integer, Relation>();
|
||||
this.invites = new HashSet<String>();
|
||||
this.open = true;
|
||||
this.name = "Untitled Faction :(";
|
||||
this.description = "Default faction description :(";
|
||||
}
|
||||
|
||||
// -------------------------------
|
||||
// Information
|
||||
// -------------------------------
|
||||
public String getName() {
|
||||
return this.getName("");
|
||||
}
|
||||
public String getName(String prefix) {
|
||||
return prefix+this.name;
|
||||
}
|
||||
public String getName(Faction otherFaction) {
|
||||
return this.getName(otherFaction.getRelationColor(this).toString());
|
||||
}
|
||||
|
||||
public String getName(Follower otherFollower) {
|
||||
return this.getName(otherFollower.getRelationColor(this).toString());
|
||||
}
|
||||
|
||||
public void setName(String newName) {
|
||||
this.name = newName;
|
||||
this.save();
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public void setDescription(String value) {
|
||||
this.description = value;
|
||||
this.save();
|
||||
}
|
||||
|
||||
public boolean getOpen() {
|
||||
return open;
|
||||
}
|
||||
|
||||
public void setOpen(boolean isOpen) {
|
||||
open = isOpen;
|
||||
this.save();
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Power
|
||||
//----------------------------------------------//
|
||||
public double getPower() {
|
||||
double ret = this.getPowerBonus();
|
||||
for (Follower follower : this.getFollowersAll()) {
|
||||
ret += follower.getPower();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public double getPowerBonus() {
|
||||
return Conf.powerDefaultBonus; // TODO this could be modified by commands later on
|
||||
}
|
||||
|
||||
public double getPowerMax() {
|
||||
double ret = this.getPowerBonus();
|
||||
for (Follower follower : this.getFollowersAll()) {
|
||||
ret += follower.getPowerMax();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public int getPowerRounded() {
|
||||
return (int) Math.round(this.getPower());
|
||||
}
|
||||
|
||||
public int getPowerMaxRounded() {
|
||||
return (int) Math.round(this.getPowerMax());
|
||||
}
|
||||
|
||||
public int getLandRounded() {
|
||||
return Board.getFactionCoordCount(this);
|
||||
}
|
||||
|
||||
public double getLandMax() {
|
||||
return this.getPower() / Conf.powerPerLand;
|
||||
}
|
||||
|
||||
public int getLandMaxRounded() {
|
||||
return (int) Math.round(this.getLandMax());
|
||||
}
|
||||
|
||||
public boolean hasLandInflation() {
|
||||
return Board.getFactionCoordCount(this) > this.getLandMaxRounded();
|
||||
}
|
||||
|
||||
// -------------------------------
|
||||
// Membership management
|
||||
// -------------------------------
|
||||
|
||||
|
||||
public ArrayList<String> invite(Follower follower) {
|
||||
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());
|
||||
}
|
||||
|
||||
if(errors.size() > 0) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
this.invites.add(follower.id);
|
||||
this.save();
|
||||
return errors;
|
||||
}
|
||||
|
||||
public ArrayList<String> deinvite(Follower follower) {
|
||||
ArrayList<String> errors = new ArrayList<String>();
|
||||
|
||||
if (follower.getFaction().equals(this)) {
|
||||
errors.add(Conf.colorSystem+follower.getFullName()+" is already a member of "+this.getName());
|
||||
errors.add(Conf.colorSystem+"You might want to "+Conf.colorCommand+Conf.aliasBase.get(0)+" "+Conf.aliasKick.get(0)+Conf.colorParameter+" "+follower.getName());
|
||||
}
|
||||
|
||||
if(errors.size() > 0) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
this.invites.remove(follower.id);
|
||||
this.save();
|
||||
return errors;
|
||||
}
|
||||
|
||||
public ArrayList<String> kick(Follower follower) {
|
||||
ArrayList<String> errors = new ArrayList<String>();
|
||||
removeFollower(follower);
|
||||
return errors;
|
||||
}
|
||||
|
||||
|
||||
public boolean isInvited(Follower follower) {
|
||||
return invites.contains(follower.id);
|
||||
}
|
||||
|
||||
// -------------------------------
|
||||
// Followers
|
||||
// -------------------------------
|
||||
|
||||
public ArrayList<Follower> getFollowersAll() {
|
||||
ArrayList<Follower> ret = new ArrayList<Follower>();
|
||||
for (Follower follower : Follower.getAll()) {
|
||||
if (follower.factionId == this.id) {
|
||||
ret.add(follower);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public ArrayList<Follower> getFollowersWhereOnline(boolean online) {
|
||||
ArrayList<Follower> ret = new ArrayList<Follower>();
|
||||
for (Follower follower : Follower.getAll()) {
|
||||
if (follower.factionId == this.id && follower.isOnline() == online) {
|
||||
ret.add(follower);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public ArrayList<Follower> getFollowersWhereRole(Role role) {
|
||||
ArrayList<Follower> ret = new ArrayList<Follower>();
|
||||
|
||||
for (Follower follower : Follower.getAll()) {
|
||||
if (follower.factionId == this.id && follower.role.equals(role)) {
|
||||
ret.add(follower);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void removeFollower(Follower follower) {
|
||||
if (this.id != follower.factionId) {
|
||||
return; // safety check
|
||||
}
|
||||
|
||||
this.invites.remove(follower.id);
|
||||
follower.resetFactionData();
|
||||
follower.save();
|
||||
this.save();
|
||||
}
|
||||
|
||||
public ArrayList<Player> getOnlinePlayers() {
|
||||
ArrayList<Player> ret = new ArrayList<Player>();
|
||||
for (Player player: Factions.server.getOnlinePlayers()) {
|
||||
Follower follower = Follower.get(player);
|
||||
if (follower.factionId == this.id) {
|
||||
ret.add(player);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Faction name
|
||||
//----------------------------------------------//
|
||||
|
||||
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 static ArrayList<String> validateName(String name) {
|
||||
ArrayList<String> errors = new ArrayList<String>();
|
||||
|
||||
if(Faction.toComparisonName(name).length() < Conf.factionNameMinLength) {
|
||||
errors.add(Conf.colorSystem+"That name is to short");
|
||||
}
|
||||
|
||||
if(name.length() > Conf.factionNameMaxLength) {
|
||||
errors.add(Conf.colorSystem+"That name is to long");
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
public String getComparisonName() {
|
||||
return Faction.toComparisonName(this.name);
|
||||
}
|
||||
|
||||
public static Faction find(String name) {
|
||||
String compName = Faction.toComparisonName(name);
|
||||
for (Faction faction : Faction.getAll()) {
|
||||
if (faction.getComparisonName().equals(compName)) {
|
||||
return faction;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isNameTaken(String name) {
|
||||
return Faction.find(name) != null;
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Messages - Directly connected to ChatFixUtil
|
||||
//----------------------------------------------//
|
||||
public void sendMessage(String message, boolean fix) {
|
||||
ChatFixUtil.sendMessage(this.getOnlinePlayers(), message, fix);
|
||||
}
|
||||
public void sendMessage(List<String> messages, boolean fix) {
|
||||
ChatFixUtil.sendMessage(this.getOnlinePlayers(), messages, fix);
|
||||
}
|
||||
public void sendMessage(String message) {
|
||||
ChatFixUtil.sendMessage(this.getOnlinePlayers(), message, true);
|
||||
}
|
||||
public void sendMessage(List<String> messages) {
|
||||
ChatFixUtil.sendMessage(this.getOnlinePlayers(), messages, true);
|
||||
}
|
||||
|
||||
// -------------------------------
|
||||
// Relation and relation colors
|
||||
// -------------------------------
|
||||
|
||||
public Relation getRelationWish(Faction otherFaction) {
|
||||
if (this.relationWish.containsKey(otherFaction.id)){
|
||||
return this.relationWish.get(otherFaction.id);
|
||||
}
|
||||
return Relation.NEUTRAL;
|
||||
}
|
||||
|
||||
public void setRelationWish(Faction otherFaction, Relation relation) {
|
||||
if (this.relationWish.containsKey(otherFaction.id) && relation.equals(Relation.NEUTRAL)){
|
||||
this.relationWish.remove(otherFaction.id);
|
||||
return;
|
||||
}
|
||||
this.relationWish.put(otherFaction.id, relation);
|
||||
}
|
||||
|
||||
public Relation getRelation(Faction otherFaction) {
|
||||
if (otherFaction.id == 0 || this.id == 0) {
|
||||
return Relation.NEUTRAL;
|
||||
}
|
||||
if (otherFaction.equals(this)) {
|
||||
return Relation.MEMBER;
|
||||
}
|
||||
if(this.getRelationWish(otherFaction).value >= otherFaction.getRelationWish(this).value) {
|
||||
return otherFaction.getRelationWish(this);
|
||||
}
|
||||
return this.getRelationWish(otherFaction);
|
||||
}
|
||||
|
||||
public Relation getRelation(Follower follower) {
|
||||
return getRelation(follower.getFaction());
|
||||
}
|
||||
|
||||
public ChatColor getRelationColor(Faction otherFaction) {
|
||||
return this.getRelation(otherFaction).getColor();
|
||||
}
|
||||
|
||||
public ChatColor getRelationColor(Follower follower) {
|
||||
return this.getRelation(follower).getColor();
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Persistance and entity management
|
||||
//----------------------------------------------//
|
||||
|
||||
public static Faction create() {
|
||||
return EM.factionCreate();
|
||||
}
|
||||
|
||||
public static Faction get(Integer factionId) {
|
||||
return EM.factionGet(factionId);
|
||||
}
|
||||
|
||||
public static Collection<Faction> getAll() {
|
||||
return EM.factionGetAll();
|
||||
}
|
||||
|
||||
public boolean save() {
|
||||
return EM.factionSave(this.id);
|
||||
}
|
||||
|
||||
}
|
400
src/com/bukkit/mcteam/factions/entities/Follower.java
Normal file
400
src/com/bukkit/mcteam/factions/entities/Follower.java
Normal file
@ -0,0 +1,400 @@
|
||||
package com.bukkit.mcteam.factions.entities;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
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;
|
||||
private String title;
|
||||
private double power;
|
||||
private long lastPowerUpdateTime;
|
||||
private boolean mapAutoUpdating;
|
||||
|
||||
public Follower() {
|
||||
this.resetFactionData();
|
||||
this.power = this.getPowerMax();
|
||||
this.lastPowerUpdateTime = System.currentTimeMillis();
|
||||
this.mapAutoUpdating = false;
|
||||
}
|
||||
|
||||
protected void resetFactionData() {
|
||||
this.factionId = 0; // The default neutral faction
|
||||
this.role = Role.NORMAL;
|
||||
this.title = "";
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public boolean isOnline() {
|
||||
return this.getPlayer() != null;
|
||||
}
|
||||
|
||||
public boolean isMapAutoUpdating() {
|
||||
return mapAutoUpdating;
|
||||
}
|
||||
|
||||
public void setMapAutoUpdating(boolean mapAutoUpdating) {
|
||||
this.mapAutoUpdating = mapAutoUpdating;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
this.save();
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Health
|
||||
//----------------------------------------------//
|
||||
public void heal(int amnt) {
|
||||
Player player = this.getPlayer();
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
player.setHealth(player.getHealth() + amnt);
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------//
|
||||
// Power
|
||||
//----------------------------------------------//
|
||||
public double getPower() {
|
||||
this.updatePower();
|
||||
return this.power;
|
||||
}
|
||||
|
||||
protected void alterPower(double delta) {
|
||||
this.power += delta;
|
||||
if (this.power > this.getPowerMax()) {
|
||||
this.power = this.getPowerMax();
|
||||
} else if (this.power < this.getPowerMin()) {
|
||||
this.power = this.getPowerMin();
|
||||
}
|
||||
Log.debug("Power of "+this.getFullName()+" is now: "+this.power);
|
||||
}
|
||||
|
||||
public double getPowerMax() {
|
||||
return Conf.powerPerPlayer;
|
||||
}
|
||||
|
||||
public double getPowerMin() {
|
||||
return -Conf.powerPerPlayer;
|
||||
}
|
||||
|
||||
public int getPowerRounded() {
|
||||
return (int) Math.round(this.getPower());
|
||||
}
|
||||
|
||||
public int getPowerMaxRounded() {
|
||||
return (int) Math.round(this.getPowerMax());
|
||||
}
|
||||
|
||||
public int getPowerMinRounded() {
|
||||
return (int) Math.round(this.getPowerMin());
|
||||
}
|
||||
|
||||
protected void updatePower() {
|
||||
long now = System.currentTimeMillis();
|
||||
long millisPassed = now - this.lastPowerUpdateTime;
|
||||
this.lastPowerUpdateTime = now;
|
||||
|
||||
int millisPerMinute = 60*1000;
|
||||
this.alterPower(millisPassed * Conf.powerPerMinute / millisPerMinute);
|
||||
//this.save(); // This would save to often. So we save this on player quit instead.
|
||||
}
|
||||
|
||||
public void onDeath() {
|
||||
this.updatePower();
|
||||
this.alterPower(-Conf.powerPerDeath);
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Territory
|
||||
//----------------------------------------------//
|
||||
public boolean isInOwnTerritory() {
|
||||
return Board.getFactionAt(this.getCoord()) == this.getFaction();
|
||||
}
|
||||
|
||||
public boolean isInOthersTerritory() {
|
||||
Faction factionHere = Board.getFactionAt(this.getCoord());
|
||||
return factionHere.id != 0 && factionHere != this.getFaction();
|
||||
}
|
||||
|
||||
public Coord getCoord() {
|
||||
return Coord.from(this);
|
||||
}
|
||||
|
||||
public void sendFactionHereMessage() {
|
||||
Faction factionHere = Board.getFactionAt(this.getCoord());
|
||||
String msg = Conf.colorSystem+" ~ "+factionHere.getName(this);
|
||||
this.sendMessage(msg);
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// Faction management
|
||||
//----------------------------------------------//
|
||||
public Faction getFaction() {
|
||||
return EM.factionGet(factionId);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
if( ! faction.getOpen() && ! faction.isInvited(this)) {
|
||||
errors.add(Conf.colorSystem+"This guild requires invitation.");
|
||||
}
|
||||
|
||||
if (this.factionId != 0) {
|
||||
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.factionId == 0) {
|
||||
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> createFaction(String name) {
|
||||
ArrayList<String> errors = new ArrayList<String>();
|
||||
|
||||
if (this.factionId != 0) {
|
||||
errors.add(Conf.colorSystem+"You must leave your current faction first.");
|
||||
}
|
||||
|
||||
if (Faction.isNameTaken(name)) {
|
||||
errors.add(Conf.colorSystem+"That name is already in use.");
|
||||
}
|
||||
|
||||
errors.addAll(Faction.validateName(name));
|
||||
|
||||
if (errors.size() > 0) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
Faction faction = EM.factionCreate();
|
||||
faction.setName(name);
|
||||
faction.save();
|
||||
this.join(faction);
|
||||
this.role = Role.ADMIN;
|
||||
this.save();
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
public ArrayList<String> invite(Follower follower) {
|
||||
ArrayList<String> errors = new ArrayList<String>();
|
||||
|
||||
//Log.debug("this.role: "+this.role);
|
||||
//Log.debug("this.role.value: "+this.role.value);
|
||||
//Log.debug("FactionRole.MODERATOR.value: "+FactionRole.MODERATOR.value);
|
||||
|
||||
if (this.role.value < Role.MODERATOR.value) {
|
||||
errors.add(Conf.colorSystem+"You must me be a moderator to invite.");
|
||||
}
|
||||
|
||||
if(errors.size() > 0) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
return this.getFaction().invite(follower);
|
||||
}
|
||||
|
||||
public ArrayList<String> deinvite(Follower follower) {
|
||||
ArrayList<String> errors = new ArrayList<String>();
|
||||
|
||||
if (this.role.value < Role.MODERATOR.value) {
|
||||
errors.add(Conf.colorSystem+"You must me be a moderator to deinvite.");
|
||||
}
|
||||
|
||||
if(errors.size() > 0) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
return follower.getFaction().deinvite(follower);
|
||||
}
|
||||
|
||||
public ArrayList<String> kick(Follower 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());
|
||||
} 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));
|
||||
} else if (follower.role.value >= this.role.value) { // TODO add more informative messages.
|
||||
errors.add(Conf.colorSystem+"Your rank is to low to kick this player.");
|
||||
}
|
||||
|
||||
if(errors.size() > 0) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// 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()) {
|
||||
if (follower.getName().equalsIgnoreCase(name.trim())) {
|
||||
return 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
|
||||
//----------------------------------------------//
|
||||
|
||||
public boolean save() {
|
||||
return EM.followerSave(this.id);
|
||||
}
|
||||
|
||||
public static Follower get(Player player) {
|
||||
return EM.followerGet(player);
|
||||
}
|
||||
|
||||
public static Collection<Follower> getAll() {
|
||||
return EM.followerGetAll();
|
||||
}
|
||||
}
|
48
src/com/bukkit/mcteam/factions/struct/Relation.java
Normal file
48
src/com/bukkit/mcteam/factions/struct/Relation.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.bukkit.mcteam.factions.struct;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import com.bukkit.mcteam.factions.entities.*;
|
||||
|
||||
public enum Relation {
|
||||
MEMBER(3, "member"),
|
||||
ALLY(2, "ally"),
|
||||
NEUTRAL(1, "neutral"),
|
||||
ENEMY(0, "enemy");
|
||||
//UNKNOWN(-1, "unknown");
|
||||
|
||||
public final int value;
|
||||
public final String nicename;
|
||||
|
||||
private Relation(final int value, final String nicename) {
|
||||
this.value = value;
|
||||
this.nicename = nicename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.nicename;
|
||||
}
|
||||
|
||||
public ChatColor getColor() {
|
||||
return Conf.relationColor(this);
|
||||
}
|
||||
|
||||
/*public String getChartDot() {
|
||||
return Conf.chartDot(this);
|
||||
}
|
||||
|
||||
public static Relation from(String str) {
|
||||
if (str.equalsIgnoreCase("member")) {
|
||||
return Relation.MEMBER;
|
||||
} else if (str.equalsIgnoreCase("ally")) {
|
||||
return Relation.ALLY;
|
||||
} else if (str.equalsIgnoreCase("neutral")) {
|
||||
return Relation.NEUTRAL;
|
||||
} else if (str.equalsIgnoreCase("enemy")) {
|
||||
return Relation.ENEMY;
|
||||
}
|
||||
|
||||
return Relation.UNKNOWN;
|
||||
}*/
|
||||
}
|
20
src/com/bukkit/mcteam/factions/struct/Role.java
Normal file
20
src/com/bukkit/mcteam/factions/struct/Role.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.bukkit.mcteam.factions.struct;
|
||||
|
||||
public enum Role {
|
||||
ADMIN(2, "admin"),
|
||||
MODERATOR(1, "moderator"),
|
||||
NORMAL(0, "normal player");
|
||||
|
||||
public final int value;
|
||||
public final String nicename;
|
||||
|
||||
private Role(final int value, final String nicename) {
|
||||
this.value = value;
|
||||
this.nicename = nicename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.nicename;
|
||||
}
|
||||
}
|
39
src/com/bukkit/mcteam/factions/util/Log.java
Normal file
39
src/com/bukkit/mcteam/factions/util/Log.java
Normal file
@ -0,0 +1,39 @@
|
||||
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.desc.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);
|
||||
}
|
||||
}
|
63
src/com/bukkit/mcteam/factions/util/TextUtil.java
Normal file
63
src/com/bukkit/mcteam/factions/util/TextUtil.java
Normal file
@ -0,0 +1,63 @@
|
||||
package com.bukkit.mcteam.factions.util;
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
import com.bukkit.mcteam.factions.entities.*;
|
||||
|
||||
public class TextUtil {
|
||||
public static String titleize(String str) {
|
||||
String line = Conf.colorChrome+repeat("_", 60);
|
||||
String center = ".[ " + Conf.colorSystem + str + Conf.colorChrome + " ].";
|
||||
int pivot = line.length() / 2;
|
||||
int eatLeft = center.length() / 2;
|
||||
int eatRight = center.length() - eatLeft;
|
||||
return line.substring(0, pivot - eatLeft) + center + line.substring(pivot + eatRight);
|
||||
}
|
||||
|
||||
public static String repeat(String s, int times) {
|
||||
if (times <= 0) return "";
|
||||
else return s + repeat(s, times-1);
|
||||
}
|
||||
|
||||
public static ArrayList<String> split(String str) {
|
||||
return new ArrayList<String>(Arrays.asList(str.trim().split("\\s+")));
|
||||
}
|
||||
|
||||
public static String implode(List<String> list, String glue) {
|
||||
String ret = "";
|
||||
for (int i=0; i<list.size(); i++) {
|
||||
if (i!=0) {
|
||||
ret += glue;
|
||||
}
|
||||
ret += list.get(i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public static String implode(List<String> list) {
|
||||
return implode(list, " ");
|
||||
}
|
||||
|
||||
public static String commandHelp(List<String> aliases, String param, String desc) {
|
||||
ArrayList<String> parts = new ArrayList<String>();
|
||||
parts.add(Conf.colorCommand+Conf.aliasBase.get(0));
|
||||
parts.add(TextUtil.implode(aliases, ", "));
|
||||
if (param.length() > 0) {
|
||||
parts.add(Conf.colorParameter+param);
|
||||
}
|
||||
if (desc.length() > 0) {
|
||||
parts.add(Conf.colorSystem+desc);
|
||||
}
|
||||
//Log.debug(TextUtil.implode(parts, " "));
|
||||
return TextUtil.implode(parts, " ");
|
||||
}
|
||||
|
||||
public static String getMaterialName(Material material) {
|
||||
String ret = material.toString();
|
||||
ret = ret.replace('_', ' ');
|
||||
ret = ret.toLowerCase();
|
||||
return ret.substring(0, 1).toUpperCase()+ret.substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
89
src/com/bukkit/mcteam/util/AsciiCompass.java
Normal file
89
src/com/bukkit/mcteam/util/AsciiCompass.java
Normal file
@ -0,0 +1,89 @@
|
||||
package com.bukkit.mcteam.util;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
public class AsciiCompass {
|
||||
public enum Point {
|
||||
N('N'),
|
||||
NE('/'),
|
||||
E('W'),
|
||||
SE('\\'),
|
||||
S('S'),
|
||||
SW('/'),
|
||||
W('W'),
|
||||
NW('\\');
|
||||
|
||||
public final char asciiChar;
|
||||
|
||||
private Point(final char asciiChar) {
|
||||
this.asciiChar = asciiChar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(this.asciiChar);
|
||||
}
|
||||
|
||||
public String toString(boolean isActive, ChatColor colorActive, ChatColor colorDefault) {
|
||||
return (isActive ? colorActive : colorDefault)+String.valueOf(this.asciiChar);
|
||||
}
|
||||
}
|
||||
|
||||
public static AsciiCompass.Point getCompassPointForDirection(double inDegrees) {
|
||||
double degrees = (inDegrees - 90) % 360 ;
|
||||
if (degrees < 0)
|
||||
degrees += 360;
|
||||
|
||||
if (0 <= degrees && degrees < 22.5)
|
||||
return AsciiCompass.Point.N;
|
||||
else if (22.5 <= degrees && degrees < 67.5)
|
||||
return AsciiCompass.Point.NE;
|
||||
else if (67.5 <= degrees && degrees < 112.5)
|
||||
return AsciiCompass.Point.E;
|
||||
else if (112.5 <= degrees && degrees < 157.5)
|
||||
return AsciiCompass.Point.SE;
|
||||
else if (157.5 <= degrees && degrees < 202.5)
|
||||
return AsciiCompass.Point.S;
|
||||
else if (202.5 <= degrees && degrees < 247.5)
|
||||
return AsciiCompass.Point.SW;
|
||||
else if (247.5 <= degrees && degrees < 292.5)
|
||||
return AsciiCompass.Point.W;
|
||||
else if (292.5 <= degrees && degrees < 337.5)
|
||||
return AsciiCompass.Point.NW;
|
||||
else if (337.5 <= degrees && degrees < 360.0)
|
||||
return AsciiCompass.Point.N;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ArrayList<String> getAsciiCompass(Point point, ChatColor colorActive, ChatColor colorDefault) {
|
||||
ArrayList<String> ret = new ArrayList<String>();
|
||||
String row;
|
||||
|
||||
row = "";
|
||||
row += Point.NW.toString(Point.NW == point, colorActive, colorDefault);
|
||||
row += Point.N.toString(Point.N == point, colorActive, colorDefault);
|
||||
row += Point.NE.toString(Point.NE == point, colorActive, colorDefault);
|
||||
ret.add(row);
|
||||
|
||||
row = "";
|
||||
row += Point.W.toString(Point.W == point, colorActive, colorDefault);
|
||||
row += colorDefault+"+";
|
||||
row += Point.E.toString(Point.E == point, colorActive, colorDefault);
|
||||
ret.add(row);
|
||||
|
||||
row = "";
|
||||
row += Point.SW.toString(Point.SW == point, colorActive, colorDefault);
|
||||
row += Point.S.toString(Point.S == point, colorActive, colorDefault);
|
||||
row += Point.SE.toString(Point.SE == point, colorActive, colorDefault);
|
||||
ret.add(row);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static ArrayList<String> getAsciiCompass(double inDegrees, ChatColor colorActive, ChatColor colorDefault) {
|
||||
return getAsciiCompass(getCompassPointForDirection(inDegrees), colorActive, colorDefault);
|
||||
}
|
||||
}
|
230
src/com/bukkit/mcteam/util/ChatFixUtil.java
Normal file
230
src/com/bukkit/mcteam/util/ChatFixUtil.java
Normal file
@ -0,0 +1,230 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
32
src/com/bukkit/mcteam/util/DiscUtil.java
Normal file
32
src/com/bukkit/mcteam/util/DiscUtil.java
Normal file
@ -0,0 +1,32 @@
|
||||
package com.bukkit.mcteam.util;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Harddisc related methods such as read and write.
|
||||
*/
|
||||
public class DiscUtil {
|
||||
/**
|
||||
* Convenience function for writing a string to a file.
|
||||
*/
|
||||
public static void write(File file, String content) throws IOException {
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8"));
|
||||
out.write(content);
|
||||
out.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function for reading a file as a string.
|
||||
*/
|
||||
public static String read(File file) throws IOException {
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
|
||||
String ret = new String(new byte[0], "UTF-8");
|
||||
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
ret += line;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
168
src/com/google/gson/MapAsArrayTypeAdapter.java
Normal file
168
src/com/google/gson/MapAsArrayTypeAdapter.java
Normal file
@ -0,0 +1,168 @@
|
||||
/*
|
||||
* Copyright (C) 2010 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.gson;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Adapts maps containing complex keys as arrays of map entries.
|
||||
*
|
||||
* <h3>Maps as JSON objects</h3>
|
||||
* The standard GSON map type adapter converts Java {@link Map Maps} to JSON
|
||||
* Objects. This requires that map keys can be serialized as strings; this is
|
||||
* insufficient for some key types. For example, consider a map whose keys are
|
||||
* points on a grid. The default JSON form encodes reasonably: <pre> {@code
|
||||
* Map<Point, String> original = new LinkedHashMap<Point, String>();
|
||||
* original.put(new Point(5, 6), "a");
|
||||
* original.put(new Point(8, 8), "b");
|
||||
* System.out.println(gson.toJson(original, type));
|
||||
* }</pre>
|
||||
* The above code prints this JSON object:<pre> {@code
|
||||
* {
|
||||
* "(5,6)": "a",
|
||||
* "(8,8)": "b"
|
||||
* }
|
||||
* }</pre>
|
||||
* But GSON is unable to deserialize this value because the JSON string name is
|
||||
* just the {@link Object#toString() toString()} of the map key. Attempting to
|
||||
* convert the above JSON to an object fails with a parse exception:
|
||||
* <pre>com.google.gson.JsonParseException: Expecting object found: "(5,6)"
|
||||
* at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler
|
||||
* at com.google.gson.ObjectNavigator.navigateClassFields
|
||||
* ...</pre>
|
||||
*
|
||||
* <h3>Maps as JSON arrays</h3>
|
||||
* An alternative approach taken by this type adapter is to encode maps as
|
||||
* arrays of map entries. Each map entry is a two element array containing a key
|
||||
* and a value. This approach is more flexible because any type can be used as
|
||||
* the map's key; not just strings. But it's also less portable because the
|
||||
* receiver of such JSON must be aware of the map entry convention.
|
||||
*
|
||||
* <p>Register this adapter when you are creating your GSON instance.
|
||||
* <pre> {@code
|
||||
* Gson gson = new GsonBuilder()
|
||||
* .registerTypeAdapter(Map.class, new MapAsArrayTypeAdapter())
|
||||
* .create();
|
||||
* }</pre>
|
||||
* This will change the structure of the JSON emitted by the code above. Now we
|
||||
* get an array. In this case the arrays elements are map entries:
|
||||
* <pre> {@code
|
||||
* [
|
||||
* [
|
||||
* {
|
||||
* "x": 5,
|
||||
* "y": 6
|
||||
* },
|
||||
* "a",
|
||||
* ],
|
||||
* [
|
||||
* {
|
||||
* "x": 8,
|
||||
* "y": 8
|
||||
* },
|
||||
* "b"
|
||||
* ]
|
||||
* ]
|
||||
* }</pre>
|
||||
* This format will serialize and deserialize just fine as long as this adapter
|
||||
* is registered.
|
||||
*
|
||||
* <p>This adapter returns regular JSON objects for maps whose keys are not
|
||||
* complex. A key is complex if its JSON-serialized form is an array or an
|
||||
* object.
|
||||
*/
|
||||
public final class MapAsArrayTypeAdapter
|
||||
implements JsonSerializer<Map<?, ?>>, JsonDeserializer<Map<?, ?>> {
|
||||
|
||||
public Map<?, ?> deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
Map<Object, Object> result = new LinkedHashMap<Object, Object>();
|
||||
Type[] keyAndValueType = typeToTypeArguments(typeOfT);
|
||||
if (json.isJsonArray()) {
|
||||
JsonArray array = json.getAsJsonArray();
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JsonArray entryArray = array.get(i).getAsJsonArray();
|
||||
Object k = context.deserialize(entryArray.get(0), keyAndValueType[0]);
|
||||
Object v = context.deserialize(entryArray.get(1), keyAndValueType[1]);
|
||||
result.put(k, v);
|
||||
}
|
||||
checkSize(array, array.size(), result, result.size());
|
||||
} else {
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
|
||||
Object k = context.deserialize(new JsonPrimitive(entry.getKey()), keyAndValueType[0]);
|
||||
Object v = context.deserialize(entry.getValue(), keyAndValueType[1]);
|
||||
result.put(k, v);
|
||||
}
|
||||
checkSize(object, object.entrySet().size(), result, result.size());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public JsonElement serialize(Map<?, ?> src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
Type[] keyAndValueType = typeToTypeArguments(typeOfSrc);
|
||||
boolean serializeAsArray = false;
|
||||
List<JsonElement> keysAndValues = new ArrayList<JsonElement>();
|
||||
for (Map.Entry<?, ?> entry : src.entrySet()) {
|
||||
JsonElement key = context.serialize(entry.getKey(), keyAndValueType[0]);
|
||||
serializeAsArray |= key.isJsonObject() || key.isJsonArray();
|
||||
keysAndValues.add(key);
|
||||
keysAndValues.add(context.serialize(entry.getValue(), keyAndValueType[1]));
|
||||
}
|
||||
|
||||
if (serializeAsArray) {
|
||||
JsonArray result = new JsonArray();
|
||||
for (int i = 0; i < keysAndValues.size(); i+=2) {
|
||||
JsonArray entryArray = new JsonArray();
|
||||
entryArray.add(keysAndValues.get(i));
|
||||
entryArray.add(keysAndValues.get(i + 1));
|
||||
result.add(entryArray);
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
JsonObject result = new JsonObject();
|
||||
for (int i = 0; i < keysAndValues.size(); i+=2) {
|
||||
result.add(keysAndValues.get(i).getAsString(), keysAndValues.get(i + 1));
|
||||
}
|
||||
checkSize(src, src.size(), result, result.entrySet().size());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private Type[] typeToTypeArguments(Type typeOfT) {
|
||||
if (typeOfT instanceof ParameterizedType) {
|
||||
Type[] typeArguments = ((ParameterizedType) typeOfT).getActualTypeArguments();
|
||||
if (typeArguments.length != 2) {
|
||||
throw new IllegalArgumentException("MapAsArrayTypeAdapter cannot handle " + typeOfT);
|
||||
}
|
||||
return typeArguments;
|
||||
}
|
||||
return new Type[] { Object.class, Object.class };
|
||||
}
|
||||
|
||||
private void checkSize(Object input, int inputSize, Object output, int outputSize) {
|
||||
if (inputSize != outputSize) {
|
||||
throw new JsonSyntaxException("Input size " + inputSize + " != output size " + outputSize
|
||||
+ " for input " + input + " and output " + output);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user