Fixes #2
All checks were successful
KnarCraft/PlaceholderSigns/pipeline/head This commit looks good

Makes the edit command able to remove placeholders from signs. Editing the sign normally does not remove the placeholder, as there is no way to differentiate between a player intending to remove a placeholder, and a player not intending to remove a placeholder.
This commit is contained in:
Kristian Knarvik 2024-04-21 22:05:09 +02:00
parent e8c93baac4
commit 080e6204f4
4 changed files with 32 additions and 3 deletions

View File

@ -61,9 +61,10 @@ public class EditSignCommand implements TabExecutor {
// Register the line change request
SignLineChangeRequest request = new SignLineChangeRequest(player, lineNumber - 1, builder.toString());
PlaceholderSigns.getInstance().getRequestHandler().addSignChangeRequest(request);
PlaceholderSigns placeholderSigns = PlaceholderSigns.getInstance();
placeholderSigns.getRequestHandler().addSignChangeRequest(request);
PlaceholderSigns.getInstance().getStringFormatter().displaySuccessMessage(commandSender,
placeholderSigns.getStringFormatter().displaySuccessMessage(commandSender,
PlaceholderSignMessage.SUCCESS_CLICK_SIGN_TO_EDIT);
return true;
}

View File

@ -17,7 +17,7 @@ import java.util.List;
*/
public class ViewSignCommand implements TabExecutor {
boolean raw;
private final boolean raw;
/**
* Instantiates a new view sign command

View File

@ -6,7 +6,9 @@ import net.knarcraft.knarlib.property.ColorConversion;
import net.knarcraft.knarlib.util.ColorHelper;
import net.knarcraft.placeholdersigns.PlaceholderSigns;
import net.knarcraft.placeholdersigns.config.PlaceholderSignMessage;
import net.knarcraft.placeholdersigns.container.PlaceholderSign;
import net.knarcraft.placeholdersigns.container.SignLineChangeRequest;
import net.knarcraft.placeholdersigns.handler.PlaceholderSignHandler;
import net.knarcraft.placeholdersigns.handler.PlaceholderSignRequestHandler;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
@ -160,6 +162,14 @@ public class SignClickListener implements Listener {
SignSide signSide = sign.getSide(side);
String[] lines = signSide.getLines();
PlaceholderSignHandler signHandler = PlaceholderSigns.getInstance().getSignHandler();
PlaceholderSign placeholderSign = PlaceholderSigns.getInstance().getSignHandler().getFromLocation(sign.getLocation());
String oldPlaceholder = null;
if (placeholderSign != null) {
// Remove the old placeholder
oldPlaceholder = placeholderSign.placeholders().get(side).remove(request.line());
}
lines[request.line()] = request.text();
// Run the sign change event to allow protection plugins to cancel, and allow the sign text listener to trigger
@ -167,6 +177,10 @@ public class SignClickListener implements Listener {
player, lines, side);
Bukkit.getPluginManager().callEvent(changeEvent);
if (changeEvent.isCancelled()) {
if (placeholderSign != null) {
// Restore the old placeholder if the action didn't complete
placeholderSign.placeholders().get(side).put(request.line(), oldPlaceholder);
}
return;
}
@ -177,6 +191,9 @@ public class SignClickListener implements Listener {
}
sign.update();
// Save any placeholder changes
signHandler.save();
PlaceholderSigns.getInstance().getStringFormatter().displaySuccessMessage(player,
PlaceholderSignMessage.SUCCESS_SIGN_CHANGED);
}

View File

@ -40,6 +40,17 @@ public class SignTextListener implements Listener {
placeholders.put(i, line);
}
updatePlaceholders(placeholders, event);
}
/**
* Updates placeholders as necessary
*
* @param placeholders <p>The placeholders parsed from the lines</p>
* @param event <p>The triggered sign change event</p>
*/
private void updatePlaceholders(@NotNull Map<Integer, String> placeholders,
@NotNull SignChangeEvent event) {
Location location = event.getBlock().getLocation();
PlaceholderSignHandler signHandler = PlaceholderSigns.getInstance().getSignHandler();
PlaceholderSign existingSign = signHandler.getFromLocation(location);