From 30f246083898ad93555e30931844c7d2bf13b997 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 12 May 2015 10:42:52 +0200 Subject: [PATCH] A couple of measures to speed up tab completion further. --- .../PredictateStartsWithIgnoreCase.java | 19 +++++++-- .../massivecore/cmd/arg/ARPlayer.java | 27 +------------ .../massivecore/cmd/arg/ARSender.java | 2 +- .../massivecore/cmd/arg/ARSenderEntity.java | 13 +++++-- .../massivecore/cmd/arg/ARSenderId.java | 12 ++++-- .../cmd/arg/ARSenderIdAbstract.java | 39 +++++++++++-------- .../massivecraft/massivecore/util/IdUtil.java | 14 +++++++ 7 files changed, 72 insertions(+), 54 deletions(-) diff --git a/src/com/massivecraft/massivecore/PredictateStartsWithIgnoreCase.java b/src/com/massivecraft/massivecore/PredictateStartsWithIgnoreCase.java index ed72c16c..96d3435d 100644 --- a/src/com/massivecraft/massivecore/PredictateStartsWithIgnoreCase.java +++ b/src/com/massivecraft/massivecore/PredictateStartsWithIgnoreCase.java @@ -1,13 +1,14 @@ package com.massivecraft.massivecore; +// Inspired by: String#regionMatches(ignoreCase, toffset, other, ooffset, len) public class PredictateStartsWithIgnoreCase implements Predictate { // -------------------------------------------- // // FIELDS // -------------------------------------------- // - private final String prefix; - public String getPrefix() { return this.prefix; }; + private final String prefixLower; + private final String prefixUpper; // -------------------------------------------- // // CONSTRUCT @@ -16,7 +17,9 @@ public class PredictateStartsWithIgnoreCase implements Predictate public static PredictateStartsWithIgnoreCase get(String prefix) { return new PredictateStartsWithIgnoreCase(prefix); } public PredictateStartsWithIgnoreCase(String prefix) { - this.prefix = prefix.toLowerCase(); + if (prefix == null) throw new NullPointerException("prefix"); + this.prefixLower = prefix.toLowerCase(); + this.prefixUpper = prefix.toUpperCase(); } // -------------------------------------------- // @@ -27,7 +30,15 @@ public class PredictateStartsWithIgnoreCase implements Predictate public boolean apply(String str) { if (str == null) return false; - return str.toLowerCase().startsWith(this.getPrefix()); + int index = this.prefixLower.length(); + if (str.length() < index) return false; + while (index-- > 0) + { + char c = str.charAt(index); + if (c == prefixLower.charAt(index)) continue; + if (c != prefixUpper.charAt(index)) return false; + } + return true; } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARPlayer.java b/src/com/massivecraft/massivecore/cmd/arg/ARPlayer.java index 98750da9..3348065f 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARPlayer.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARPlayer.java @@ -1,13 +1,9 @@ package com.massivecraft.massivecore.cmd.arg; -import java.util.Collection; -import java.util.Iterator; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.massivecraft.massivecore.store.SenderIdSourceMixinAllSenderIds; import com.massivecraft.massivecore.util.IdUtil; -import com.massivecraft.massivecore.util.MUtil; public class ARPlayer extends ARSenderIdAbstract { @@ -19,7 +15,7 @@ public class ARPlayer extends ARSenderIdAbstract public static ARPlayer get() { return i; } private ARPlayer() { - super(SenderIdSourceMixinAllSenderIds.get()); + super(SenderIdSourceMixinAllSenderIds.get(), true, true); } // -------------------------------------------- // @@ -33,25 +29,4 @@ public class ARPlayer extends ARSenderIdAbstract return IdUtil.getPlayer(senderId); } - @Override - public Collection getTabList(CommandSender sender, String arg) - { - // Super Ret - Collection ret = super.getTabList(sender, arg); - - // Filter Ret - Iterator iter = ret.iterator(); - while (iter.hasNext()) - { - String name = iter.next(); - if ( ! MUtil.isValidPlayerName(name)) - { - iter.remove(); - } - } - - // Return Ret - return ret; - } - } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSender.java b/src/com/massivecraft/massivecore/cmd/arg/ARSender.java index 8cd2f83b..36df8b21 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSender.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSender.java @@ -15,7 +15,7 @@ public class ARSender extends ARSenderIdAbstract public static ARSender get() { return i; } private ARSender() { - super(SenderIdSourceMixinAllSenderIds.get()); + super(SenderIdSourceMixinAllSenderIds.get(), true, false); } // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSenderEntity.java b/src/com/massivecraft/massivecore/cmd/arg/ARSenderEntity.java index 746754fd..667e3cbc 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSenderEntity.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSenderEntity.java @@ -18,9 +18,15 @@ public class ARSenderEntity> extends ARSenderIdAbstrac // CONSTRUCT // -------------------------------------------- // - private ARSenderEntity(SenderColl coll, boolean online) + private ARSenderEntity(SenderColl coll, boolean onlineOnly, boolean playerOnly) { - super(coll, online); + super(coll, onlineOnly, playerOnly); + this.coll = coll; + } + + private ARSenderEntity(SenderColl coll, boolean onlineOnly) + { + super(coll, onlineOnly); this.coll = coll; } @@ -34,7 +40,8 @@ public class ARSenderEntity> extends ARSenderIdAbstrac // GET // -------------------------------------------- // - public static > ARSenderEntity get(SenderColl coll, boolean online) { return new ARSenderEntity(coll, online); } + public static > ARSenderEntity get(SenderColl coll, boolean onlineOnly, boolean playerOnly) { return new ARSenderEntity(coll, onlineOnly, playerOnly); } + public static > ARSenderEntity get(SenderColl coll, boolean onlineOnly) { return new ARSenderEntity(coll, onlineOnly); } public static > ARSenderEntity get(SenderColl coll) { return new ARSenderEntity(coll); } // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSenderId.java b/src/com/massivecraft/massivecore/cmd/arg/ARSenderId.java index 87e49b27..ecfcff6c 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSenderId.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSenderId.java @@ -9,9 +9,14 @@ public class ARSenderId extends ARSenderIdAbstract // CONSTRUCT // -------------------------------------------- // - private ARSenderId(SenderIdSource source, boolean online) + private ARSenderId(SenderIdSource source, boolean onlineOnly, boolean playerOnly) { - super(source, online); + super(source, onlineOnly, playerOnly); + } + + private ARSenderId(SenderIdSource source, boolean onlineOnly) + { + super(source, onlineOnly); } private ARSenderId(SenderIdSource source) @@ -30,7 +35,8 @@ public class ARSenderId extends ARSenderIdAbstract // GET // -------------------------------------------- // - public static ARSenderId get(SenderIdSource source, boolean online) { return new ARSenderId(source, online); } + public static ARSenderId get(SenderIdSource source, boolean onlineOnly, boolean playerOnly) { return new ARSenderId(source, onlineOnly, playerOnly); } + public static ARSenderId get(SenderIdSource source, boolean onlineOnly) { return new ARSenderId(source, onlineOnly); } public static ARSenderId get(SenderIdSource source) { return new ARSenderId(source); } // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSenderIdAbstract.java b/src/com/massivecraft/massivecore/cmd/arg/ARSenderIdAbstract.java index 40751bdf..13db6286 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSenderIdAbstract.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSenderIdAbstract.java @@ -1,12 +1,13 @@ package com.massivecraft.massivecore.cmd.arg; import java.util.Collection; +import java.util.List; import java.util.Set; -import java.util.TreeSet; import org.bukkit.command.CommandSender; import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.store.SenderIdSource; import com.massivecraft.massivecore.util.IdUtil; @@ -19,16 +20,23 @@ public abstract class ARSenderIdAbstract extends ARAbstract // -------------------------------------------- // protected final SenderIdSource source; - protected final boolean online; + protected final boolean onlineOnly; + protected final boolean playerOnly; // -------------------------------------------- // // CONSTRUCT // -------------------------------------------- // - public ARSenderIdAbstract(SenderIdSource source, boolean online) + public ARSenderIdAbstract(SenderIdSource source, boolean onlineOnly, boolean playerOnly) { this.source = source; - this.online = online; + this.onlineOnly = onlineOnly; + this.playerOnly = playerOnly; + } + + public ARSenderIdAbstract(SenderIdSource source, boolean online) + { + this(source, online, false); } public ARSenderIdAbstract(SenderIdSource source) @@ -49,7 +57,7 @@ public abstract class ARSenderIdAbstract extends ARAbstract @Override public String getTypeName() { - if (online) return "online player"; + if (onlineOnly) return "online player"; else return "player"; } @@ -94,28 +102,25 @@ public abstract class ARSenderIdAbstract extends ARAbstract @Override public Collection getTabList(CommandSender sender, String arg) { - // Create Ret - Set ret = new TreeSet(String.CASE_INSENSITIVE_ORDER); - - // Fill Ret Set names; - if (online) + if (onlineOnly) { - names = IdUtil.getOnlineNames(); + names = playerOnly ? IdUtil.getOnlinePlayerNames() : IdUtil.getOnlineNames(); + + List ret = new MassiveList(); for (String name : names) { if ( ! Mixin.canSee(sender, name)) continue; ret.add(name); } + return ret; } else { - names = IdUtil.getAllNames(); - ret.addAll(names); + names = playerOnly ? IdUtil.getAllPlayerNames() : IdUtil.getAllNames(); + + return names; } - - // Return Ret - return ret; } // -------------------------------------------- // @@ -137,7 +142,7 @@ public abstract class ARSenderIdAbstract extends ARAbstract if ( ! coll.contains(senderId)) continue; // ... and the online check passes ... - if (this.online && !Mixin.isOnline(senderId)) continue; + if (this.onlineOnly && !Mixin.isOnline(senderId)) continue; // ... and the result is non null ... T result = this.getResultForSenderId(senderId); diff --git a/src/com/massivecraft/massivecore/util/IdUtil.java b/src/com/massivecraft/massivecore/util/IdUtil.java index b478e25b..f23cbedf 100644 --- a/src/com/massivecraft/massivecore/util/IdUtil.java +++ b/src/com/massivecraft/massivecore/util/IdUtil.java @@ -119,6 +119,9 @@ public class IdUtil implements Listener, Runnable private static Set onlineNames = new ConcurrentSkipListSet(String.CASE_INSENSITIVE_ORDER); public static Set getOnlineNames() { return Collections.unmodifiableSet(onlineNames); } + private static Set onlinePlayerNames = new ConcurrentSkipListSet(String.CASE_INSENSITIVE_ORDER); + public static Set getOnlinePlayerNames() { return Collections.unmodifiableSet(onlinePlayerNames); } + private static Set allIds = new ConcurrentSkipListSet(String.CASE_INSENSITIVE_ORDER); public static Set getAllIds() { return Collections.unmodifiableSet(allIds); } @@ -126,6 +129,9 @@ public class IdUtil implements Listener, Runnable private static Set allNames = new ConcurrentSkipListSet(String.CASE_INSENSITIVE_ORDER); public static Set getAllNames() { return Collections.unmodifiableSet(allNames); } + private static Set allPlayerNames = new ConcurrentSkipListSet(String.CASE_INSENSITIVE_ORDER); + public static Set getAllPlayerNames() { return Collections.unmodifiableSet(allPlayerNames); } + // -------------------------------------------- // // REGISTRY // -------------------------------------------- // @@ -215,7 +221,9 @@ public class IdUtil implements Listener, Runnable if (name == null) throw new NullPointerException("name"); onlineNames.remove(name); + onlinePlayerNames.remove(name); allNames.remove(name); + allPlayerNames.remove(name); IdData data = nameToData.remove(name); if (data == null) return null; @@ -243,10 +251,14 @@ public class IdUtil implements Listener, Runnable if (id != null && name != null) { + boolean player = MUtil.isValidPlayerName(name); + if (online) onlineIds.add(id); allIds.add(id); + if (online && player) onlinePlayerNames.add(name); if (online) onlineNames.add(name); + if (player) allPlayerNames.add(name); allNames.add(name); } } @@ -336,9 +348,11 @@ public class IdUtil implements Listener, Runnable onlineIds.clear(); onlineNames.clear(); + onlinePlayerNames.clear(); allIds.clear(); allNames.clear(); + allPlayerNames.clear(); // Load Datas loadDatas();