Eventified /f f and better flag interest detection.

This commit is contained in:
Olof Larsson 2014-10-15 14:35:50 +02:00
parent 6709363f99
commit 7696265eef
7 changed files with 335 additions and 148 deletions

View File

@ -22,4 +22,26 @@ public class Const
public static final char[] MAP_KEY_CHARS = "\\/#?$%=&^ABCDEFGHJKLMNOPQRSTUVWXYZ1234567890abcdeghjmnopqrsuvwxyz".toCharArray(); public static final char[] MAP_KEY_CHARS = "\\/#?$%=&^ABCDEFGHJKLMNOPQRSTUVWXYZ1234567890abcdeghjmnopqrsuvwxyz".toCharArray();
// SHOW
public static final String SHOW_ID_FACTION_ID = BASENAME_ + "id";
public static final String SHOW_ID_FACTION_DESCRIPTION = BASENAME_ + "description";
public static final String SHOW_ID_FACTION_AGE = BASENAME_ + "age";
public static final String SHOW_ID_FACTION_FLAGS = BASENAME_ + "flags";
public static final String SHOW_ID_FACTION_POWER = BASENAME_ + "power";
public static final String SHOW_ID_FACTION_LANDVALUES = BASENAME_ + "landvalue";
public static final String SHOW_ID_FACTION_BANK = BASENAME_ + "bank";
public static final String SHOW_ID_FACTION_RELATIONS = BASENAME_ + "relations";
public static final String SHOW_ID_FACTION_FOLLOWERS = BASENAME_ + "followers";
public static final int SHOW_PRIORITY_FACTION_ID = 1000;
public static final int SHOW_PRIORITY_FACTION_DESCRIPTION = 2000;
public static final int SHOW_PRIORITY_FACTION_AGE = 3000;
public static final int SHOW_PRIORITY_FACTION_FLAGS = 4000;
public static final int SHOW_PRIORITY_FACTION_POWER = 5000;
public static final int SHOW_PRIORITY_FACTION_LANDVALUES = 6000;
public static final int SHOW_PRIORITY_FACTION_BANK = 7000;
public static final int SHOW_PRIORITY_FACTION_RELATIONS = 8000;
public static final int SHOW_PRIORITY_FACTION_FOLLOWERS = 9000;
} }

View File

