|
7 | 7 | import com.comphenix.protocol.wrappers.nbt.NbtBase; |
8 | 8 | import com.comphenix.protocol.wrappers.nbt.NbtCompound; |
9 | 9 | import com.comphenix.protocol.wrappers.nbt.NbtList; |
| 10 | +import com.comphenix.protocol.wrappers.nbt.NbtType; |
10 | 11 | import org.bukkit.Bukkit; |
11 | 12 | import org.bukkit.block.Sign; |
12 | 13 | import org.bukkit.entity.Player; |
13 | 14 | import org.bukkit.plugin.Plugin; |
14 | 15 |
|
| 16 | +import java.util.Collection; |
15 | 17 | import java.util.List; |
16 | 18 |
|
17 | 19 | public class DependencyProtocolLib { |
@@ -80,28 +82,91 @@ public void onPacketSending(PacketEvent event) { |
80 | 82 | } |
81 | 83 |
|
82 | 84 | public static NbtCompound onSignSend(Player player, NbtCompound signNbt) { |
83 | | - NbtList<String> msgs = signNbt.getCompound("front_text").getList("messages"); |
84 | | - List<NbtBase<String>> msgList = msgs.getValue(); |
85 | | - if (msgList.isEmpty()) return signNbt; |
86 | | - String raw_line1 = msgList.get(0).getValue(); |
87 | | - if (LocketteProAPI.isLockStringOrAdditionalString(Utils.getSignLineFromUnknown(raw_line1))) { |
| 85 | + NbtCompound frontText = signNbt.getCompound("front_text"); |
| 86 | + if (frontText == null) { |
| 87 | + return signNbt; |
| 88 | + } |
| 89 | + NbtList<?> msgs = frontText.getList("messages"); |
| 90 | + if (msgs == null) { |
| 91 | + return signNbt; |
| 92 | + } |
| 93 | + @SuppressWarnings("unchecked") |
| 94 | + List<NbtBase<?>> msgList = (List<NbtBase<?>>) (List<?>) msgs.getValue(); |
| 95 | + if (msgList.isEmpty()) { |
| 96 | + return signNbt; |
| 97 | + } |
| 98 | + String rawLine1 = readMessageValue(msgList.get(0)); |
| 99 | + if (LocketteProAPI.isLockStringOrAdditionalString(Utils.getSignLineFromUnknown(rawLine1))) { |
88 | 100 | // Private line |
89 | | - String line1 = Utils.getSignLineFromUnknown(raw_line1); |
| 101 | + String line1 = Utils.getSignLineFromUnknown(rawLine1); |
90 | 102 | if (LocketteProAPI.isLineExpired(line1)) { |
91 | | - msgList.get(0).setValue(formatText(Config.getLockExpireString())); |
| 103 | + writeMessageValue(msgList.get(0), Config.getLockExpireString()); |
92 | 104 | } else { |
93 | | - msgList.get(0).setValue(formatText(Utils.StripSharpSign(line1))); |
| 105 | + writeMessageValue(msgList.get(0), Utils.StripSharpSign(line1)); |
94 | 106 | } |
95 | 107 | // Other line |
96 | 108 | for (int i = 1; i < msgList.size(); i++) { |
97 | | - String line = Utils.getSignLineFromUnknown(msgList.get(i).getValue()); |
| 109 | + String line = Utils.getSignLineFromUnknown(readMessageValue(msgList.get(i))); |
98 | 110 | if (Utils.isUsernameUuidLine(line)) { |
99 | | - msgList.get(i).setValue(formatText(Utils.getUsernameFromLine(line))); |
| 111 | + writeMessageValue(msgList.get(i), Utils.getUsernameFromLine(line)); |
100 | 112 | } |
101 | 113 | } |
102 | 114 | } |
103 | 115 | return signNbt; |
104 | 116 | } |
| 117 | + |
| 118 | + private static String readMessageValue(NbtBase<?> messageBase) { |
| 119 | + if (messageBase == null) { |
| 120 | + return ""; |
| 121 | + } |
| 122 | + NbtType type = messageBase.getType(); |
| 123 | + if (type == NbtType.TAG_STRING) { |
| 124 | + Object value = messageBase.getValue(); |
| 125 | + return value == null ? "" : value.toString(); |
| 126 | + } |
| 127 | + if (type == NbtType.TAG_COMPOUND) { |
| 128 | + return readMessageFromCompound((NbtCompound) messageBase); |
| 129 | + } |
| 130 | + Object value = messageBase.getValue(); |
| 131 | + return value == null ? "" : value.toString(); |
| 132 | + } |
| 133 | + |
| 134 | + private static String readMessageFromCompound(NbtCompound compound) { |
| 135 | + if (compound == null) { |
| 136 | + return ""; |
| 137 | + } |
| 138 | + StringBuilder builder = new StringBuilder(); |
| 139 | + String text = compound.getStringOrDefault("text"); |
| 140 | + if (text != null && !text.isEmpty()) { |
| 141 | + builder.append(text); |
| 142 | + } |
| 143 | + NbtList<?> extraList = compound.getList("extra"); |
| 144 | + if (extraList != null) { |
| 145 | + @SuppressWarnings("unchecked") |
| 146 | + List<NbtBase<?>> extras = (List<NbtBase<?>>) (List<?>) extraList.getValue(); |
| 147 | + for (NbtBase<?> extra : extras) { |
| 148 | + builder.append(readMessageValue(extra)); |
| 149 | + } |
| 150 | + } |
| 151 | + return builder.toString(); |
| 152 | + } |
| 153 | + |
| 154 | + private static void writeMessageValue(NbtBase<?> messageBase, String text) { |
| 155 | + if (messageBase == null) { |
| 156 | + return; |
| 157 | + } |
| 158 | + if (messageBase.getType() == NbtType.TAG_STRING) { |
| 159 | + @SuppressWarnings("unchecked") |
| 160 | + NbtBase<String> stringBase = (NbtBase<String>) messageBase; |
| 161 | + stringBase.setValue(formatText(text)); |
| 162 | + return; |
| 163 | + } |
| 164 | + if (messageBase.getType() == NbtType.TAG_COMPOUND) { |
| 165 | + NbtCompound compound = (NbtCompound) messageBase; |
| 166 | + compound.put("text", text); |
| 167 | + compound.remove("extra"); |
| 168 | + } |
| 169 | + } |
105 | 170 | private static String formatText(String string) { |
106 | 171 | return "{\"text\":\"" + string + "\"}"; |
107 | 172 | } |
|
0 commit comments