A couple of measures to speed up tab completion further.

This commit is contained in:
Olof Larsson 2015-05-12 10:42:52 +02:00
parent 37000854be
commit 30f2460838
7 changed files with 72 additions and 54 deletions

View File

@ -1,13 +1,14 @@
package com.massivecraft.massivecore; package com.massivecraft.massivecore;
// Inspired by: String#regionMatches(ignoreCase, toffset, other, ooffset, len)
public class PredictateStartsWithIgnoreCase implements Predictate<String> public class PredictateStartsWithIgnoreCase implements Predictate<String>
{ {
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS // FIELDS
// -------------------------------------------- // // -------------------------------------------- //
private final String prefix; private final String prefixLower;
public String getPrefix() { return this.prefix; }; private final String prefixUpper;
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // CONSTRUCT
@ -16,7 +17,9 @@ public class PredictateStartsWithIgnoreCase implements Predictate<String>
public static PredictateStartsWithIgnoreCase get(String prefix) { return new PredictateStartsWithIgnoreCase(prefix); } public static PredictateStartsWithIgnoreCase get(String prefix) { return new PredictateStartsWithIgnoreCase(prefix); }
public PredictateStartsWithIgnoreCase(String 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<String>
public boolean apply(String str) public boolean apply(String str)
{ {
if (str == null) return false; 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;
} }
} }

View File

@ -1,13 +1,9 @@
package com.massivecraft.massivecore.cmd.arg; 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 org.bukkit.entity.Player;
import com.massivecraft.massivecore.store.SenderIdSourceMixinAllSenderIds; import com.massivecraft.massivecore.store.SenderIdSourceMixinAllSenderIds;
import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
public class ARPlayer extends ARSenderIdAbstract<Player> public class ARPlayer extends ARSenderIdAbstract<Player>
{ {
@ -19,7 +15,7 @@ public class ARPlayer extends ARSenderIdAbstract<Player>
public static ARPlayer get() { return i; } public static ARPlayer get() { return i; }
private ARPlayer() private ARPlayer()
{ {
super(SenderIdSourceMixinAllSenderIds.get()); super(SenderIdSourceMixinAllSenderIds.get(), true, true);
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -33,25 +29,4 @@ public class ARPlayer extends ARSenderIdAbstract<Player>
return IdUtil.getPlayer(senderId); return IdUtil.getPlayer(senderId);
} }
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
// Super Ret
Collection<String> ret = super.getTabList(sender, arg);
// Filter Ret
Iterator<String> iter = ret.iterator();
while (iter.hasNext())
{
String name = iter.next();
if ( ! MUtil.isValidPlayerName(name))
{
iter.remove();
}
}
// Return Ret
return ret;
}
} }

View File

