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

@ -1,12 +1,17 @@
package com.massivecraft.factions.engine;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.Bukkit;
@ -62,6 +67,7 @@ import org.bukkit.projectiles.ProjectileSource;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.PlayerRoleComparator;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.TerritoryAccess;
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.MConf;
import com.massivecraft.factions.entity.MPlayerColl;
import com.massivecraft.factions.event.EventFactionsChunkChangeType;
import com.massivecraft.factions.event.EventFactionsChunksChange;
import com.massivecraft.factions.event.EventFactionsFactionShow;
import com.massivecraft.factions.event.EventFactionsPvpDisallowed;
import com.massivecraft.factions.event.EventFactionsPowerChange;
import com.massivecraft.factions.event.EventFactionsPowerChange.PowerChangeReason;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.util.VisualizeUtil;
import com.massivecraft.massivecore.EngineAbstract;
import com.massivecraft.massivecore.PriorityLines;
import com.massivecraft.massivecore.event.EventMassiveCorePlayerLeave;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.money.Money;
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.PlayerUtil;
import com.massivecraft.massivecore.util.TimeDiffUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.util.Txt;
public class EngineMain extends EngineAbstract
@ -105,6 +118,227 @@ public class EngineMain extends EngineAbstract
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
// -------------------------------------------- //