Render Rank icon only for client users
This commit is contained in:
@@ -4,10 +4,11 @@ package de.fullrisk.citrusDev.client;
|
|||||||
import de.fullrisk.citrusDev.Config;
|
import de.fullrisk.citrusDev.Config;
|
||||||
import de.fullrisk.citrusDev.UI.*;
|
import de.fullrisk.citrusDev.UI.*;
|
||||||
import de.fullrisk.citrusDev.UIHelper.DraggableHud;
|
import de.fullrisk.citrusDev.UIHelper.DraggableHud;
|
||||||
import io.wispforest.owo.ui.hud.Hud;
|
import de.fullrisk.citrusDev.helper.RankHelper;
|
||||||
import net.fabricmc.api.ClientModInitializer;
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||||
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
|
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
|
||||||
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
||||||
import net.minecraft.client.KeyMapping;
|
import net.minecraft.client.KeyMapping;
|
||||||
import net.minecraft.resources.Identifier;
|
import net.minecraft.resources.Identifier;
|
||||||
import org.lwjgl.glfw.GLFW;
|
import org.lwjgl.glfw.GLFW;
|
||||||
@@ -40,6 +41,10 @@ public class CitrusDevClient implements ClientModInitializer {
|
|||||||
category
|
category
|
||||||
));
|
));
|
||||||
|
|
||||||
|
ClientPlayConnectionEvents.JOIN.register((sender, client1, player) -> {
|
||||||
|
RankHelper.fetchAllRanks();
|
||||||
|
});
|
||||||
|
|
||||||
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||||
|
|
||||||
if (client.player != null && !client.isPaused()) {
|
if (client.player != null && !client.isPaused()) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package de.fullrisk.citrusDev.helper;
|
package de.fullrisk.citrusDev.helper;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
import de.fullrisk.citrusDev.client.CitrusDevClient;
|
import de.fullrisk.citrusDev.client.CitrusDevClient;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
|
|
||||||
@@ -7,21 +9,22 @@ import java.net.URI;
|
|||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class RankHelper {
|
public class RankHelper {
|
||||||
public static final ConcurrentHashMap<UUID, String> rankCache = new ConcurrentHashMap<>();
|
public static final ConcurrentHashMap<UUID, String> rankCache = new ConcurrentHashMap<>();
|
||||||
private static final HttpClient client = HttpClient.newHttpClient();
|
private static final HttpClient client = HttpClient.newBuilder()
|
||||||
|
.connectTimeout(Duration.ofSeconds(10)).build();
|
||||||
|
|
||||||
|
private static boolean fetched = false;
|
||||||
|
|
||||||
public static String getRankIcon(UUID uuid) {
|
public static String getRankIcon(UUID uuid) {
|
||||||
String rank = rankCache.get(uuid);
|
String rank = rankCache.get(uuid);
|
||||||
|
|
||||||
if (rank == null) {
|
if (rank == null || rank.equals("none")) {
|
||||||
rankCache.put(uuid, "loading");
|
return "";
|
||||||
fetchRank(uuid);
|
|
||||||
return " ";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rank.equals("loading")) return " ";
|
if (rank.equals("loading")) return " ";
|
||||||
@@ -37,52 +40,54 @@ public class RankHelper {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String RANK_URL = CitrusDevClient.API_URL + "/api/player/%s/rank";
|
private static final String RANKS_URL = CitrusDevClient.API_URL + "/api/ranks";
|
||||||
|
|
||||||
private static void fetchRank(UUID uuid) {
|
public static void fetchAllRanks() {
|
||||||
CompletableFuture.runAsync(() -> {
|
fetched = true;
|
||||||
try {
|
|
||||||
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(RANK_URL.formatted(uuid)))
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(RANKS_URL))
|
||||||
|
.timeout(Duration.ofSeconds(10))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||||
|
.thenAccept(response -> {
|
||||||
if (response.statusCode() == 200) {
|
if (response.statusCode() == 200) {
|
||||||
String rawRank = response.body().trim().toLowerCase();
|
JsonObject json = JsonParser.parseString(response.body()).getAsJsonObject();
|
||||||
|
|
||||||
if (rawRank.equals(rankCache.get(uuid))) {
|
rankCache.clear();
|
||||||
return; // No change
|
|
||||||
|
for (var entry : json.entrySet()) {
|
||||||
|
UUID uuid = UUID.fromString(entry.getKey());
|
||||||
|
String rank = entry.getValue().getAsString();
|
||||||
|
rankCache.put(uuid, rank);
|
||||||
}
|
}
|
||||||
|
|
||||||
CitrusDevClient.debugLog("Received Rank Content: '" + rawRank + "'");
|
CitrusDevClient.debugLog("Fetched ranks for " + json.size() + " players");
|
||||||
|
|
||||||
rankCache.put(uuid, rawRank);
|
|
||||||
|
|
||||||
Minecraft.getInstance().execute(() -> {
|
Minecraft.getInstance().execute(() -> {
|
||||||
var world = Minecraft.getInstance().level;
|
var world = Minecraft.getInstance().level;
|
||||||
if (world != null) {
|
if (world != null) {
|
||||||
var player = world.getPlayerByUUID(uuid);
|
for (var player : world.players()) {
|
||||||
if (player != null) {
|
|
||||||
player.setCustomNameVisible(player.isCustomNameVisible());
|
player.setCustomNameVisible(player.isCustomNameVisible());
|
||||||
CitrusDevClient.debugLog("Refreshed nametag for: " + player.getName().getString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
rankCache.put(uuid, "none");
|
CitrusDevClient.debugLog("Failed to fetch ranks: " + response.statusCode());
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
CitrusDevClient.debugLog("Failed to fetch rank: " + e.getMessage());
|
|
||||||
rankCache.put(uuid, "none");
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
.exceptionally(error -> {
|
||||||
|
CitrusDevClient.debugLog("Failed to fetch ranks: " + error.getMessage());
|
||||||
|
fetched = false;
|
||||||
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void refreshRankIfChanged() {
|
public static void refreshIfNecessary() {
|
||||||
Minecraft client = Minecraft.getInstance();
|
if (!fetched) {
|
||||||
if (client.getConnection() == null) return;
|
fetchAllRanks();
|
||||||
|
}
|
||||||
client.getConnection().getOnlinePlayers().forEach(playerInfo -> fetchRank(playerInfo.getProfile().id()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user