-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPlayerNameFormatting.java
More file actions
43 lines (38 loc) · 1.71 KB
/
PlayerNameFormatting.java
File metadata and controls
43 lines (38 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package dev.ftb.mods.ftbranks;
import dev.architectury.utils.GameInstance;
import dev.ftb.mods.ftblibrary.util.PlayerDisplayNameUtil;
import dev.ftb.mods.ftbranks.api.FTBRanksAPI;
import dev.ftb.mods.ftbranks.impl.decorate.TextComponentParser;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.Style;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
public class PlayerNameFormatting {
public static Component formatPlayerName(Player player, Component originalName) {
if (!(player instanceof ServerPlayer serverPlayer)) return originalName;
String format = FTBRanksAPI.getPermissionValue(serverPlayer, "ftbranks.name_format").asString().orElse("");
if (!format.isEmpty()) {
try {
return TextComponentParser.parse(format, s -> s.equals("name") ? originalName : null);
} catch (Exception ex) {
String s = "Error parsing " + format + ": " + ex;
FTBRanks.LOGGER.error(s);
return Component.literal("BrokenFormatting").withStyle(Style.EMPTY
.withColor(ChatFormatting.RED)
.withHoverEvent(new HoverEvent.ShowText(Component.literal(s)))
);
}
} else {
return originalName;
}
}
public static void refreshPlayerNames() {
MinecraftServer server = GameInstance.getServer();
if (server != null) {
server.getPlayerList().getPlayers().forEach(PlayerDisplayNameUtil::refreshDisplayName);
}
}
}