Fixes compatibility for pre-1.20 spigot

This commit is contained in:
2023-06-17 14:37:05 +02:00
parent ef2238a535
commit 6b6e9d007e
3 changed files with 74 additions and 7 deletions

View File

@@ -1,10 +1,10 @@
package net.knarcraft.stargate.container;
import net.knarcraft.knarlib.util.ColorHelper;
import net.knarcraft.stargate.utility.SignHelper;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.DyeColor;
import org.bukkit.block.Sign;
import org.bukkit.block.sign.Side;
/**
* A class that keeps track of the sign colors for a given sign
@@ -27,7 +27,7 @@ public class SignData {
this.sign = sign;
this.mainSignColor = mainSignColor;
this.highlightSignColor = highlightSignColor;
this.dyedColor = sign.getSide(Side.FRONT).getColor();
this.dyedColor = SignHelper.getDye(sign);
}
/**

View File

@@ -6,12 +6,12 @@ import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.container.SignData;
import net.knarcraft.stargate.portal.property.PortalLocation;
import net.knarcraft.stargate.utility.PermissionHelper;
import net.knarcraft.stargate.utility.SignHelper;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.block.sign.Side;
import java.util.Map;
@@ -176,7 +176,7 @@ public class PortalSignDrawer {
*/
private void clearSign(Sign sign) {
for (int index = 0; index <= 3; index++) {
sign.getSide(Side.FRONT).setLine(index, "");
SignHelper.setSignLine(sign, index, "");
}
}
@@ -189,7 +189,7 @@ public class PortalSignDrawer {
return;
}
clearSign(sign);
sign.getSide(Side.FRONT).setLine(0, translateAllColorCodes(portal.getName()));
SignHelper.setSignLine(sign, 0, translateAllColorCodes(portal.getName()));
sign.update();
}
@@ -257,7 +257,7 @@ public class PortalSignDrawer {
*/
public void setLine(SignData signData, int index, String text) {
ChatColor mainColor = signData.getMainSignColor();
signData.getSign().getSide(Side.FRONT).setLine(index, mainColor + text);
SignHelper.setSignLine(signData.getSign(), index, mainColor + text);
}
/**
@@ -358,7 +358,7 @@ public class PortalSignDrawer {
if (!(blockState instanceof Sign sign)) {
return;
}
sign.getSide(Side.FRONT).setLine(3, errorColor + Stargate.getString("signInvalidGate"));
SignHelper.setSignLine(sign, 3, errorColor + Stargate.getString("signInvalidGate"));
sign.update();
Stargate.logInfo(String.format("Gate layout on line %d does not exist [%s]", lineIndex, gateName));

View File

@@ -0,0 +1,67 @@
package net.knarcraft.stargate.utility;
import org.bukkit.DyeColor;
import org.bukkit.block.Sign;
import org.bukkit.block.sign.Side;
/**
* A helper class for dealing with signs
*/
public final class SignHelper {
private static final boolean HAS_SIGN_SIDES = hasSignSides();
private SignHelper() {
}
/**
* Gets the dye color of the given sign
*
* @param sign <p>The sign to check</p>
* @return <p>The dye currently applied to the sign</p>
*/
public static DyeColor getDye(Sign sign) {
if (HAS_SIGN_SIDES) {
return sign.getSide(Side.FRONT).getColor();
} else {
// Note: This is depreciated, but is currently necessary for pre-1.19.4 support
//noinspection deprecation
return sign.getColor();
}
}
/**
* Sets the text of a line on a sign
*
* @param sign <p>The sign to set text for</p>
* @param line <p>The line to set</p>
* @param text <p>The text to set</p>
*/
public static void setSignLine(Sign sign, int line, String text) {
if (HAS_SIGN_SIDES) {
sign.getSide(Side.FRONT).setLine(line, text);
} else {
// Note: This is depreciated, but is currently necessary for pre-1.19.4 support
//noinspection deprecation
sign.setLine(line, text);
}
}
/**
* Checks whether the running version differentiates between the front or back of a sign
*
* @return <p>True if the server supports sign side differentiation</p>
*/
private static boolean hasSignSides() {
try {
Class.forName("Side");
Class<?> aClass = Class.forName("org.bukkit.block.Sign");
aClass.getMethod("getSide", Side.class);
return true;
} catch (ClassNotFoundException | NoSuchMethodException ignored) {
return false;
}
}
}