|
2 | 2 |
|
3 | 3 | import dev.architectury.event.EventResult;
|
4 | 4 | import dev.architectury.event.events.common.ChatEvent;
|
5 |
| -import dev.ftb.mods.ftblibrary.util.TextComponentUtils; |
6 | 5 | import dev.ftb.mods.ftbranks.FTBRanks;
|
7 | 6 | import dev.ftb.mods.ftbranks.api.FTBRanksAPI;
|
8 | 7 | import dev.ftb.mods.ftbranks.api.RankManager;
|
9 |
| -import dev.ftb.mods.ftbranks.impl.condition.AlwaysActiveCondition; |
10 |
| -import dev.ftb.mods.ftbranks.impl.condition.AndCondition; |
11 |
| -import dev.ftb.mods.ftbranks.impl.condition.CreativeModeCondition; |
12 |
| -import dev.ftb.mods.ftbranks.impl.condition.DimensionCondition; |
13 |
| -import dev.ftb.mods.ftbranks.impl.condition.FakePlayerCondition; |
14 |
| -import dev.ftb.mods.ftbranks.impl.condition.NotCondition; |
15 |
| -import dev.ftb.mods.ftbranks.impl.condition.OPCondition; |
16 |
| -import dev.ftb.mods.ftbranks.impl.condition.OrCondition; |
17 |
| -import dev.ftb.mods.ftbranks.impl.condition.PlaytimeCondition; |
18 |
| -import dev.ftb.mods.ftbranks.impl.condition.RankAddedCondition; |
19 |
| -import dev.ftb.mods.ftbranks.impl.condition.SpawnCondition; |
20 |
| -import dev.ftb.mods.ftbranks.impl.condition.StatCondition; |
21 |
| -import dev.ftb.mods.ftbranks.impl.condition.XorCondition; |
| 8 | +import dev.ftb.mods.ftbranks.impl.condition.*; |
22 | 9 | import net.minecraft.ChatFormatting;
|
23 |
| -import net.minecraft.network.chat.Component; |
24 | 10 | import net.minecraft.network.chat.HoverEvent;
|
25 |
| -import net.minecraft.network.chat.MutableComponent; |
26 | 11 | import net.minecraft.network.chat.TextComponent;
|
27 | 12 | import net.minecraft.network.chat.TranslatableComponent;
|
28 | 13 | import net.minecraft.server.MinecraftServer;
|
|
34 | 19 | * @author LatvianModder
|
35 | 20 | */
|
36 | 21 | public class FTBRanksAPIImpl extends FTBRanksAPI {
|
37 |
| - public static RankManagerImpl manager; |
38 |
| - |
39 |
| - @Override |
40 |
| - public RankManager getManager() { |
41 |
| - return manager; |
42 |
| - } |
43 |
| - |
44 |
| - public static void serverAboutToStart(MinecraftServer server) { |
45 |
| - manager = new RankManagerImpl(server); |
46 |
| - } |
47 |
| - |
48 |
| - public static void serverStarted(MinecraftServer server) { |
49 |
| - // manager.initCommands(); |
50 |
| - |
51 |
| - try { |
52 |
| - manager.load(); |
53 |
| - } catch (Exception ex) { |
54 |
| - ex.printStackTrace(); |
55 |
| - } |
56 |
| - } |
57 |
| - |
58 |
| - public static void serverStopped(MinecraftServer server) { |
59 |
| - manager = null; |
60 |
| - } |
61 |
| - |
62 |
| - public static void worldSaved(ServerLevel event) { |
63 |
| - if (manager != null) { |
64 |
| - manager.saveRanksNow(); |
65 |
| - manager.savePlayersNow(); |
66 |
| - } |
67 |
| - } |
68 |
| - |
69 |
| - public static void serverStarting(MinecraftServer server) { |
70 |
| - manager.registerCondition("always_active", (rank, json) -> AlwaysActiveCondition.INSTANCE); |
71 |
| - manager.registerCondition("rank_added", RankAddedCondition::new); |
72 |
| - |
73 |
| - manager.registerCondition("not", NotCondition::new); |
74 |
| - manager.registerCondition("or", OrCondition::new); |
75 |
| - manager.registerCondition("and", AndCondition::new); |
76 |
| - manager.registerCondition("xor", XorCondition::new); |
77 |
| - |
78 |
| - manager.registerCondition("op", (rank, tag) -> new OPCondition()); |
79 |
| - manager.registerCondition("spawn", (rank, tag) -> new SpawnCondition()); |
80 |
| - manager.registerCondition("dimension", (rank, tag) -> new DimensionCondition(tag)); |
81 |
| - manager.registerCondition("playtime", (rank, tag) -> new PlaytimeCondition(tag)); |
82 |
| - manager.registerCondition("stat", (rank, tag) -> new StatCondition(tag)); |
83 |
| - manager.registerCondition("fake_player", (rank, tag) -> new FakePlayerCondition()); |
84 |
| - manager.registerCondition("creative_mode", (rank, tag) -> new CreativeModeCondition()); |
85 |
| - } |
86 |
| - |
87 |
| - |
88 |
| - public static EventResult serverChat(ServerPlayer player, TextFilter.FilteredText eventMessage, ChatEvent.ChatComponent component) { |
89 |
| - String format = FTBRanksAPI.getPermissionValue(player, "ftbranks.name_format").asString().orElse(""); |
90 |
| - |
91 |
| - if (format.isEmpty()) { |
92 |
| - return EventResult.pass(); |
93 |
| - } |
94 |
| - |
95 |
| - TextComponent main = new TextComponent(""); |
96 |
| - TextComponent cachedNameForChat; |
97 |
| - |
98 |
| - try { |
99 |
| - cachedNameForChat = TextComponentParser.parse(format, s -> { |
100 |
| - if (s.equals("name")) { |
101 |
| - return player.getDisplayName(); |
102 |
| - } |
103 |
| - |
104 |
| - return null; |
105 |
| - }); |
106 |
| - } catch (Exception ex) { |
107 |
| - String s = "Error parsing " + format + ": " + ex; |
108 |
| - FTBRanks.LOGGER.error(s); |
109 |
| - cachedNameForChat = new TextComponent("BrokenFormatting"); |
110 |
| - cachedNameForChat.withStyle(ChatFormatting.RED); |
111 |
| - cachedNameForChat.setStyle(cachedNameForChat.getStyle().withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponent(s)))); |
112 |
| - } |
113 |
| - |
114 |
| - main.append(cachedNameForChat); |
115 |
| - main.append(" "); |
116 |
| - |
117 |
| - MutableComponent text = null; |
118 |
| - |
119 |
| - // In the easiest case, we have the vanilla chat format, |
120 |
| - // so we can just use the message from that. |
121 |
| - if (component instanceof TranslatableComponent tc && tc.getKey().equals("chat.type.text") && tc.getArgs().length > 1) { |
122 |
| - Object message = tc.getArgs()[1]; |
123 |
| - if (message instanceof Component c) { |
124 |
| - text = c.copy(); |
125 |
| - } else { |
126 |
| - text = new TextComponent(message.toString()); |
127 |
| - } |
128 |
| - } |
129 |
| - |
130 |
| - // Otherwise, fall back to parsing the message as a string and turning it back into a component. |
131 |
| - if (text == null) { |
132 |
| - FTBRanks.LOGGER.debug("Chat message format has been changed, fall back to parsing as string!"); |
133 |
| - FTBRanks.LOGGER.debug("Since this may break formatting, feel free to remove the `ftbranks.name_format` permission node to stop this from happening."); |
134 |
| - text = TextComponentUtils.withLinks(eventMessage.getFiltered()).copy(); |
135 |
| - } |
136 |
| - |
137 |
| - ChatFormatting color = ChatFormatting.getByName(FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.color").asString().orElse(null)); |
138 |
| - if (color != null) { |
139 |
| - text.setStyle(text.getStyle().applyFormat(color)); |
140 |
| - } |
141 |
| - |
142 |
| - if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.bold").asBooleanOrFalse()) { |
143 |
| - text.setStyle(text.getStyle().applyFormat(ChatFormatting.BOLD)); |
144 |
| - } |
145 |
| - |
146 |
| - if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.italic").asBooleanOrFalse()) { |
147 |
| - text.setStyle(text.getStyle().applyFormat(ChatFormatting.ITALIC)); |
148 |
| - } |
149 |
| - |
150 |
| - if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.underlined").asBooleanOrFalse()) { |
151 |
| - text.setStyle(text.getStyle().applyFormat(ChatFormatting.UNDERLINE)); |
152 |
| - } |
153 |
| - |
154 |
| - if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.strikethrough").asBooleanOrFalse()) { |
155 |
| - text.setStyle(text.getStyle().applyFormat(ChatFormatting.STRIKETHROUGH)); |
156 |
| - } |
157 |
| - |
158 |
| - if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.obfuscated").asBooleanOrFalse()) { |
159 |
| - text.setStyle(text.getStyle().applyFormat(ChatFormatting.OBFUSCATED)); |
160 |
| - } |
161 |
| - |
162 |
| - main.append(text); |
163 |
| - |
164 |
| - component.setFiltered(main); |
165 |
| - return EventResult.interruptTrue(); |
166 |
| - } |
| 22 | + public static RankManagerImpl manager; |
| 23 | + |
| 24 | + @Override |
| 25 | + public RankManager getManager() { |
| 26 | + return manager; |
| 27 | + } |
| 28 | + |
| 29 | + public static void serverAboutToStart(MinecraftServer server) { |
| 30 | + manager = new RankManagerImpl(server); |
| 31 | + } |
| 32 | + |
| 33 | + public static void serverStarted(MinecraftServer server) { |
| 34 | + try { |
| 35 | + manager.load(); |
| 36 | + } catch (Exception ex) { |
| 37 | + ex.printStackTrace(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public static void serverStopped(MinecraftServer server) { |
| 42 | + manager = null; |
| 43 | + } |
| 44 | + |
| 45 | + public static void worldSaved(ServerLevel event) { |
| 46 | + if (manager != null) { |
| 47 | + manager.saveRanksNow(); |
| 48 | + manager.savePlayersNow(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static void serverStarting(MinecraftServer server) { |
| 53 | + manager.registerCondition("always_active", (rank, json) -> AlwaysActiveCondition.INSTANCE); |
| 54 | + manager.registerCondition("rank_added", RankAddedCondition::new); |
| 55 | + |
| 56 | + manager.registerCondition("not", NotCondition::new); |
| 57 | + manager.registerCondition("or", OrCondition::new); |
| 58 | + manager.registerCondition("and", AndCondition::new); |
| 59 | + manager.registerCondition("xor", XorCondition::new); |
| 60 | + |
| 61 | + manager.registerCondition("op", (rank, tag) -> new OPCondition()); |
| 62 | + manager.registerCondition("spawn", (rank, tag) -> new SpawnCondition()); |
| 63 | + manager.registerCondition("dimension", (rank, tag) -> new DimensionCondition(tag)); |
| 64 | + manager.registerCondition("playtime", (rank, tag) -> new PlaytimeCondition(tag)); |
| 65 | + manager.registerCondition("stat", (rank, tag) -> new StatCondition(tag)); |
| 66 | + manager.registerCondition("fake_player", (rank, tag) -> new FakePlayerCondition()); |
| 67 | + manager.registerCondition("creative_mode", (rank, tag) -> new CreativeModeCondition()); |
| 68 | + } |
| 69 | + |
| 70 | + public static EventResult serverChat(ServerPlayer player, TextFilter.FilteredText eventMessage, ChatEvent.ChatComponent component) { |
| 71 | + String format = FTBRanksAPI.getPermissionValue(player, "ftbranks.name_format").asString().orElse(""); |
| 72 | + |
| 73 | + if (format.isEmpty()) { |
| 74 | + return EventResult.pass(); |
| 75 | + } |
| 76 | + |
| 77 | + TextComponent main = new TextComponent(""); |
| 78 | + TextComponent cachedNameForChat; |
| 79 | + |
| 80 | + try { |
| 81 | + cachedNameForChat = TextComponentParser.parse(format, s -> s.equals("name") ? player.getDisplayName() : null); |
| 82 | + } catch (Exception ex) { |
| 83 | + String s = "Error parsing " + format + ": " + ex; |
| 84 | + FTBRanks.LOGGER.error(s); |
| 85 | + cachedNameForChat = new TextComponent("BrokenFormatting"); |
| 86 | + cachedNameForChat.withStyle(ChatFormatting.RED); |
| 87 | + cachedNameForChat.setStyle(cachedNameForChat.getStyle().withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponent(s)))); |
| 88 | + } |
| 89 | + |
| 90 | + main.append(cachedNameForChat); |
| 91 | + main.append(" "); |
| 92 | + |
| 93 | + if (component.getFiltered() instanceof TranslatableComponent fullComp) { |
| 94 | + ChatFormatting color = ChatFormatting.getByName(FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.color").asString().orElse(null)); |
| 95 | + for (int i = 1; i < fullComp.getArgs().length; i++) { |
| 96 | + Object arg = fullComp.getArgs()[i]; |
| 97 | + TextComponent part = arg instanceof TextComponent tc ? tc : new TextComponent(arg.toString()); |
| 98 | + |
| 99 | + if (color != null) { |
| 100 | + part.setStyle(part.getStyle().applyFormat(color)); |
| 101 | + } |
| 102 | + if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.bold").asBooleanOrFalse()) { |
| 103 | + part.setStyle(part.getStyle().applyFormat(ChatFormatting.BOLD)); |
| 104 | + } |
| 105 | + if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.italic").asBooleanOrFalse()) { |
| 106 | + part.setStyle(part.getStyle().applyFormat(ChatFormatting.ITALIC)); |
| 107 | + } |
| 108 | + if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.underlined").asBooleanOrFalse()) { |
| 109 | + part.setStyle(part.getStyle().applyFormat(ChatFormatting.UNDERLINE)); |
| 110 | + } |
| 111 | + if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.strikethrough").asBooleanOrFalse()) { |
| 112 | + part.setStyle(part.getStyle().applyFormat(ChatFormatting.STRIKETHROUGH)); |
| 113 | + } |
| 114 | + if (FTBRanksAPI.getPermissionValue(player, "ftbranks.chat_text.obfuscated").asBooleanOrFalse()) { |
| 115 | + part.setStyle(part.getStyle().applyFormat(ChatFormatting.OBFUSCATED)); |
| 116 | + } |
| 117 | + main.append(part); |
| 118 | + } |
| 119 | + } else { |
| 120 | + main.append(component.getFiltered()); |
| 121 | + } |
| 122 | + |
| 123 | + component.setFiltered(main); |
| 124 | + return EventResult.interruptTrue(); |
| 125 | + } |
167 | 126 | }
|
0 commit comments