From 3f949e18cc5052c36b36fb5455a812be3e7373da Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 30 Dec 2014 00:31:36 -0500 Subject: [PATCH 1/3] Lets not do hundreds of object clones when they are nearly never needed. --- .../massivecraft/factions/cmd/CmdShow.java | 46 ++++++------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdShow.java b/src/main/java/com/massivecraft/factions/cmd/CmdShow.java index d24bcbdb..100f72d5 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdShow.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdShow.java @@ -16,7 +16,7 @@ import java.util.ArrayList; import java.util.List; public class CmdShow extends FCommand { - + private static final int ARBITRARY_LIMIT = 25000; public CmdShow() { this.aliases.add("show"); this.aliases.add("who"); @@ -70,11 +70,6 @@ public class CmdShow extends FCommand { Relation rel = otherFaction.getRelationTo(faction); String s = otherFaction.getTag(fme); if (rel.isAlly()) { - FancyMessage beforeAdd = null; - try { - beforeAdd = currentAllies.clone(); - } catch (CloneNotSupportedException ignored) {} - if (firstAlly) { currentAllies.then(s).tooltip(getToolTips(otherFaction)); } else { @@ -82,16 +77,11 @@ public class CmdShow extends FCommand { } firstAlly = false; - if (currentAllies.toJSONString().length() > Short.MAX_VALUE) { - allies.add(beforeAdd); - currentAllies = new FancyMessage(s).tooltip(getToolTips(otherFaction)); + if (currentAllies.toJSONString().length() > ARBITRARY_LIMIT) { + allies.add(currentAllies); + currentAllies = new FancyMessage(); } } else if (rel.isEnemy()) { - FancyMessage beforeAdd = null; - try { - beforeAdd = currentEnemies.clone(); - } catch (CloneNotSupportedException ignored) {} - if (firstEnemy) { currentEnemies.then(s).tooltip(getToolTips(otherFaction)); } else { @@ -99,9 +89,9 @@ public class CmdShow extends FCommand { } firstEnemy = false; - if (currentEnemies.toJSONString().length() > Short.MAX_VALUE) { - enemies.add(beforeAdd); - currentEnemies = new FancyMessage(s).tooltip(getToolTips(otherFaction)); + if (currentEnemies.toJSONString().length() > ARBITRARY_LIMIT) { + enemies.add(currentEnemies); + currentEnemies = new FancyMessage(); } } } @@ -117,11 +107,6 @@ public class CmdShow extends FCommand { for (FPlayer p : MiscUtil.rankOrder(faction.getFPlayers())) { String name = p.getNameAndTitle(); if (p.isOnline()) { - FancyMessage beforeAdd = null; - try { - beforeAdd = currentOnline.clone(); - } catch (CloneNotSupportedException ignored) {} - if (firstOnline) { currentOnline.then(name).tooltip(getToolTips(p)); } else { @@ -129,16 +114,11 @@ public class CmdShow extends FCommand { } firstOnline = false; - if (currentOnline.toJSONString().length() > Short.MAX_VALUE) { - online.add(beforeAdd); - currentOnline = new FancyMessage(name).tooltip(getToolTips(p)); + if (currentOnline.toJSONString().length() > ARBITRARY_LIMIT) { + online.add(currentOnline); + currentOnline = new FancyMessage(); } } else { - FancyMessage beforeAdd = null; - try { - beforeAdd = currentOffline.clone(); - } catch (CloneNotSupportedException ignored) {} - if (firstOffline) { currentOffline.then(name).tooltip(getToolTips(p)); } else { @@ -146,9 +126,9 @@ public class CmdShow extends FCommand { } firstOffline = false; - if (currentOffline.toJSONString().length() > Short.MAX_VALUE) { - offline.add(beforeAdd); - currentOffline = new FancyMessage(name).tooltip(getToolTips(p)); + if (currentOffline.toJSONString().length() > ARBITRARY_LIMIT) { + offline.add(currentOffline); + currentOffline = new FancyMessage(); } } } From 44dc04e3e17461aee1ecc33dd079fe569dde36a8 Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Fri, 2 Jan 2015 19:19:57 -0500 Subject: [PATCH 2/3] There are lots of unfactioned players, but we shouldn't care about them. --- .../massivecraft/factions/cmd/CmdShow.java | 134 +++++++++--------- 1 file changed, 70 insertions(+), 64 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdShow.java b/src/main/java/com/massivecraft/factions/cmd/CmdShow.java index 100f72d5..4fdf5aa2 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdShow.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdShow.java @@ -17,11 +17,12 @@ import java.util.List; public class CmdShow extends FCommand { private static final int ARBITRARY_LIMIT = 25000; + public CmdShow() { this.aliases.add("show"); this.aliases.add("who"); - //this.requiredArgs.add(""); + // this.requiredArgs.add(""); this.optionalArgs.put("faction tag", "yours"); this.permission = Permission.SHOW.node; @@ -42,7 +43,8 @@ public class CmdShow extends FCommand { } } - // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay + // if economy is enabled, they're not on the bypass list, and this + // command has a cost set, make 'em pay if (!payForCommand(Conf.econCostShow, TL.COMMAND_SHOW_TOSHOW, TL.COMMAND_SHOW_FORSHOW)) { return; } @@ -57,83 +59,87 @@ public class CmdShow extends FCommand { List allies = new ArrayList(); List enemies = new ArrayList(); - FancyMessage currentAllies = new FancyMessage(TL.COMMAND_SHOW_ALLIES.toString()).color(ChatColor.GOLD); - FancyMessage currentEnemies = new FancyMessage(TL.COMMAND_SHOW_ENEMIES.toString()).color(ChatColor.GOLD); + if (!faction.isNone()) { + FancyMessage currentAllies = new FancyMessage(TL.COMMAND_SHOW_ALLIES.toString()).color(ChatColor.GOLD); + FancyMessage currentEnemies = new FancyMessage(TL.COMMAND_SHOW_ENEMIES.toString()).color(ChatColor.GOLD); - boolean firstAlly = true; - boolean firstEnemy = true; - for (Faction otherFaction : Factions.getInstance().getAllFactions()) { - if (otherFaction == faction) { - continue; - } - - Relation rel = otherFaction.getRelationTo(faction); - String s = otherFaction.getTag(fme); - if (rel.isAlly()) { - if (firstAlly) { - currentAllies.then(s).tooltip(getToolTips(otherFaction)); - } else { - currentAllies.then(", " + s).tooltip(getToolTips(otherFaction)); + boolean firstAlly = true; + boolean firstEnemy = true; + for (Faction otherFaction : Factions.getInstance().getAllFactions()) { + if (otherFaction == faction) { + continue; } - firstAlly = false; - if (currentAllies.toJSONString().length() > ARBITRARY_LIMIT) { - allies.add(currentAllies); - currentAllies = new FancyMessage(); - } - } else if (rel.isEnemy()) { - if (firstEnemy) { - currentEnemies.then(s).tooltip(getToolTips(otherFaction)); - } else { - currentEnemies.then(", " + s).tooltip(getToolTips(otherFaction)); - } - firstEnemy = false; + Relation rel = otherFaction.getRelationTo(faction); + String s = otherFaction.getTag(fme); + if (rel.isAlly()) { + if (firstAlly) { + currentAllies.then(s).tooltip(getToolTips(otherFaction)); + } else { + currentAllies.then(", " + s).tooltip(getToolTips(otherFaction)); + } + firstAlly = false; - if (currentEnemies.toJSONString().length() > ARBITRARY_LIMIT) { - enemies.add(currentEnemies); - currentEnemies = new FancyMessage(); + if (currentAllies.toJSONString().length() > ARBITRARY_LIMIT) { + allies.add(currentAllies); + currentAllies = new FancyMessage(); + } + } else if (rel.isEnemy()) { + if (firstEnemy) { + currentEnemies.then(s).tooltip(getToolTips(otherFaction)); + } else { + currentEnemies.then(", " + s).tooltip(getToolTips(otherFaction)); + } + firstEnemy = false; + + if (currentEnemies.toJSONString().length() > ARBITRARY_LIMIT) { + enemies.add(currentEnemies); + currentEnemies = new FancyMessage(); + } } } + allies.add(currentAllies); + enemies.add(currentEnemies); } - allies.add(currentAllies); - enemies.add(currentEnemies); List online = new ArrayList(); List offline = new ArrayList(); - FancyMessage currentOnline = new FancyMessage(TL.COMMAND_SHOW_MEMBERSONLINE.toString()).color(ChatColor.GOLD); - FancyMessage currentOffline = new FancyMessage(TL.COMMAND_SHOW_MEMBERSOFFLINE.toString()).color(ChatColor.GOLD); - boolean firstOnline = true; - boolean firstOffline = true; - for (FPlayer p : MiscUtil.rankOrder(faction.getFPlayers())) { - String name = p.getNameAndTitle(); - if (p.isOnline()) { - if (firstOnline) { - currentOnline.then(name).tooltip(getToolTips(p)); - } else { - currentOnline.then(", " + name).tooltip(getToolTips(p)); - } - firstOnline = false; + if (!faction.isNone()) { + FancyMessage currentOnline = new FancyMessage(TL.COMMAND_SHOW_MEMBERSONLINE.toString()).color(ChatColor.GOLD); + FancyMessage currentOffline = new FancyMessage(TL.COMMAND_SHOW_MEMBERSOFFLINE.toString()).color(ChatColor.GOLD); + boolean firstOnline = true; + boolean firstOffline = true; + for (FPlayer p : MiscUtil.rankOrder(faction.getFPlayers())) { + String name = p.getNameAndTitle(); + if (p.isOnline()) { + if (firstOnline) { + currentOnline.then(name).tooltip(getToolTips(p)); + } else { + currentOnline.then(", " + name).tooltip(getToolTips(p)); + } + firstOnline = false; - if (currentOnline.toJSONString().length() > ARBITRARY_LIMIT) { - online.add(currentOnline); - currentOnline = new FancyMessage(); - } - } else { - if (firstOffline) { - currentOffline.then(name).tooltip(getToolTips(p)); + if (currentOnline.toJSONString().length() > ARBITRARY_LIMIT) { + online.add(currentOnline); + currentOnline = new FancyMessage(); + } } else { - currentOffline.then(", " + name).tooltip(getToolTips(p)); - } - firstOffline = false; + if (firstOffline) { + currentOffline.then(name).tooltip(getToolTips(p)); + } else { + currentOffline.then(", " + name).tooltip(getToolTips(p)); + } + firstOffline = false; - if (currentOffline.toJSONString().length() > ARBITRARY_LIMIT) { - offline.add(currentOffline); - currentOffline = new FancyMessage(); + if (currentOffline.toJSONString().length() > ARBITRARY_LIMIT) { + offline.add(currentOffline); + currentOffline = new FancyMessage(); + } } } + online.add(currentOnline); + offline.add(currentOffline); } - online.add(currentOnline); - offline.add(currentOffline); // Send all at once ;D msg(p.txt.titleize(faction.getTag(fme))); @@ -156,7 +162,7 @@ public class CmdShow extends FCommand { msg(TL.COMMAND_SHOW_LANDVALUE, stringValue, stringRefund); } - //Show bank contents + // Show bank contents if (Conf.bankEnabled) { msg(TL.COMMAND_SHOW_BANKCONTAINS, Econ.moneyString(Econ.getBalance(faction.getAccountId()))); } From 37565463adc8db78f5bb06127273c31f701c9e51 Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Fri, 2 Jan 2015 19:33:34 -0500 Subject: [PATCH 3/3] A few optimizations. --- src/main/java/com/massivecraft/factions/Faction.java | 2 ++ .../java/com/massivecraft/factions/zcore/MCommand.java | 4 ++-- .../massivecraft/factions/zcore/persist/MemoryFaction.java | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/Faction.java b/src/main/java/com/massivecraft/factions/Faction.java index de171488..bc379628 100644 --- a/src/main/java/com/massivecraft/factions/Faction.java +++ b/src/main/java/com/massivecraft/factions/Faction.java @@ -164,6 +164,8 @@ public interface Faction extends EconomyParticipator { public boolean removeFPlayer(FPlayer fplayer); + public int getSize(); + public Set getFPlayers(); public Set getFPlayersWhereOnline(boolean online); diff --git a/src/main/java/com/massivecraft/factions/zcore/MCommand.java b/src/main/java/com/massivecraft/factions/zcore/MCommand.java index ed66ea71..c38a00bc 100644 --- a/src/main/java/com/massivecraft/factions/zcore/MCommand.java +++ b/src/main/java/com/massivecraft/factions/zcore/MCommand.java @@ -307,8 +307,8 @@ public abstract class MCommand { public String replaceFactionTags(String s, Faction faction) { boolean raidable = faction.getLandRounded() > faction.getPower(); FPlayer fLeader = faction.getFPlayerAdmin(); - String online = String.valueOf(faction.getFPlayersWhereOnline(true).size()); - String members = String.valueOf(faction.getFPlayers().size()); + String online = String.valueOf(faction.getOnlinePlayers().size()); + String members = String.valueOf(faction.getSize()); String leader = fLeader == null ? "Server" : fLeader.getName().substring(0, fLeader.getName().length() > 14 ? 13 : fLeader.getName().length()); return s.replace("{power}", String.valueOf(faction.getPowerRounded())).replace("{maxPower}", String.valueOf(faction.getPowerMaxRounded())).replace("{leader}", leader).replace("{chunks}", String.valueOf(faction.getLandRounded())).replace("{raidable}", String.valueOf(raidable)).replace("{warps}", String.valueOf(faction.getWarps().size())).replace("{online}", online).replace("{members}", members); } diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java index a9298cc8..abae38c4 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java @@ -458,6 +458,10 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator { } + public int getSize() { + return fplayers.size(); + } + public Set getFPlayers() { // return a shallow copy of the FPlayer list, to prevent tampering and // concurrency issues @@ -466,6 +470,9 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator { public Set getFPlayersWhereOnline(boolean online) { Set ret = new HashSet(); + if (!this.isNormal()) { + return ret; + } for (FPlayer fplayer : fplayers) { if (fplayer.isOnline() == online) {