Minor improvements
All checks were successful
EpicKnarvik97/PlaceholderSigns/pipeline/head This commit looks good

Makes /editsign convert formatting codes for non-placeholder text
Makes /editsign able to clear a line on a sign
This commit is contained in:
Kristian Knarvik 2023-04-07 19:15:40 +02:00
parent ac8c418991
commit 837eac46b7
5 changed files with 49 additions and 24 deletions

View File

@ -8,7 +8,7 @@ import net.knarcraft.placeholdersigns.handler.PlaceholderSignHandler;
import net.knarcraft.placeholdersigns.listener.SignBreakListener; import net.knarcraft.placeholdersigns.listener.SignBreakListener;
import net.knarcraft.placeholdersigns.listener.SignClickListener; import net.knarcraft.placeholdersigns.listener.SignClickListener;
import net.knarcraft.placeholdersigns.listener.SignTextListener; import net.knarcraft.placeholdersigns.listener.SignTextListener;
import net.md_5.bungee.api.ChatColor; import net.knarcraft.placeholdersigns.util.ColorHelper;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
@ -22,8 +22,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* This plugin's main class * This plugin's main class
@ -146,7 +144,7 @@ public final class PlaceholderSigns extends JavaPlugin {
} }
// Convert color codes // Convert color codes
newText = translateAllColorCodes(newText); newText = ColorHelper.translateAllColorCodes(newText);
// Only change the line if the text has changed // Only change the line if the text has changed
if (!newText.equals(oldText)) { if (!newText.equals(oldText)) {
@ -162,20 +160,5 @@ public final class PlaceholderSigns extends JavaPlugin {
} }
} }
/**
* Translates all found color codes to formatting in a string
*
* @param message <p>The string to search for color codes</p>
* @return <p>The message with color codes translated</p>
*/
private static String translateAllColorCodes(String message) {
message = ChatColor.translateAlternateColorCodes('&', message);
Pattern pattern = Pattern.compile("&?(#[a-fA-F0-9]{6})");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group(1)));
}
return message;
}
} }

View File

@ -29,7 +29,7 @@ public class EditSignCommand implements TabExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] args) { @NotNull String[] args) {
if (args.length < 2 || !(commandSender instanceof Player player)) { if (args.length < 1 || !(commandSender instanceof Player player)) {
return false; return false;
} }
@ -44,8 +44,16 @@ public class EditSignCommand implements TabExecutor {
return false; return false;
} }
// Make sure no argument is treated as an empty string
String start;
if (args.length > 1) {
start = args[1];
} else {
start = "";
}
// Get all arguments as a space-separated string // Get all arguments as a space-separated string
StringBuilder builder = new StringBuilder(args[1]); StringBuilder builder = new StringBuilder(start);
for (int i = 2; i < args.length; i++) { for (int i = 2; i < args.length; i++) {
builder.append(" ").append(args[i]); builder.append(" ").append(args[i]);
} }

View File

@ -2,6 +2,7 @@ package net.knarcraft.placeholdersigns.listener;
import net.knarcraft.placeholdersigns.PlaceholderSigns; import net.knarcraft.placeholdersigns.PlaceholderSigns;
import net.knarcraft.placeholdersigns.container.LineChangeRequest; import net.knarcraft.placeholdersigns.container.LineChangeRequest;
import net.knarcraft.placeholdersigns.util.ColorHelper;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -43,7 +44,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.setLine(i, finalLines[i]); sign.setLine(i, ColorHelper.translateAllColorCodes(finalLines[i]));
} }
sign.update(); sign.update();

View File

@ -0,0 +1,33 @@
package net.knarcraft.placeholdersigns.util;
import net.md_5.bungee.api.ChatColor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A helper class for dealing with colors and formatting codes
*/
public final class ColorHelper {
private ColorHelper() {
}
/**
* Translates all found color codes to formatting in a string
*
* @param message <p>The string to search for color codes</p>
* @return <p>The message with color codes translated</p>
*/
public static String translateAllColorCodes(String message) {
message = ChatColor.translateAlternateColorCodes('&', message);
Pattern pattern = Pattern.compile("&?(#[a-fA-F0-9]{6})");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group(1)));
}
return message;
}
}

View File

@ -7,9 +7,9 @@ depend:
commands: commands:
editSign: editSign:
usage: /<command> <line> <text> [text] ... usage: /<command> <line> [text] [text] ...
permission: placeholdersigns.edit permission: placeholdersigns.edit
description: Changes the line of a sign without a text limit description: Changes the line of a sign, without a text limit, and with color conversion
permissions: permissions:
placeholdersigns.*: placeholdersigns.*: