Skip to content

Commit 2221b08

Browse files
committed
chore: simplify sign logic
1 parent 12c56b3 commit 2221b08

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

Bukkit/src/main/java/com/plotsquared/bukkit/schematic/StateWrapper.java

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import com.plotsquared.core.PlotSquared;
2525
import com.plotsquared.core.util.ReflectionUtils;
2626
import com.sk89q.jnbt.CompoundTag;
27-
import com.sk89q.jnbt.ListTag;
28-
import com.sk89q.jnbt.StringTag;
2927
import com.sk89q.jnbt.Tag;
3028
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
3129
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
@@ -182,6 +180,13 @@ public boolean restoreTag(@NonNull Block block) {
182180
);
183181
CRAFT_BLOCK_ENTITY_STATE_LOAD_DATA.invoke(blockState, nativeTag);
184182
if (blockState instanceof Sign sign) {
183+
if (!PaperLib.isPaper()) {
184+
if (!PAPER_SIGN_NOTIFIED) {
185+
PAPER_SIGN_NOTIFIED = true;
186+
LOGGER.error("PlotSquared can't populate sign tile entities. To load sign content, use Paper.");
187+
}
188+
return false;
189+
}
185190
Object text;
186191
if ((text = tag.getValue().get("front_text")) != null && text instanceof CompoundTag textTag) {
187192
setSignTextHack(sign, textTag, true);
@@ -218,37 +223,32 @@ private static void setSignTextHack(Sign sign, CompoundTag text, boolean front)
218223
if (text.containsKey("has_glowing_text")) {
219224
side.setGlowingText(text.getByte("has_glowing_text") == 1);
220225
}
226+
if (!initializeSignHack()) {
227+
return;
228+
}
229+
// TODO: Pre 1.21.5 sign texts are JSON in string tags... somehow support and fix that
221230
List<Tag> lines = text.getList("messages");
222231
if (lines != null) {
223232
for (int i = 0; i < Math.min(lines.size(), 3); i++) {
224233
Tag line = lines.get(i);
225-
if (line instanceof StringTag stringTag) {
226-
//noinspection deprecation - Paper deprecatiom
227-
side.setLine(i, stringTag.getValue());
228-
continue;
229-
}
230-
if (line instanceof ListTag || line instanceof CompoundTag) {
231-
if (!initializeSignHack()) {
232-
continue;
233-
}
234-
// Minecraft uses mixed lists / arrays in their sign texts. One line can be a complex component, whereas
235-
// the following line could simply be a string. Those simpler lines are represented as `{"": ""}` (only in
236-
// SNBT those will be shown as a standard string). Adventure can't parse those, so we handle these lines as
237-
// plaintext lines (can't contain any other extra data either way).
238-
if (line instanceof CompoundTag compoundTag && compoundTag.getValue().containsKey("")) {
239-
//noinspection deprecation - Paper deprecatiom
240-
side.setLine(i, compoundTag.getString(""));
241-
continue;
242-
}
243-
// serializes the line content from JNBT to Gson JSON objects, passes that to adventure and deserializes
244-
// into an adventure component.
245-
BUKKIT_SIGN_SIDE_LINE_SET.invoke(
246-
side, i, GSON_SERIALIZER_DESERIALIZE_TREE.invoke(
247-
KYORI_GSON_SERIALIZER,
248-
GSON.toJsonTree(line.getValue())
249-
)
250-
);
234+
Object content = line.getValue();
235+
// Minecraft uses mixed lists / arrays in their sign texts. One line can be a complex component, whereas
236+
// the following line could simply be a string. Those simpler lines are represented as `{"": ""}` (only in
237+
// SNBT those will be shown as a standard string).
238+
if (line instanceof CompoundTag compoundTag && compoundTag.getValue().containsKey("")) {
239+
content = compoundTag.getValue().get("");
251240
}
241+
// serializes the line content from JNBT to Gson JSON objects, passes that to adventure and deserializes
242+
// into an adventure component.
243+
// pass all possible types of content into the deserializer (Strings, Compounds, Arrays), even though Strings
244+
// could be set directly via Sign#setLine(int, String). The overhead is minimal, the serializer can handle
245+
// strings - and we don't have to use the deprecated method.
246+
BUKKIT_SIGN_SIDE_LINE_SET.invoke(
247+
side, i, GSON_SERIALIZER_DESERIALIZE_TREE.invoke(
248+
KYORI_GSON_SERIALIZER,
249+
GSON.toJsonTree(content)
250+
)
251+
);
252252
}
253253
}
254254
}
@@ -260,13 +260,6 @@ private static boolean initializeSignHack() {
260260
if (KYORI_GSON_SERIALIZER != null) {
261261
return true; // already initialized
262262
}
263-
if (!PaperLib.isPaper()) {
264-
if (!PAPER_SIGN_NOTIFIED) {
265-
PAPER_SIGN_NOTIFIED = true;
266-
LOGGER.error("Can't populate non-plain sign line. To load modern sign content, use Paper.");
267-
}
268-
return false;
269-
}
270263
try {
271264
char[] dontObfuscate = new char[]{
272265
'n', 'e', 't', '.', 'k', 'y', 'o', 'r', 'i', '.', 'a', 'd', 'v', 'e', 'n', 't', 'u', 'r', 'e', '.',
@@ -290,7 +283,7 @@ private static boolean initializeSignHack() {
290283
return true;
291284
} catch (Throwable e) {
292285
FAILED_SIGN_INITIALIZATION = true;
293-
LOGGER.error("Failed to initialize sign-hack. Signs populated by schematics might not have their line contents.", e);
286+
LOGGER.error("Failed to initialize sign-hack. Signs populated by schematics won't have their line contents.", e);
294287
return false;
295288
}
296289
}

0 commit comments

Comments
 (0)