@ -15,7 +15,7 @@ public class ARSender extends ARSenderIdAbstract<CommandSender>
public static ARSender get() { return i; } public static ARSender get() { return i; }
private ARSender() private ARSender()
{ {
super(SenderIdSourceMixinAllSenderIds.get()); super(SenderIdSourceMixinAllSenderIds.get(), true, false);
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -18,9 +18,15 @@ public class ARSenderEntity<T extends SenderEntity<T>> extends ARSenderIdAbstrac
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
private ARSenderEntity(SenderColl<T> coll, boolean online) private ARSenderEntity(SenderColl<T> coll, boolean onlineOnly, boolean playerOnly)
{ {
super(coll, online); super(coll, onlineOnly, playerOnly);
this.coll = coll;
}
private ARSenderEntity(SenderColl<T> coll, boolean onlineOnly)
{
super(coll, onlineOnly);
this.coll = coll; this.coll = coll;
} }
@ -34,7 +40,8 @@ public class ARSenderEntity<T extends SenderEntity<T>> extends ARSenderIdAbstrac
// GET // GET
// -------------------------------------------- // // -------------------------------------------- //
public static <T extends SenderEntity<T>> ARSenderEntity<T> get(SenderColl<T> coll, boolean online) { return new ARSenderEntity<T>(coll, online); } public static <T extends SenderEntity<T>> ARSenderEntity<T> get(SenderColl<T> coll, boolean onlineOnly, boolean playerOnly) { return new ARSenderEntity<T>(coll, onlineOnly, playerOnly); }
public static <T extends SenderEntity<T>> ARSenderEntity<T> get(SenderColl<T> coll, boolean onlineOnly) { return new ARSenderEntity<T>(coll, onlineOnly); }
public static <T extends SenderEntity<T>> ARSenderEntity<T> get(SenderColl<T> coll) { return new ARSenderEntity<T>(coll); } public static <T extends SenderEntity<T>> ARSenderEntity<T> get(SenderColl<T> coll) { return new ARSenderEntity<T>(coll); }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -9,9 +9,14 @@ public class ARSenderId extends ARSenderIdAbstract<String>
// CONSTRUCT // 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) private ARSenderId(SenderIdSource source)
@ -30,7 +35,8 @@ public class ARSenderId extends ARSenderIdAbstract<String>
// GET // 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); } public static ARSenderId get(SenderIdSource source) { return new ARSenderId(source); }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -1,12 +1,13 @@
package com.massivecraft.massivecore.cmd.arg; package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.store.SenderIdSource; import com.massivecraft.massivecore.store.SenderIdSource;
import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.IdUtil;
@ -19,16 +20,23 @@ public abstract class ARSenderIdAbstract<T> extends ARAbstract<T>
// -------------------------------------------- // // -------------------------------------------- //
protected final SenderIdSource source; protected final SenderIdSource source;
protected final boolean online; protected final boolean onlineOnly;
protected final boolean playerOnly;
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public ARSenderIdAbstract(SenderIdSource source, boolean online) public ARSenderIdAbstract(SenderIdSource source, boolean onlineOnly, boolean playerOnly)
{ {
this.source = source; 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) public ARSenderIdAbstract(SenderIdSource source)
@ -49,7 +57,7 @@ public abstract class ARSenderIdAbstract<T> extends ARAbstract<T>
@Override @Override
public String getTypeName() public String getTypeName()
{ {
if (online) return "online player"; if (onlineOnly) return "online player";
else return "player"; else return "player";
} }
@ -94,28 +102,25 @@ public abstract class ARSenderIdAbstract<T> extends ARAbstract<T>
@Override @Override
public Collection<String> getTabList(CommandSender sender, String arg) public Collection<String> getTabList(CommandSender sender, String arg)
{ {
// Create Ret
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
// Fill Ret
Set<String> names; Set<String> names;
if (online) if (onlineOnly)
{ {
names = IdUtil.getOnlineNames(); names = playerOnly ? IdUtil.getOnlinePlayerNames() : IdUtil.getOnlineNames();
List<String> ret = new MassiveList<String>();
for (String name : names) for (String name : names)
{ {
if ( ! Mixin.canSee(sender, name)) continue; if ( ! Mixin.canSee(sender, name)) continue;
ret.add(name); ret.add(name);
} }
return ret;
} }
else else
{ {
names = IdUtil.getAllNames(); names = playerOnly ? IdUtil.getAllPlayerNames() : IdUtil.getAllNames();
ret.addAll(names);
return names;
} }
// Return Ret
return ret;
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -137,7 +142,7 @@ public abstract class ARSenderIdAbstract<T> extends ARAbstract<T>
if ( ! coll.contains(senderId)) continue; if ( ! coll.contains(senderId)) continue;
// ... and the online check passes ... // ... 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 ... // ... and the result is non null ...
T result = this.getResultForSenderId(senderId); T result = this.getResultForSenderId(senderId);

View File

@ -119,6 +119,9 @@ public class IdUtil implements Listener, Runnable
private static Set<String> onlineNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER); private static Set<String> onlineNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getOnlineNames() { return Collections.unmodifiableSet(onlineNames); } public static Set<String> getOnlineNames() { return Collections.unmodifiableSet(onlineNames); }
private static Set<String> onlinePlayerNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getOnlinePlayerNames() { return Collections.unmodifiableSet(onlinePlayerNames); }
private static Set<String> allIds = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER); private static Set<String> allIds = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getAllIds() { return Collections.unmodifiableSet(allIds); } public static Set<String> getAllIds() { return Collections.unmodifiableSet(allIds); }
@ -126,6 +129,9 @@ public class IdUtil implements Listener, Runnable
private static Set<String> allNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER); private static Set<String> allNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getAllNames() { return Collections.unmodifiableSet(allNames); } public static Set<String> getAllNames() { return Collections.unmodifiableSet(allNames); }
private static Set<String> allPlayerNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getAllPlayerNames() { return Collections.unmodifiableSet(allPlayerNames); }
// -------------------------------------------- // // -------------------------------------------- //
// REGISTRY // REGISTRY
// -------------------------------------------- // // -------------------------------------------- //
@ -215,7 +221,9 @@ public class IdUtil implements Listener, Runnable
if (name == null) throw new NullPointerException("name"); if (name == null) throw new NullPointerException("name");
onlineNames.remove(name); onlineNames.remove(name);
onlinePlayerNames.remove(name);
allNames.remove(name); allNames.remove(name);
allPlayerNames.remove(name);
IdData data = nameToData.remove(name); IdData data = nameToData.remove(name);
if (data == null) return null; if (data == null) return null;
@ -243,10 +251,14 @@ public class IdUtil implements Listener, Runnable
if (id != null && name != null) if (id != null && name != null)
{ {
boolean player = MUtil.isValidPlayerName(name);
if (online) onlineIds.add(id); if (online) onlineIds.add(id);
allIds.add(id); allIds.add(id);
if (online && player) onlinePlayerNames.add(name);
if (online) onlineNames.add(name); if (online) onlineNames.add(name);
if (player) allPlayerNames.add(name);
allNames.add(name); allNames.add(name);
} }
} }
@ -336,9 +348,11 @@ public class IdUtil implements Listener, Runnable
onlineIds.clear(); onlineIds.clear();
onlineNames.clear(); onlineNames.clear();
onlinePlayerNames.clear();
allIds.clear(); allIds.clear();
allNames.clear(); allNames.clear();
allPlayerNames.clear();
// Load Datas // Load Datas
loadDatas(); loadDatas();