Make argument profile use our usercache

This commit is contained in:
Sotr
2019-04-01 15:40:00 +08:00
parent eda69152f0
commit e616f1b1ed
3 changed files with 139 additions and 16 deletions

View File

@@ -35,7 +35,7 @@ echo "[Akarin] Ready to build"
\cp -rf "$basedir/src/api/pom.xml" "$paperbasedir/Paper-API/" \cp -rf "$basedir/src/api/pom.xml" "$paperbasedir/Paper-API/"
\cp -rf "$basedir/src" "$paperbasedir/Paper-Server/" \cp -rf "$basedir/src" "$paperbasedir/Paper-Server/"
\cp -rf "$basedir/pom.xml" "$paperbasedir/Paper-Server/" \cp -rf "$basedir/pom.xml" "$paperbasedir/Paper-Server/"
mvn clean install mvn clean install -DskipTests
else else
rm -rf Paper-API/src rm -rf Paper-API/src
rm -rf Paper-Server/src rm -rf Paper-Server/src

View File

@@ -58,11 +58,11 @@ public class AkarinUserCache {
protected final Gson gson; protected final Gson gson;
private final File userCacheFile; private final File userCacheFile;
public static boolean isOnlineMode() { private static boolean isOnlineMode() {
return UserCache.isOnlineMode() || (SpigotConfig.bungee && PaperConfig.bungeeOnlineMode); return UserCache.isOnlineMode() || (SpigotConfig.bungee && PaperConfig.bungeeOnlineMode);
} }
public static Date createExpireDate(boolean force) { private static Date createExpireDate(boolean force) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (force || (now - lastWarpExpireDate) > RECREATE_DATE_INTERVAL) { if (force || (now - lastWarpExpireDate) > RECREATE_DATE_INTERVAL) {
lastWarpExpireDate = now; lastWarpExpireDate = now;
@@ -76,15 +76,15 @@ public class AkarinUserCache {
return lastExpireDate; return lastExpireDate;
} }
public static boolean isExpired(UserCacheEntry entry) { private static boolean isExpired(UserCacheEntry entry) {
return System.currentTimeMillis() >= entry.getExpireDate().getTime(); return System.currentTimeMillis() >= entry.getExpireDate().getTime();
} }
public static UserCacheEntry refreshExpireDate(UserCacheEntry entry) { private static UserCacheEntry refreshExpireDate(UserCacheEntry entry) {
return new UserCacheEntry(entry.getProfile(), createExpireDate(true)); return new UserCacheEntry(entry.getProfile(), createExpireDate(true));
} }
public static GameProfile lookup(GameProfileRepository profileRepo, String username, ProfileLookupCallback callback, boolean async) { private static GameProfile lookup(GameProfileRepository profileRepo, String username, ProfileLookupCallback callback, boolean async) {
if (!isOnlineMode()) { if (!isOnlineMode()) {
String usernameKey = username.toLowerCase(Locale.ROOT); String usernameKey = username.toLowerCase(Locale.ROOT);
GameProfile offlineProfile = new GameProfile(EntityHuman.getOfflineUUID(usernameKey), usernameKey); GameProfile offlineProfile = new GameProfile(EntityHuman.getOfflineUUID(usernameKey), usernameKey);
@@ -132,11 +132,11 @@ public class AkarinUserCache {
this.load(); this.load();
} }
GameProfile lookupAndCache(String username, ProfileLookupCallback callback, boolean async) { private GameProfile lookupAndCache(String username, ProfileLookupCallback callback, boolean async) {
return lookupAndCache(username, callback, createExpireDate(false), async); return lookupAndCache(username, callback, createExpireDate(false), async);
} }
GameProfile lookupAndCache(String username, ProfileLookupCallback callback, Date date, boolean async) { private GameProfile lookupAndCache(String username, ProfileLookupCallback callback, Date date, boolean async) {
ProfileLookupCallback callbackHandler = new ProfileLookupCallback() { ProfileLookupCallback callbackHandler = new ProfileLookupCallback() {
@Override @Override
public void onProfileLookupSucceeded(GameProfile gameprofile) { public void onProfileLookupSucceeded(GameProfile gameprofile) {
@@ -195,11 +195,11 @@ public class AkarinUserCache {
return entry == null ? null : entry.getProfile(); return entry == null ? null : entry.getProfile();
} }
void offer(GameProfile profile) { protected void offer(GameProfile profile) {
offer(profile, createExpireDate(false)); offer(profile, createExpireDate(false));
} }
void offer(GameProfile profile, Date date) { protected void offer(GameProfile profile, Date date) {
String keyUsername = isOnlineMode() ? profile.getName() : profile.getName().toLowerCase(Locale.ROOT); String keyUsername = isOnlineMode() ? profile.getName() : profile.getName().toLowerCase(Locale.ROOT);
UserCacheEntry entry = profiles.getIfPresent(keyUsername); UserCacheEntry entry = profiles.getIfPresent(keyUsername);
@@ -219,12 +219,12 @@ public class AkarinUserCache {
this.save(); this.save();
} }
void offer(UserCacheEntry entry) { private void offer(UserCacheEntry entry) {
if (!isExpired(entry)) if (!isExpired(entry))
profiles.put(isOnlineMode() ? entry.getProfile().getName() : entry.getProfile().getName().toLowerCase(Locale.ROOT), entry); profiles.put(isOnlineMode() ? entry.getProfile().getName() : entry.getProfile().getName().toLowerCase(Locale.ROOT), entry);
} }
String[] usernames() { protected String[] usernames() {
return profiles.asMap().keySet().toArray(new String[profiles.asMap().size()]); return profiles.asMap().keySet().toArray(new String[profiles.asMap().size()]);
} }
@@ -250,11 +250,11 @@ public class AkarinUserCache {
} }
} }
public void save() { protected void save() {
save(true); save(true);
} }
public void save(boolean async) { protected void save(boolean async) {
Runnable save = () -> { Runnable save = () -> {
String jsonString = this.gson.toJson(this.entries()); String jsonString = this.gson.toJson(this.entries());
BufferedWriter writer = null; BufferedWriter writer = null;
@@ -278,7 +278,7 @@ public class AkarinUserCache {
save.run(); save.run();
} }
List<UserCacheEntry> entries() { protected List<UserCacheEntry> entries() {
return Lists.newArrayList(profiles.asMap().values()); return Lists.newArrayList(profiles.asMap().values());
} }
} }

View File

@@ -0,0 +1,123 @@
package net.minecraft.server;
import com.google.common.collect.Lists;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class ArgumentProfile implements ArgumentType<ArgumentProfile.a> {
private static final Collection<String> b = Arrays.asList("Player", "0123", "dd12be42-52a9-4a91-a8a1-11c01849e498", "@e");
public static final SimpleCommandExceptionType a = new SimpleCommandExceptionType(new ChatMessage("argument.player.unknown", new Object[0]));
public ArgumentProfile() {}
public static Collection<GameProfile> a(CommandContext<CommandListenerWrapper> commandcontext, String s) throws CommandSyntaxException {
return ((ArgumentProfile.a) commandcontext.getArgument(s, ArgumentProfile.a.class)).getNames((CommandListenerWrapper) commandcontext.getSource());
}
public static ArgumentProfile a() {
return new ArgumentProfile();
}
public ArgumentProfile.a parse(StringReader stringreader) throws CommandSyntaxException {
if (stringreader.canRead() && stringreader.peek() == '@') {
ArgumentParserSelector argumentparserselector = new ArgumentParserSelector(stringreader);
EntitySelector entityselector = argumentparserselector.s();
if (entityselector.b()) {
throw ArgumentEntity.c.create();
} else {
return new ArgumentProfile.b(entityselector);
}
} else {
int i = stringreader.getCursor();
while (stringreader.canRead() && stringreader.peek() != ' ') {
stringreader.skip();
}
String s = stringreader.getString().substring(i, stringreader.getCursor());
return (commandlistenerwrapper) -> {
GameProfile gameprofile = commandlistenerwrapper.getServer().getModernUserCache().acquire(s); // Akarin
if (gameprofile == null) {
throw ArgumentProfile.a.create();
} else {
return Collections.singleton(gameprofile);
}
};
}
}
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> commandcontext, SuggestionsBuilder suggestionsbuilder) {
if (commandcontext.getSource() instanceof ICompletionProvider) {
StringReader stringreader = new StringReader(suggestionsbuilder.getInput());
stringreader.setCursor(suggestionsbuilder.getStart());
ArgumentParserSelector argumentparserselector = new ArgumentParserSelector(stringreader);
try {
argumentparserselector.s();
} catch (CommandSyntaxException commandsyntaxexception) {
;
}
return argumentparserselector.a(suggestionsbuilder, (suggestionsbuilder1) -> {
ICompletionProvider.b((Iterable) ((ICompletionProvider) commandcontext.getSource()).l(), suggestionsbuilder1);
});
} else {
return Suggestions.empty();
}
}
public Collection<String> getExamples() {
return ArgumentProfile.b;
}
public static class b implements ArgumentProfile.a {
private final EntitySelector a;
public b(EntitySelector entityselector) {
this.a = entityselector;
}
public Collection<GameProfile> getNames(CommandListenerWrapper commandlistenerwrapper) throws CommandSyntaxException {
List<EntityPlayer> list = this.a.d(commandlistenerwrapper);
if (list.isEmpty()) {
throw ArgumentEntity.e.create();
} else {
List<GameProfile> list1 = Lists.newArrayList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
EntityPlayer entityplayer = (EntityPlayer) iterator.next();
list1.add(entityplayer.getProfile());
}
return list1;
}
}
}
@FunctionalInterface
public interface a {
Collection<GameProfile> getNames(CommandListenerWrapper commandlistenerwrapper) throws CommandSyntaxException;
}
}