Skip to content

Commit 445f928

Browse files
committed
Sendto can now handle serverids short and long fixed syntax and added caching so type.generic isnt calling the proxy or depending on it
1 parent 007b052 commit 445f928

5 files changed

Lines changed: 107 additions & 58 deletions

File tree

loader/src/main/java/net/swofty/loader/Hypixel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ ToProxyChannels.REQUEST_SERVERS_NAME, new JSONObject(),
208208
}
209209

210210
Logger.info("Received server name: " + HypixelConst.getServerName());
211+
212+
new net.swofty.proxyapi.ProxyInformation()
213+
.getAllServersInformation()
214+
.thenAccept(net.swofty.type.generic.utility.ProxyServersCache::update)
215+
.exceptionally(ex -> null);
211216
});
212217
checkProxyConnected(MinecraftServer.getSchedulerManager());
213218

@@ -333,6 +338,7 @@ private static Map<String, String> parseOptionalArgs(String[] args) {
333338
return options;
334339
}
335340

341+
336342
private static void checkProxyConnected(Scheduler scheduler) {
337343
scheduler.submitTask(() -> {
338344
AtomicBoolean responded = new AtomicBoolean(false);

type.generic/src/main/java/net/swofty/type/generic/command/commands/SendToCommand.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import net.minestom.server.command.builder.arguments.ArgumentString;
55
import net.minestom.server.command.builder.suggestion.SuggestionEntry;
66
import net.swofty.commons.ServerType;
7-
import net.swofty.velocity.gamemanager.GameManager;
87
import net.swofty.type.generic.command.CommandParameters;
98
import net.swofty.type.generic.command.HypixelCommand;
109
import net.swofty.type.generic.user.HypixelPlayer;
1110
import net.swofty.type.generic.user.categories.Rank;
11+
import net.swofty.type.generic.utility.ProxyServersCache;
1212

1313
import java.util.UUID;
1414

@@ -24,28 +24,24 @@ public class SendToCommand extends HypixelCommand {
2424
@Override
2525
public void registerUsage(MinestomCommand command) {
2626

27+
2728
ArgumentEnum<ServerType> serverType =
2829
new ArgumentEnum<>("server_type", ServerType.class);
2930

3031

3132
ArgumentString serverId = new ArgumentString("server_id")
3233
.setSuggestionCallback((sender, context, suggestion) -> {
33-
GameManager.getServers().values().forEach(list -> {
34-
list.forEach(gs -> {
35-
suggestion.addEntry(new SuggestionEntry(gs.shortDisplayName()));
36-
suggestion.addEntry(new SuggestionEntry(gs.displayName()));
37-
});
38-
});
34+
for (String s : ProxyServersCache.getSuggestions()) {
35+
suggestion.addEntry(new SuggestionEntry(s));
36+
}
3937
});
4038

4139

4240
command.addSyntax((sender, context) -> {
4341
if (!permissionCheck(sender)) return;
4442

4543
HypixelPlayer player = (HypixelPlayer) sender;
46-
ServerType type = context.get(serverType);
47-
48-
player.sendTo(type, true);
44+
player.sendTo(context.get(serverType), true);
4945
}, serverType);
5046

5147
command.addSyntax((sender, context) -> {
@@ -54,24 +50,12 @@ public void registerUsage(MinestomCommand command) {
5450
HypixelPlayer player = (HypixelPlayer) sender;
5551
String input = context.get(serverId);
5652

57-
58-
GameManager.GameServer gs = GameManager.getFromDisplayName(input);
59-
if (gs != null) {
60-
player.sendTo(gs.internalID(), true);
61-
return;
53+
UUID uuid = ProxyServersCache.resolve(input);
54+
if (uuid != null) {
55+
player.sendTo(uuid, true);
56+
} else {
57+
player.sendMessage("§cServer not found.");
6258
}
63-
64-
65-
try {
66-
UUID uuid = UUID.fromString(input);
67-
gs = GameManager.getFromUUID(uuid);
68-
if (gs != null) {
69-
player.sendTo(gs.internalID(), true);
70-
return;
71-
}
72-
} catch (IllegalArgumentException ignored) {}
73-
74-
player.sendMessage("§cServer not found.");
7559
}, serverId);
7660
}
7761
}

type.generic/src/main/java/net/swofty/type/generic/user/HypixelPlayer.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,37 @@ public PlayerSkin getPlayerSkin() {
127127
return new PlayerSkin(texture, signature);
128128
}
129129

130-
public void sendTo(ServerType type) {
131-
sendTo(type, false);
132-
}
130+
public void sendTo(ServerType type) {
131+
sendTo(type, false);
132+
}
133133

134-
public void sendTo(ServerType type, boolean force) {
135-
ProxyPlayer player = asProxyPlayer();
134+
public void sendTo(ServerType type, boolean force) {
135+
ProxyPlayer player = asProxyPlayer();
136136

137-
if (type == HypixelConst.getTypeLoader().getType() && !force) {
138-
this.teleport(HypixelConst.getTypeLoader().getLoaderValues().spawnPosition().apply(this.getOriginServer()));
139-
return;
140-
}
137+
if (type == HypixelConst.getTypeLoader().getType() && !force) {
138+
this.teleport(HypixelConst.getTypeLoader().getLoaderValues().spawnPosition().apply(this.getOriginServer()));
139+
return;
140+
}
141141

142-
HypixelConst.getTypeLoader().getTablistManager().nullifyCache(this);
142+
HypixelConst.getTypeLoader().getTablistManager().nullifyCache(this);
143143

144-
/*showTitle(Title.title(
145-
Component.text(SkyBlockTexture.FULL_SCREEN_BLACK.toString()),
146-
Component.empty(),
147-
Title.Times.times(Duration.ofSeconds(1), Duration.ofMillis(300), Duration.ZERO)
148-
));*/
144+
player.transferTo(type);
145+
}
149146

150-
player.transferTo(type);
151-
}
147+
// --- NEW: UUID transfer (for m1A / mini2B etc) ---
148+
public void sendTo(UUID serverUuid) {
149+
sendTo(serverUuid, false);
150+
}
151+
152+
public void sendTo(UUID serverUuid, boolean force) {
153+
ProxyPlayer player = asProxyPlayer();
154+
155+
// you can ignore 'force' for now unless you want extra logic;
156+
// keeping signature consistent with ServerType sendTo.
157+
HypixelConst.getTypeLoader().getTablistManager().nullifyCache(this);
158+
159+
// This exists in ProxyPlayer
160+
player.transferToWithIndication(serverUuid);
161+
}
152162

153163
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package net.swofty.type.generic.utility;
2+
3+
import net.swofty.commons.UnderstandableProxyServer;
4+
5+
import java.util.*;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
import java.util.stream.Collectors;
8+
9+
public final class ProxyServersCache {
10+
private ProxyServersCache() {}
11+
12+
private static final Map<String, UUID> nameToUuid = new ConcurrentHashMap<>();
13+
private static volatile List<String> suggestions = List.of();
14+
private static volatile long lastRefreshMs = 0;
15+
16+
public static List<String> getSuggestions() {
17+
return suggestions;
18+
}
19+
20+
public static UUID resolve(String input) {
21+
if (input == null) return null;
22+
return nameToUuid.get(input.toLowerCase(Locale.ROOT));
23+
}
24+
25+
public static boolean shouldRefresh(long ttlMs) {
26+
return (System.currentTimeMillis() - lastRefreshMs) > ttlMs;
27+
}
28+
29+
public static void update(List<UnderstandableProxyServer> servers) {
30+
Map<String, UUID> newMap = new HashMap<>();
31+
List<String> newSuggestions = new ArrayList<>();
32+
33+
for (UnderstandableProxyServer s : servers) {
34+
if (s == null) continue;
35+
36+
UUID uuid = s.uuid();
37+
if (uuid == null) continue;
38+
39+
String shortName = s.shortName();
40+
String name = s.name();
41+
42+
if (shortName != null && !shortName.isBlank()) {
43+
newMap.put(shortName.toLowerCase(Locale.ROOT), uuid);
44+
newSuggestions.add(shortName);
45+
}
46+
if (name != null && !name.isBlank()) {
47+
newMap.put(name.toLowerCase(Locale.ROOT), uuid);
48+
newSuggestions.add(name);
49+
}
50+
}
51+
52+
// de-dupe + sort (short ones first)
53+
newSuggestions = newSuggestions.stream()
54+
.distinct()
55+
.sorted(Comparator.comparingInt(String::length).thenComparing(String::compareToIgnoreCase))
56+
.collect(Collectors.toList());
57+
58+
nameToUuid.clear();
59+
nameToUuid.putAll(newMap);
60+
suggestions = newSuggestions;
61+
lastRefreshMs = System.currentTimeMillis();
62+
}
63+
}

velocity.extension/src/main/java/net/swofty/velocity/gamemanager/GameManager.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,4 @@ public boolean hasEmptySlots() {
162162
return maxPlayers > registeredServer().getPlayersConnected().size();
163163
}
164164
}
165-
public static @Nullable GameServer getFromDisplayName(String name) {
166-
for (ArrayList<GameServer> list : servers.values()) {
167-
for (GameServer gs : list) {
168-
if (gs.displayName().equalsIgnoreCase(name)
169-
|| gs.shortDisplayName().equalsIgnoreCase(name)) {
170-
return gs;
171-
}
172-
}
173-
}
174-
return null;
175-
}
176-
public static boolean hasServer(String name) {
177-
return getFromDisplayName(name) != null;
178-
}
179165
}

0 commit comments

Comments
 (0)