@ -1,26 +1,13 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import java.util.ArrayList; import java.util.TreeSet;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.massivecraft.factions.cmd.arg.ARFaction; import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.event.EventFactionsChunkChangeType; import com.massivecraft.factions.event.EventFactionsFactionShow;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.Perm; import com.massivecraft.factions.Perm;
import com.massivecraft.factions.PlayerRoleComparator; import com.massivecraft.massivecore.PriorityLines;
import com.massivecraft.factions.Rel;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm; import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.money.Money;
import com.massivecraft.massivecore.util.TimeDiffUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsFaction extends FactionsCommand public class CmdFactionsFaction extends FactionsCommand
@ -52,127 +39,19 @@ public class CmdFactionsFaction extends FactionsCommand
Faction faction = this.arg(0, ARFaction.get(), msenderFaction); Faction faction = this.arg(0, ARFaction.get(), msenderFaction);
if (faction == null) return; if (faction == null) return;
// Data precalculation // Event
//boolean none = faction.isNone(); EventFactionsFactionShow event = new EventFactionsFactionShow(sender, faction);
boolean normal = faction.isNormal(); event.run();
if (event.isCancelled()) return;
// INFO: Title // Title
msg(Txt.titleize("Faction " + faction.getName(msender))); msg(Txt.titleize("Faction " + faction.getName(msender)));
// INFO: Id (admin mode output only) // Lines
if (msender.isUsingAdminMode()) TreeSet<PriorityLines> priorityLiness = new TreeSet<PriorityLines>(event.getIdPriorityLiness().values());
for (PriorityLines priorityLines : priorityLiness)
{ {
msg("<a>ID: <i>%s", faction.getId()); sendMessage(priorityLines.getLines());
}
// INFO: Description
msg("<a>Description: <i>%s", faction.getDescription());
if (normal)
{
// INFO: Age
long ageMillis = faction.getCreatedAtMillis() - System.currentTimeMillis();
LinkedHashMap<TimeUnit, Long> ageUnitcounts = TimeDiffUtil.limit(TimeDiffUtil.unitcounts(ageMillis, TimeUnit.getAllButMillis()), 3);
String ageString = TimeDiffUtil.formatedVerboose(ageUnitcounts, "<i>");
msg("<a>Age: <i>%s", ageString);
// INFO: Open
// TODO: Why hardcode displaying the open flag only? We should rather display everything publicly editable.
msg("<a>Open: <i>"+(faction.getFlag(MFlag.getFlagOpen()) ? "<lime>Yes<i>, anyone can join" : "<rose>No<i>, only invited people can join"));
// INFO: Power
double powerBoost = faction.getPowerBoost();
String boost = (powerBoost == 0.0) ? "" : (powerBoost > 0.0 ? " (bonus: " : " (penalty: ") + powerBoost + ")";
msg("<a>Land / Power / Maxpower: <i> %d/%d/%d %s", faction.getLandCount(), faction.getPowerRounded(), faction.getPowerMaxRounded(), boost);
// show the land value
if (Econ.isEnabled())
{
long landCount = faction.getLandCount();
for (EventFactionsChunkChangeType type : EventFactionsChunkChangeType.values())
{
Double money = MConf.get().econChunkCost.get(type);
if (money == null) continue;
if (money == 0D) continue;
money *= landCount;
String word = null;
if (money > 0)
{
word = "cost";
}
else
{
word = "reward";
money *= -1;
}
msg("<a>Total land %s %s: <i>%s", type.toString().toLowerCase(), word, Money.format(money));
}
// Show bank contents
if (MConf.get().bankEnabled)
{
msg("<a>Bank contains: <i>"+Money.format(Money.get(faction)));
}
}
// Display important flags
// TODO: Find the non default flags, and display them instead.
if (faction.getFlag(MFlag.getFlagPermanent()))
{
msg("<a>This faction is permanent - remaining even with no followers.");
}
if (faction.getFlag(MFlag.getFlagPeaceful()))
{
msg("<a>This faction is peaceful - in truce with everyone.");
}
}
String sepparator = Txt.parse("<i>")+", ";
// List the relations to other factions
Map<Rel, List<String>> relationNames = faction.getFactionNamesPerRelation(msender, true);
if (faction.getFlag(MFlag.getFlagPeaceful()))
{
sendMessage(Txt.parse("<a>In Truce with:<i> *everyone*"));
}
else
{
sendMessage(Txt.parse("<a>In Truce with: ") + Txt.implode(relationNames.get(Rel.TRUCE), sepparator));
}
sendMessage(Txt.parse("<a>Allies: ") + Txt.implode(relationNames.get(Rel.ALLY), sepparator));
sendMessage(Txt.parse("<a>Enemies: ") + Txt.implode(relationNames.get(Rel.ENEMY), sepparator));
// List the followers...
List<String> followerNamesOnline = new ArrayList<String>();
List<String> followerNamesOffline = new ArrayList<String>();
List<MPlayer> followers = faction.getMPlayers();
Collections.sort(followers, PlayerRoleComparator.get());
for (MPlayer follower : followers)
{
if (follower.isOnline() && Mixin.canSee(sender, follower.getId()))
{
followerNamesOnline.add(follower.getNameAndTitle(msender));
}
else if (normal)
{
// For the non-faction we skip the offline members since they are far to many (infinate almost)
followerNamesOffline.add(follower.getNameAndTitle(msender));
}
}
sendMessage(Txt.parse("<a>Followers online (%s): ", followerNamesOnline.size()) + Txt.implode(followerNamesOnline, sepparator));
if (normal)
{
sendMessage(Txt.parse("<a>Followers offline (%s): ", followerNamesOffline.size()) + Txt.implode(followerNamesOffline, sepparator));
} }
} }

View File

