|
1 | 1 | package me.rothes.protocolstringreplacer.packetlisteners.server.scoreboard; |
2 | 2 |
|
3 | | -import com.comphenix.protocol.PacketType; |
4 | 3 | import com.comphenix.protocol.events.PacketContainer; |
5 | 4 | import com.comphenix.protocol.events.PacketEvent; |
6 | | -import com.comphenix.protocol.utility.MinecraftReflection; |
7 | | -import com.comphenix.protocol.wrappers.WrappedChatComponent; |
8 | | -import me.rothes.protocolstringreplacer.api.replacer.ReplacerConfig; |
| 5 | +import com.comphenix.protocol.reflect.StructureModifier; |
9 | 6 | import me.rothes.protocolstringreplacer.api.user.PsrUser; |
10 | | -import me.rothes.protocolstringreplacer.replacer.ListenType; |
11 | 7 | import org.jetbrains.annotations.NotNull; |
12 | 8 |
|
13 | | -import java.lang.reflect.Field; |
14 | | -import java.util.Arrays; |
15 | | -import java.util.List; |
16 | | -import java.util.Optional; |
17 | | -import java.util.function.BiPredicate; |
18 | | -import java.util.stream.Collectors; |
19 | | - |
20 | | -public class UpdateTeam extends AbstractScoreBoardListener { |
21 | | - |
22 | | - protected final BiPredicate<ReplacerConfig, PsrUser> teamDNameFilter = (replacerConfig, user) -> |
23 | | - containType(replacerConfig) && checkFilter(user, replacerConfig) && replacerConfig.handleScoreboardTeamDisplayName(); |
24 | | - protected final BiPredicate<ReplacerConfig, PsrUser> teamPrefixFilter = (replacerConfig, user) -> |
25 | | - containType(replacerConfig) && checkFilter(user, replacerConfig) && replacerConfig.handleScoreboardTeamPrefix(); |
26 | | - protected final BiPredicate<ReplacerConfig, PsrUser> teamSuffixFilter = (replacerConfig, user) -> |
27 | | - containType(replacerConfig) && checkFilter(user, replacerConfig) && replacerConfig.handleScoreboardTeamSuffix(); |
28 | | - |
29 | | - private final Field displayName; |
30 | | - private final Field prefix; |
31 | | - private final Field suffix; |
32 | | - |
33 | | - public UpdateTeam() { |
34 | | - super(PacketType.Play.Server.SCOREBOARD_TEAM, ListenType.SCOREBOARD); |
35 | | - |
36 | | - Class<?> info = PacketType.Play.Server.SCOREBOARD_TEAM.getPacketClass().getDeclaredClasses()[0]; |
37 | | - List<Field> collect = Arrays.stream(info.getDeclaredFields()) |
38 | | - .filter(it -> it.getType() == MinecraftReflection.getIChatBaseComponentClass()) |
39 | | - .collect(Collectors.toList()); |
40 | | - displayName = collect.get(0); |
41 | | - displayName.setAccessible(true); |
42 | | - prefix = collect.get(1); |
43 | | - prefix.setAccessible(true); |
44 | | - suffix = collect.get(2); |
45 | | - suffix.setAccessible(true); |
46 | | - } |
| 9 | +public final class UpdateTeam extends BaseUpdateTeamListener { |
47 | 10 |
|
| 11 | + @Override |
48 | 12 | protected void process(@NotNull PacketEvent packetEvent) { |
49 | 13 | PsrUser user = getEventUser(packetEvent); |
50 | 14 | if (user == null) { |
51 | 15 | return; |
52 | 16 | } |
53 | 17 | PacketContainer packet = packetEvent.getPacket(); |
54 | | - |
55 | | - Optional<?> read = (Optional<?>) packet.getModifier().withType(Optional.class).read(0); |
56 | | - if (!read.isPresent()) { |
| 18 | + StructureModifier<String> strings = packet.getStrings(); |
| 19 | + String replacedText = getReplacedText(packetEvent, user, listenType, strings.read(1), teamDNameFilter); |
| 20 | + if (replacedText == null) { |
57 | 21 | return; |
58 | 22 | } |
| 23 | + strings.write(1, replacedText); |
59 | 24 |
|
60 | | - Object o = read.get(); |
61 | | - if (processField(o, prefix, packetEvent, user, teamPrefixFilter)) { |
| 25 | + replacedText = getReplacedText(packetEvent, user, listenType, strings.read(2), teamPrefixFilter); |
| 26 | + if (replacedText == null) { |
62 | 27 | return; |
63 | 28 | } |
64 | | - if (processField(o, displayName, packetEvent, user, teamDNameFilter)) { |
| 29 | + strings.write(2, replacedText); |
| 30 | + |
| 31 | + replacedText = getReplacedText(packetEvent, user, listenType, strings.read(3), teamSuffixFilter); |
| 32 | + if (replacedText == null) { |
65 | 33 | return; |
66 | 34 | } |
67 | | - processField(o, suffix, packetEvent, user, teamSuffixFilter); |
68 | | - } |
| 35 | + strings.write(3, replacedText); |
69 | 36 |
|
70 | | - private boolean processField(Object infoObj, Field field, PacketEvent event, PsrUser user, |
71 | | - BiPredicate<ReplacerConfig, PsrUser> filter) { |
72 | | - try { |
73 | | - WrappedChatComponent wrappedChatComponent = WrappedChatComponent.fromHandle(field.get(infoObj)); |
74 | | - String json = wrappedChatComponent.getJson(); |
75 | | - String replacedJson = getReplacedJson(event, user, listenType, json, filter); |
76 | | - if (replacedJson == null) { |
77 | | - return true; |
78 | | - } |
79 | | - wrappedChatComponent.setJson(replacedJson); |
80 | | - field.set(infoObj, wrappedChatComponent.getHandle()); |
81 | | - } catch (IllegalAccessException e) { |
82 | | - throw new RuntimeException(e); |
83 | | - } |
84 | | - return false; |
85 | 37 | } |
86 | 38 |
|
87 | 39 | } |
0 commit comments