From 3e0a68fd6e1591b69358882eebe25208d11167be Mon Sep 17 00:00:00 2001 From: Brettflan Date: Tue, 6 Nov 2012 09:43:27 -0600 Subject: [PATCH] Performance overhaul sponsored by rockxz3, using his large server's data for reference to help with testing and tuning. Timing numbers below are based on this data set on my test server. * PlayerMoveEvent performance boost (from 0.047ms to 0.015ms in testing, ~313% as fast): now more thrifty in determining whether player has actually moved between chunks before doing anything else; important since this event triggers extremely quickly * PlayerInteractEvent performance boost (from 0.068ms to 0.034ms in testing, ~200% as fast): now ignores left-clicks for interaction checks, since in CraftBukkit 1.4 left-clicks no longer open doors or activate buttons/levers/etc.; not as important as above, but still triggers quite often as people are digging or interacting with blocks * "/f list" command performance boost (from 234ms to 30ms in testing, ~780% as fast): code was getting information for all factions, narrowed it down to only getting info for displayed page range * "/f show" command performance boost (from 132ms to 28ms in testing, ~470% as fast): tweaked the ally & enemy listing code used --- src/com/massivecraft/factions/FPlayer.java | 2 +- src/com/massivecraft/factions/Faction.java | 7 ++++ .../massivecraft/factions/cmd/CmdList.java | 35 ++++++++++++++++++- .../massivecraft/factions/cmd/CmdShow.java | 2 +- .../integration/SpoutMainListener.java | 4 +-- .../listeners/FactionsPlayerListener.java | 35 ++++++++++++------- 6 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index 4823432c..0c73c656 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -451,7 +451,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator { return; } - Faction factionHere = Board.getFactionAt(new FLocation(this)); + Faction factionHere = Board.getFactionAt(this.getLastStoodAt()); String msg = P.p.txt.parse("")+" ~ "+factionHere.getTag(this); if (factionHere.getDescription().length() > 0) { diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index bd75514f..ac9714ac 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -277,6 +277,12 @@ public class Faction extends Entity implements EconomyParticipator } public Map> getFactionTagsPerRelation(RelationParticipator rp) + { + return getFactionTagsPerRelation(rp, false); + } + + // onlyNonNeutral option provides substantial performance boost on large servers for listing only non-neutral factions + public Map> getFactionTagsPerRelation(RelationParticipator rp, boolean onlyNonNeutral) { Map> ret = new HashMap>(); for (Rel rel : Rel.values()) @@ -286,6 +292,7 @@ public class Faction extends Entity implements EconomyParticipator for (Faction faction : Factions.i.get()) { Rel relation = faction.getRelationTo(this); + if (onlyNonNeutral && relation == Rel.NEUTRAL) continue; ret.get(relation).add(faction.getTag(rp)); } return ret; diff --git a/src/com/massivecraft/factions/cmd/CmdList.java b/src/com/massivecraft/factions/cmd/CmdList.java index fd120708..edcd1f27 100644 --- a/src/com/massivecraft/factions/cmd/CmdList.java +++ b/src/com/massivecraft/factions/cmd/CmdList.java @@ -73,6 +73,8 @@ public class CmdList extends FCommand }); ArrayList lines = new ArrayList(); + +/* // this code was really slow on large servers, getting full info for every faction and then only showing 9 of them; rewritten below lines.add(p.txt.parse("Factionless %d online", Factions.i.getNone().getFPlayersWhereOnline(true).size())); for (Faction faction : factionList) { @@ -87,6 +89,37 @@ public class CmdList extends FCommand } sendMessage(p.txt.getPage(lines, this.argAsInt(0, 1), "Faction List")); + */ + + factionList.add(0, Factions.i.getNone()); + + final int pageheight = 9; + int pagenumber = this.argAsInt(0, 1); + int pagecount = (factionList.size() / pageheight) + 1; + int start = (pagenumber - 1) * pageheight; + int end = start + pageheight; + if (end > factionList.size()) + end = factionList.size(); + + lines.add(p.txt.titleize("Faction List "+pagenumber+"/"+pagecount)); + + for (Faction faction : factionList.subList(start, end)) + { + if (faction.isNone()) + { + lines.add(p.txt.parse("Factionless %d online", Factions.i.getNone().getFPlayersWhereOnline(true).size())); + continue; + } + lines.add(p.txt.parse("%s %d/%d online, %d/%d/%d", + faction.getTag(fme), + faction.getFPlayersWhereOnline(true).size(), + faction.getFPlayers().size(), + faction.getLandRounded(), + faction.getPowerRounded(), + faction.getPowerMaxRounded()) + ); + } + + sendMessage(lines); } - } diff --git a/src/com/massivecraft/factions/cmd/CmdShow.java b/src/com/massivecraft/factions/cmd/CmdShow.java index 00a6ad78..db835064 100644 --- a/src/com/massivecraft/factions/cmd/CmdShow.java +++ b/src/com/massivecraft/factions/cmd/CmdShow.java @@ -93,7 +93,7 @@ public class CmdShow extends FCommand String sepparator = p.txt.parse("")+", "; // List the relations to other factions - Map> relationTags = faction.getFactionTagsPerRelation(fme); + Map> relationTags = faction.getFactionTagsPerRelation(fme, true); if (faction.getFlag(FFlag.PEACEFUL)) { diff --git a/src/com/massivecraft/factions/integration/SpoutMainListener.java b/src/com/massivecraft/factions/integration/SpoutMainListener.java index c2499dfa..ff737874 100644 --- a/src/com/massivecraft/factions/integration/SpoutMainListener.java +++ b/src/com/massivecraft/factions/integration/SpoutMainListener.java @@ -72,7 +72,7 @@ public class SpoutMainListener implements Listener if (!sPlayer.isSpoutCraftEnabled() || (Conf.spoutTerritoryDisplaySize <= 0 && ! Conf.spoutTerritoryNoticeShow)) return false; - FLocation here = new FLocation(player); + FLocation here = player.getLastStoodAt(); doAccessInfo(player, sPlayer, here); @@ -89,7 +89,7 @@ public class SpoutMainListener implements Listener private void doLabels(FPlayer player, SpoutPlayer sPlayer, boolean notify) { - FLocation here = new FLocation(player); + FLocation here = player.getLastStoodAt(); Faction factionHere = Board.getFactionAt(here); String tag = factionHere.getColorTo(player).toString() + factionHere.getTag(); diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index db481d8d..fd0c109a 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -52,6 +52,11 @@ public class FactionsPlayerListener implements Listener // Update the lastLoginTime for this fplayer me.setLastLoginTime(System.currentTimeMillis()); + + // Store player's current FLocation and notify them where they are + me.setLastStoodAt(new FLocation(event.getPlayer().getLocation())); + if ( ! SpoutFeatures.updateTerritoryDisplay(me)) + me.sendFactionHereMessage(); } @EventHandler(priority = EventPriority.NORMAL) @@ -67,15 +72,28 @@ public class FactionsPlayerListener implements Listener SpoutFeatures.playerDisconnect(me); } - @EventHandler(priority = EventPriority.NORMAL) + @EventHandler(priority = EventPriority.MONITOR) public void onPlayerMove(PlayerMoveEvent event) { + if (event.isCancelled()) return; + + // quick check to make sure player is moving between chunks; good performance boost + if + ( + event.getFrom().getBlockX() >> 4 == event.getTo().getBlockX() >> 4 + && + event.getFrom().getBlockZ() >> 4 == event.getTo().getBlockZ() >> 4 + && + event.getFrom().getWorld() == event.getTo().getWorld() + ) + return; + Player player = event.getPlayer(); FPlayer me = FPlayers.i.get(player); // Did we change coord? FLocation from = me.getLastStoodAt(); - FLocation to = new FLocation(player.getLocation()); + FLocation to = new FLocation(event.getTo()); if (from.equals(to)) return; @@ -110,7 +128,7 @@ public class FactionsPlayerListener implements Listener if (me.getAutoClaimFor() != null) { - me.attemptClaim(me.getAutoClaimFor(), player.getLocation(), true); + me.attemptClaim(me.getAutoClaimFor(), event.getTo(), true); } } @@ -118,14 +136,12 @@ public class FactionsPlayerListener implements Listener public void onPlayerInteract(PlayerInteractEvent event) { if (event.isCancelled()) return; + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; // only interested in right-clicks as of MC 1.4+; good performance boost Block block = event.getClickedBlock(); Player player = event.getPlayer(); - if (block == null) - { - return; // clicked in air, apparently - } + if (block == null) return; // clicked in air, apparently if ( ! canPlayerUseBlock(player, block, false)) { @@ -150,11 +166,6 @@ public class FactionsPlayerListener implements Listener return; } - if (event.getAction() != Action.RIGHT_CLICK_BLOCK) - { - return; // only interested on right-clicks for below - } - if ( ! playerCanUseItemHere(player, block.getLocation(), event.getMaterial(), false)) { event.setCancelled(true);