@ -1,12 +1,17 @@
package com.massivecraft.factions.engine; package com.massivecraft.factions.engine;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -62,6 +67,7 @@ import org.bukkit.projectiles.ProjectileSource;
import com.massivecraft.factions.Const; import com.massivecraft.factions.Const;
import com.massivecraft.factions.Factions; import com.massivecraft.factions.Factions;
import com.massivecraft.factions.PlayerRoleComparator;
import com.massivecraft.factions.Rel; import com.massivecraft.factions.Rel;
import com.massivecraft.factions.TerritoryAccess; import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.factions.entity.BoardColl; import com.massivecraft.factions.entity.BoardColl;
@ -72,17 +78,24 @@ import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MConf; import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MPlayerColl; import com.massivecraft.factions.entity.MPlayerColl;
import com.massivecraft.factions.event.EventFactionsChunkChangeType;
import com.massivecraft.factions.event.EventFactionsChunksChange; import com.massivecraft.factions.event.EventFactionsChunksChange;
import com.massivecraft.factions.event.EventFactionsFactionShow;
import com.massivecraft.factions.event.EventFactionsPvpDisallowed; import com.massivecraft.factions.event.EventFactionsPvpDisallowed;
import com.massivecraft.factions.event.EventFactionsPowerChange; import com.massivecraft.factions.event.EventFactionsPowerChange;
import com.massivecraft.factions.event.EventFactionsPowerChange.PowerChangeReason; import com.massivecraft.factions.event.EventFactionsPowerChange.PowerChangeReason;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.util.VisualizeUtil; import com.massivecraft.factions.util.VisualizeUtil;
import com.massivecraft.massivecore.EngineAbstract; import com.massivecraft.massivecore.EngineAbstract;
import com.massivecraft.massivecore.PriorityLines;
import com.massivecraft.massivecore.event.EventMassiveCorePlayerLeave; import com.massivecraft.massivecore.event.EventMassiveCorePlayerLeave;
import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.money.Money;
import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.PlayerUtil; import com.massivecraft.massivecore.util.PlayerUtil;
import com.massivecraft.massivecore.util.TimeDiffUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public class EngineMain extends EngineAbstract public class EngineMain extends EngineAbstract
@ -105,6 +118,227 @@ public class EngineMain extends EngineAbstract
return Factions.get(); return Factions.get();
} }
// -------------------------------------------- //
// FACTION SHOW
// -------------------------------------------- //
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onFactionShow(EventFactionsFactionShow event)
{
final int tableCols = 4;
final CommandSender sender = event.getSender();
final MPlayer msender = event.getMSender();
final Faction faction = event.getFaction();
final boolean normal = faction.isNormal();
final Map<String, PriorityLines> idPriorityLiness = event.getIdPriorityLiness();
final boolean peaceful = faction.getFlag(MFlag.getFlagPeaceful());
// ID
if (msender.isUsingAdminMode())
{
show(idPriorityLiness, Const.SHOW_ID_FACTION_ID, Const.SHOW_PRIORITY_FACTION_ID, "ID", faction.getId());
}
// DESCRIPTION
show(idPriorityLiness, Const.SHOW_ID_FACTION_DESCRIPTION, Const.SHOW_PRIORITY_FACTION_DESCRIPTION, "Description", faction.getDescription());
// SECTION: NORMAL
if (normal)
{
// AGE
long ageMillis = faction.getCreatedAtMillis() - System.currentTimeMillis();
LinkedHashMap<TimeUnit, Long> ageUnitcounts = TimeDiffUtil.limit(TimeDiffUtil.unitcounts(ageMillis, TimeUnit.getAllButMillis()), 3);
String ageDesc = TimeDiffUtil.formatedVerboose(ageUnitcounts, "<i>");
show(idPriorityLiness, Const.SHOW_ID_FACTION_AGE, Const.SHOW_PRIORITY_FACTION_AGE, "Age", ageDesc);
// FLAGS
// We display all editable and non default ones. The rest we skip.
List<String> flagDescs = new LinkedList<String>();
for (Entry<MFlag, Boolean> entry : faction.getFlags().entrySet())
{
final MFlag mflag = entry.getKey();
if (mflag == null) continue;
final Boolean value = entry.getValue();
if (value == null) continue;
if ( ! mflag.isInteresting(value)) continue;
String flagDesc = Txt.parse(value ? "<g>" : "<b>") + mflag.getName();
flagDescs.add(flagDesc);
}
String flagsDesc = Txt.parse("<silver><italic>default");
if ( ! flagDescs.isEmpty())
{
flagsDesc = Txt.implode(flagDescs, Txt.parse(" <i>| "));
}
show(idPriorityLiness, Const.SHOW_ID_FACTION_FLAGS, Const.SHOW_PRIORITY_FACTION_FLAGS, "Flags", flagsDesc);
// POWER
double powerBoost = faction.getPowerBoost();
String boost = (powerBoost == 0.0) ? "" : (powerBoost > 0.0 ? " (bonus: " : " (penalty: ") + powerBoost + ")";
String powerDesc = Txt.parse("%d/%d/%d%s", faction.getLandCount(), faction.getPowerRounded(), faction.getPowerMaxRounded(), boost);
show(idPriorityLiness, Const.SHOW_ID_FACTION_POWER, Const.SHOW_PRIORITY_FACTION_POWER, "Land / Power / Maxpower", powerDesc);
// SECTION: ECON
if (Econ.isEnabled())
{
// LANDVALUES
List<String> landvalueLines = new LinkedList<String>();
long landCount = faction.getLandCount();
for (EventFactionsChunkChangeType type : EventFactionsChunkChangeType.values())
{
Double money = MConf.get().econChunkCost.get(type);
if (money == null) continue;
if (money == 0) continue;
System.out.println("money: "+money);
money *= landCount;
String word = "Cost";
if (money <= 0)
{
word = "Reward";
money *= -1;
}
String key = Txt.parse("Total Land %s %s", type.toString().toLowerCase(), word);
String value = Money.format(money);
String line = show(key, value);
landvalueLines.add(line);
}
idPriorityLiness.put(Const.SHOW_ID_FACTION_LANDVALUES, new PriorityLines(Const.SHOW_PRIORITY_FACTION_LANDVALUES, landvalueLines));
// BANK
if (MConf.get().bankEnabled)
{
String bankDesc = Money.format(Money.get(faction));
show(idPriorityLiness, Const.SHOW_ID_FACTION_BANK, Const.SHOW_PRIORITY_FACTION_BANK, "Bank", bankDesc);
}
}
}
// RELATIONS
List<String> relationLines = new ArrayList<String>();
String none = Txt.parse("<silver><italic>none");
String everyone = MConf.get().colorTruce.toString() + Txt.parse("<italic>*EVERYONE*");
Set<Rel> rels = EnumSet.of(Rel.TRUCE, Rel.ALLY, Rel.ENEMY);
Map<Rel, List<String>> relNames = faction.getRelationNames(msender, rels, true);
for (Entry<Rel, List<String>> entry : relNames.entrySet())
{
Rel rel = entry.getKey();
List<String> names = entry.getValue();
String header = Txt.parse("<a>Relation %s%s<a> (%d):", rel.getColor().toString(), Txt.getNicedEnum(rel), names.size());
relationLines.add(header);
if (rel == Rel.TRUCE && peaceful)
{
relationLines.add(everyone);
}
else
{
if (names.isEmpty())
{
relationLines.add(none);
}
else
{
relationLines.addAll(table(names, tableCols));
}
}
}
idPriorityLiness.put(Const.SHOW_ID_FACTION_RELATIONS, new PriorityLines(Const.SHOW_PRIORITY_FACTION_RELATIONS, relationLines));
// FOLLOWERS
List<String> followerLines = new ArrayList<String>();
List<String> followerNamesOnline = new ArrayList<String>();
List<String> followerNamesOffline = new ArrayList<String>();
List<MPlayer> followers = faction.getMPlayers();
Collections.sort(followers, PlayerRoleComparator.get());
for (MPlayer follower : followers)
{
if (follower.isOnline() && Mixin.canSee(sender, follower.getId()))
{
followerNamesOnline.add(follower.getNameAndTitle(msender));
}
else if (normal)
{
// For the non-faction we skip the offline members since they are far to many (infinite almost)
followerNamesOffline.add(follower.getNameAndTitle(msender));
}
}
String headerOnline = Txt.parse("<a>Followers Online (%s):", followerNamesOnline.size());
followerLines.add(headerOnline);
if (followerNamesOnline.isEmpty())
{
followerLines.add(none);
}
else
{
followerLines.addAll(table(followerNamesOnline, tableCols));
}
if (normal)
{
String headerOffline = Txt.parse("<a>Followers Offline (%s):", followerNamesOffline.size());
followerLines.add(headerOffline);
if (followerNamesOffline.isEmpty())
{
followerLines.add(none);
}
else
{
followerLines.addAll(table(followerNamesOffline, tableCols));
}
}
idPriorityLiness.put(Const.SHOW_ID_FACTION_FOLLOWERS, new PriorityLines(Const.SHOW_PRIORITY_FACTION_FOLLOWERS, followerLines));
}
public static String show(String key, String value)
{
return Txt.parse("<a>%s: <i>%s", key, value);
}
public static PriorityLines show(int priority, String key, String value)
{
return new PriorityLines(priority, show(key, value));
}
public static void show(Map<String, PriorityLines> idPriorityLiness, String id, int priority, String key, String value)
{
idPriorityLiness.put(id, show(priority, key, value));
}
public static List<String> table(List<String> strings, int cols)
{
List<String> ret = new ArrayList<String>();
StringBuilder row = new StringBuilder();
int count = 0;
Iterator<String> iter = strings.iterator();
while (iter.hasNext())
{
String string = iter.next();
row.append(string);
count++;
if (iter.hasNext() && count != cols)
{
row.append(Txt.parse(" <i>| "));
}
else
{
ret.add(row.toString());
row = new StringBuilder();
count = 0;
}
}
return ret;
}
// -------------------------------------------- // // -------------------------------------------- //
// UPDATE LAST ACTIVITY // UPDATE LAST ACTIVITY
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -562,27 +562,29 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
this.setRelationWish(faction.getId(), rel); this.setRelationWish(faction.getId(), rel);
} }
// TODO: What is this and where is it used? public Map<Rel, List<String>> getRelationNames(RelationParticipator rp, Set<Rel> rels, boolean skipPeaceful)
public Map<Rel, List<String>> getFactionNamesPerRelation(RelationParticipator rp)
{ {
return getFactionNamesPerRelation(rp, false); // Create Ret
} Map<Rel, List<String>> ret = new LinkedHashMap<Rel, List<String>>();
for (Rel rel : rels)
// onlyNonNeutral option provides substantial performance boost on large servers for listing only non-neutral factions
public Map<Rel, List<String>> getFactionNamesPerRelation(RelationParticipator rp, boolean onlyNonNeutral)
{
Map<Rel, List<String>> ret = new HashMap<Rel, List<String>>();
for (Rel rel : Rel.values())
{ {
ret.put(rel, new ArrayList<String>()); ret.put(rel, new ArrayList<String>());
} }
for (Faction faction : FactionColl.get().getAll()) for (Faction faction : FactionColl.get().getAll())
{ {
Rel relation = faction.getRelationTo(this); if (skipPeaceful && faction.getFlag(MFlag.getFlagPeaceful())) continue;
if (onlyNonNeutral && relation == Rel.NEUTRAL) continue;
ret.get(relation).add(faction.getName(rp)); Rel rel = faction.getRelationTo(this);
List<String> names = ret.get(rel);
if (names == null) continue;
String name = faction.getName(rp);
names.add(name);
} }
// Return Ret
return ret; return ret;
} }

