Fixes some bugs preventing placeholders on the back of signs from working
All checks were successful
KnarCraft/PlaceholderSigns/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-04-05 03:45:43 +02:00
parent 51564570ad
commit 66bb96631a
3 changed files with 21 additions and 10 deletions

View File

@ -154,13 +154,21 @@ public final class PlaceholderSigns extends JavaPlugin {
} }
// Update placeholders // Update placeholders
SignSide front = sign.getSide(Side.FRONT);
SignSide back = sign.getSide(Side.BACK);
Map<Side, Map<Integer, String>> placeholders = placeholderSign.placeholders(); Map<Side, Map<Integer, String>> placeholders = placeholderSign.placeholders();
String[] frontLines = sign.getSide(Side.FRONT).getLines(); String[] frontLines = front.getLines();
String[] backLines = sign.getSide(Side.BACK).getLines(); String[] backLines = back.getLines();
// Only update the sign if the text has changed // Only update the sign if the text has changed
if (updatePlaceholders(frontLines, placeholders.get(Side.FRONT), sign.getSide(Side.FRONT)) || boolean updateNecessary = false;
updatePlaceholders(backLines, placeholders.get(Side.BACK), sign.getSide(Side.BACK))) { if (placeholders.get(Side.FRONT) != null) {
updateNecessary |= updatePlaceholders(frontLines, placeholders.get(Side.FRONT), front);
}
if (placeholders.get(Side.BACK) != null) {
updateNecessary |= updatePlaceholders(backLines, placeholders.get(Side.BACK), back);
}
if (updateNecessary) {
sign.update(); sign.update();
} }
} }

View File

@ -45,7 +45,9 @@ public class SignClickListener implements Listener {
// Check if the player has run the /editSign command // Check if the player has run the /editSign command
SignLineChangeRequest request = PlaceholderSigns.getInstance().getSignChangeRequest(player); SignLineChangeRequest request = PlaceholderSigns.getInstance().getSignChangeRequest(player);
if (request != null) { if (request != null) {
doSignChange(sign, request, event, player); SignSide standingOn = sign.getTargetSide(player);
Side side = sign.getSide(Side.FRONT).equals(standingOn) ? Side.FRONT : Side.BACK;
doSignChange(sign, request, event, player, side);
} }
} }
@ -85,17 +87,18 @@ public class SignClickListener implements Listener {
* @param player <p>The player triggering the sign change</p> * @param player <p>The player triggering the sign change</p>
*/ */
private void doSignChange(@NotNull Sign sign, @NotNull SignLineChangeRequest request, private void doSignChange(@NotNull Sign sign, @NotNull SignLineChangeRequest request,
@NotNull PlayerInteractEvent event, @NotNull Player player) { @NotNull PlayerInteractEvent event, @NotNull Player player, @NotNull Side side) {
// Cancel the event to prevent vanilla behavior // Cancel the event to prevent vanilla behavior
event.setCancelled(true); event.setCancelled(true);
String[] lines = sign.getSide(Side.FRONT).getLines(); SignSide signSide = sign.getSide(side);
String[] lines = signSide.getLines();
lines[request.line()] = request.text(); lines[request.line()] = request.text();
// Run the sign change event to allow protection plugins to cancel, and allow the sign text listener to trigger // Run the sign change event to allow protection plugins to cancel, and allow the sign text listener to trigger
SignChangeEvent changeEvent = new SignChangeEvent(Objects.requireNonNull(event.getClickedBlock()), SignChangeEvent changeEvent = new SignChangeEvent(Objects.requireNonNull(event.getClickedBlock()),
player, lines, Side.FRONT); player, lines, side);
Bukkit.getPluginManager().callEvent(changeEvent); Bukkit.getPluginManager().callEvent(changeEvent);
if (changeEvent.isCancelled()) { if (changeEvent.isCancelled()) {
return; return;
@ -104,7 +107,7 @@ public class SignClickListener implements Listener {
// Update the sign with the new text // Update the sign with the new text
String[] finalLines = changeEvent.getLines(); String[] finalLines = changeEvent.getLines();
for (int i = 0; i < finalLines.length; i++) { for (int i = 0; i < finalLines.length; i++) {
sign.getSide(Side.FRONT).setLine(i, ColorHelper.translateAllColorCodes(finalLines[i])); signSide.setLine(i, ColorHelper.translateAllColorCodes(finalLines[i]));
} }
sign.update(); sign.update();

View File

@ -53,7 +53,7 @@ public class SignTextListener implements Listener {
signHandler.registerSign(placeholderSign); signHandler.registerSign(placeholderSign);
} else if (!placeholders.isEmpty()) { } else if (!placeholders.isEmpty()) {
// Overwrite the placeholders of the existing placeholder sign // Overwrite the placeholders of the existing placeholder sign
Map<Integer, String> existing = existingSign.placeholders().get(event.getSide()); Map<Integer, String> existing = existingSign.placeholders().computeIfAbsent(event.getSide(), k -> new HashMap<>());
existing.putAll(placeholders); existing.putAll(placeholders);
signHandler.save(); signHandler.save();
} }