MC Version 1.20.1, use waxed sign api (#884)

* bump api version to 1.20.1
* Sign.isWaxed is now API
This commit is contained in:
KleinCrafter
2023-10-17 08:33:06 +02:00
committed by GitHub
parent fc92ad2307
commit f2e76a50cb
4 changed files with 6 additions and 70 deletions

View File

@@ -44,7 +44,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20-R0.1-SNAPSHOT</version>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@@ -1,7 +1,6 @@
package de.diddiz.LogBlock.blockstate;
import de.diddiz.LogBlock.util.BukkitUtils;
import de.diddiz.LogBlock.util.Reflections;
import java.awt.Color;
import java.util.Arrays;
import java.util.Collections;
@@ -28,7 +27,7 @@ public class BlockStateCodecSign implements BlockStateCodec {
public YamlConfiguration serialize(BlockState state) {
YamlConfiguration conf = null;
if (state instanceof Sign sign) {
boolean waxed = Reflections.isSignWaxed(sign);
boolean waxed = sign.isWaxed();
if (waxed) {
conf = new YamlConfiguration();
conf.set("waxed", waxed);
@@ -92,7 +91,7 @@ public class BlockStateCodecSign implements BlockStateCodec {
if (state instanceof Sign) {
Sign sign = (Sign) state;
if (conf != null) {
sign.setEditable(!conf.getBoolean("waxed"));
sign.setWaxed(conf.getBoolean("waxed"));
for (Side side : Side.values()) {
ConfigurationSection sideSection = side == Side.FRONT ? conf : conf.getConfigurationSection(side.name().toLowerCase());
DyeColor signColor = DyeColor.BLACK;
@@ -120,7 +119,7 @@ public class BlockStateCodecSign implements BlockStateCodec {
signSide.setGlowingText(glowing);
}
} else {
sign.setEditable(true);
sign.setWaxed(false);
for (Side side : Side.values()) {
SignSide signSide = sign.getSide(side);
for (int i = 0; i < 4; i++) {

View File

@@ -5,7 +5,6 @@ import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig;
import de.diddiz.LogBlock.util.BukkitUtils;
import de.diddiz.LogBlock.util.Reflections;
import java.util.UUID;
import org.bukkit.DyeColor;
import org.bukkit.GameEvent;
@@ -99,8 +98,7 @@ public class InteractLogging extends LoggingListener {
if (BukkitUtils.isDye(itemType) || itemType == Material.GLOW_INK_SAC || itemType == Material.INK_SAC || itemType == Material.HONEYCOMB) {
final BlockState before = event.getClickedBlock().getState();
if (before instanceof Sign signBefore) {
boolean waxed = Reflections.isSignWaxed(signBefore);
if (!waxed) {
if (!signBefore.isWaxed()) {
final Sign signAfter = (Sign) event.getClickedBlock().getState();
Side side = BukkitUtils.getFacingSignSide(player, clicked);
SignSide signSideBefore = signBefore.getSide(side);
@@ -116,7 +114,7 @@ public class InteractLogging extends LoggingListener {
consumer.queueBlockReplace(Actor.actorFromEntity(player), signBefore, signAfter);
}
} else if (itemType == Material.HONEYCOMB) {
signAfter.setEditable(false);
signAfter.setWaxed(true);
consumer.queueBlockReplace(Actor.actorFromEntity(player), signBefore, signAfter);
} else if (BukkitUtils.isDye(itemType) && hasText(signSideBefore)) {
DyeColor newColor = BukkitUtils.dyeToDyeColor(itemType);

View File

@@ -1,61 +0,0 @@
package de.diddiz.LogBlock.util;
import de.diddiz.LogBlock.LogBlock;
import java.lang.reflect.Field;
import java.util.logging.Level;
import org.bukkit.block.Sign;
public class Reflections {
private static Field FIELD_CraftBlockEntityState_snapshot;
private static Field FIELD_SignBlockEntity_isWaxed;
public static boolean isSignWaxed(Sign sign) {
try {
if (FIELD_CraftBlockEntityState_snapshot == null) {
Class<?> superClass = sign.getClass().getSuperclass();
while (superClass != null) {
try {
FIELD_CraftBlockEntityState_snapshot = superClass.getDeclaredField("snapshot");
FIELD_CraftBlockEntityState_snapshot.setAccessible(true);
break;
} catch (NoSuchFieldException ignored) {
}
superClass = superClass.getSuperclass();
}
}
if (FIELD_CraftBlockEntityState_snapshot == null) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Reflections: Sign field 'snapshot' not found");
return false;
}
Object snapshot = FIELD_CraftBlockEntityState_snapshot.get(sign);
if (snapshot == null) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Reflections: Sign snapshot is null?");
return false;
}
if (FIELD_SignBlockEntity_isWaxed == null) {
Class<?> snapshotClass = snapshot.getClass();
while (snapshotClass != null) {
for (Field f : snapshotClass.getDeclaredFields()) {
if (f.getType() == boolean.class) {
FIELD_SignBlockEntity_isWaxed = f;
FIELD_SignBlockEntity_isWaxed.setAccessible(true);
break;
}
}
if (FIELD_SignBlockEntity_isWaxed != null) {
break;
}
snapshotClass = snapshotClass.getSuperclass();
}
}
if (FIELD_SignBlockEntity_isWaxed == null) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Reflections: Sign field 'isWaxed' not found");
return false;
}
return FIELD_SignBlockEntity_isWaxed.getBoolean(snapshot);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Reflections: Sign.isWaxed reflection failed", e);
}
return false;
}
}