Skip to content

Commit cfe6d78

Browse files
committed
Handle compound sign text in ProtocolLib
1 parent 7e3b031 commit cfe6d78

File tree

1 file changed

+75
-10
lines changed

1 file changed

+75
-10
lines changed

src/main/java/me/crafter/mc/lockettepro/DependencyProtocolLib.java

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import com.comphenix.protocol.wrappers.nbt.NbtBase;
88
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
99
import com.comphenix.protocol.wrappers.nbt.NbtList;
10+
import com.comphenix.protocol.wrappers.nbt.NbtType;
1011
import org.bukkit.Bukkit;
1112
import org.bukkit.block.Sign;
1213
import org.bukkit.entity.Player;
1314
import org.bukkit.plugin.Plugin;
1415

16+
import java.util.Collection;
1517
import java.util.List;
1618

1719
public class DependencyProtocolLib {
@@ -80,28 +82,91 @@ public void onPacketSending(PacketEvent event) {
8082
}
8183

8284
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))) {
88100
// Private line
89-
String line1 = Utils.getSignLineFromUnknown(raw_line1);
101+
String line1 = Utils.getSignLineFromUnknown(rawLine1);
90102
if (LocketteProAPI.isLineExpired(line1)) {
91-
msgList.get(0).setValue(formatText(Config.getLockExpireString()));
103+
writeMessageValue(msgList.get(0), Config.getLockExpireString());
92104
} else {
93-
msgList.get(0).setValue(formatText(Utils.StripSharpSign(line1)));
105+
writeMessageValue(msgList.get(0), Utils.StripSharpSign(line1));
94106
}
95107
// Other line
96108
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)));
98110
if (Utils.isUsernameUuidLine(line)) {
99-
msgList.get(i).setValue(formatText(Utils.getUsernameFromLine(line)));
111+
writeMessageValue(msgList.get(i), Utils.getUsernameFromLine(line));
100112
}
101113
}
102114
}
103115
return signNbt;
104116
}
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+
}
105170
private static String formatText(String string) {
106171
return "{\"text\":\"" + string + "\"}";
107172
}

0 commit comments

Comments
 (0)