View File

@ -188,6 +188,13 @@ public class MFlag extends Entity<MFlag> implements Prioritized, Registerable
// EXTRAS // EXTRAS
// -------------------------------------------- // // -------------------------------------------- //
public boolean isInteresting(boolean value)
{
if ( ! this.isVisible()) return false;
if (this.isEditable()) return true;
return this.isStandard() != value;
}
public String getStateInfo(boolean value, boolean withDesc) public String getStateInfo(boolean value, boolean withDesc)
{ {
String valueDesc = value ? "<g>YES" : "<b>NOO"; String valueDesc = value ? "<g>YES" : "<b>NOO";

View File

@ -0,0 +1,43 @@
package com.massivecraft.factions.event;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.massivecore.PriorityLines;
public class EventFactionsFactionShow extends EventFactionsAbstractSender
{
// -------------------------------------------- //
// REQUIRED EVENT CODE
// -------------------------------------------- //
private static final HandlerList handlers = new HandlerList();
@Override public HandlerList getHandlers() { return handlers; }
public static HandlerList getHandlerList() { return handlers; }
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final Faction faction;
public Faction getFaction() { return this.faction; }
private final Map<String, PriorityLines> idPriorityLiness;
public Map<String, PriorityLines> getIdPriorityLiness() { return this.idPriorityLiness; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public EventFactionsFactionShow(CommandSender sender, Faction faction)
{
super(sender);
this.faction = faction;
this.idPriorityLiness = new HashMap<String, PriorityLines>();
}
}

View File

@ -783,7 +783,7 @@ public class EngineDynmap extends EngineAbstract
ret = ret.replace("%" + flag + ".color%", color); // monsters (red or green) ret = ret.replace("%" + flag + ".color%", color); // monsters (red or green)
ret = ret.replace("%" + flag + ".boolcolor%", boolcolor); // true (red or green) ret = ret.replace("%" + flag + ".boolcolor%", boolcolor); // true (red or green)
if (!mflag.isVisible()) continue; if ( ! mflag.isInteresting(value)) continue;
flagMapParts.add(flag + ": " + boolcolor); flagMapParts.add(flag + ": " + boolcolor);
flagTableParts.add(color); flagTableParts.add(color);
} }