Updates KnarLib
All checks were successful
KnarCraft/PermissionSigns/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-01-25 15:35:07 +01:00
parent a3578fe754
commit d082e8348a
9 changed files with 38 additions and 41 deletions

View File

@ -112,7 +112,7 @@
<dependency> <dependency>
<groupId>net.knarcraft</groupId> <groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId> <artifactId>knarlib</artifactId>
<version>1.2.4</version> <version>1.2.5</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -55,7 +55,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
public final class PermissionSigns extends JavaPlugin { public final class PermissionSigns extends JavaPlugin {
private static PermissionSigns instance; private static PermissionSigns instance;
private Translator translator;
private StringFormatter stringFormatter; private StringFormatter stringFormatter;
private Configuration configuration; private Configuration configuration;
@ -77,21 +76,12 @@ public final class PermissionSigns extends JavaPlugin {
return instance; return instance;
} }
/**
* Gets the translator
*
* @return <p>The translator</p>
*/
public @NotNull Translator getTranslator() {
return translator;
}
/** /**
* Gets the string formatter * Gets the string formatter
* *
* @return <p>The string formatter</p> * @return <p>The string formatter</p>
*/ */
public @NotNull StringFormatter stringFormatter() { public @NotNull StringFormatter getStringFormatter() {
return stringFormatter; return stringFormatter;
} }
@ -129,7 +119,7 @@ public final class PermissionSigns extends JavaPlugin {
this.saveDefaultConfig(); this.saveDefaultConfig();
config.options().copyDefaults(true); config.options().copyDefaults(true);
translator = new Translator(); Translator translator = new Translator();
translator.registerMessageCategory(Translatable.ALREADY_HAS_PERMISSIONS); translator.registerMessageCategory(Translatable.ALREADY_HAS_PERMISSIONS);
stringFormatter = new StringFormatter(this.getDescription().getName(), translator); stringFormatter = new StringFormatter(this.getDescription().getName(), translator);
configuration = new Configuration(this.getConfig(), translator, this.getDataFolder()); configuration = new Configuration(this.getConfig(), translator, this.getDataFolder());

View File

@ -16,7 +16,7 @@ public class AboutCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) { @NotNull String[] args) {
StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
stringFormatter.displaySuccessMessage(sender, "Permission Signs plugin created by " + ChatColor.GOLD + stringFormatter.displaySuccessMessage(sender, "Permission Signs plugin created by " + ChatColor.GOLD +
"EpicKnarvik97"); "EpicKnarvik97");
stringFormatter.displaySuccessMessage(sender, "Plugin version: " + stringFormatter.displaySuccessMessage(sender, "Plugin version: " +

View File

@ -18,7 +18,7 @@ public class CancelCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) { @NotNull String[] args) {
StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
if (sender instanceof Player player) { if (sender instanceof Player player) {
if (sender.hasPermission("permissionsigns.admin.create")) { if (sender.hasPermission("permissionsigns.admin.create")) {

View File

@ -26,7 +26,7 @@ public class CreateCommand implements CommandExecutor {
//Name and permission(s) required, but duration and cost optional //Name and permission(s) required, but duration and cost optional
String usage = "/ps create <name> <description> <permission,permission> [cost] [duration] - Used for creating" + String usage = "/ps create <name> <description> <permission,permission> [cost] [duration] - Used for creating" +
" a new permission sign"; " a new permission sign";
StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
if (!(sender instanceof Player)) { if (!(sender instanceof Player)) {
stringFormatter.displayErrorMessage(sender, Translatable.COMMAND_PLAYER_ONLY); stringFormatter.displayErrorMessage(sender, Translatable.COMMAND_PLAYER_ONLY);
@ -64,7 +64,7 @@ public class CreateCommand implements CommandExecutor {
String name = arguments[0]; String name = arguments[0];
String description = replaceUnderscoresWithSpaces(arguments[1]); String description = replaceUnderscoresWithSpaces(arguments[1]);
String[] permissions = arguments[2].replaceAll("\\?", " ").split(","); String[] permissions = arguments[2].replaceAll("\\?", " ").split(",");
StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
for (String permission : permissions) { for (String permission : permissions) {
if (!permission.contains(":")) { if (!permission.contains(":")) {

View File

@ -18,7 +18,7 @@ public class ReloadCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) { @NotNull String[] args) {
StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
if (sender.hasPermission("permissionsigns.admin.reload")) { if (sender.hasPermission("permissionsigns.admin.reload")) {
SignManager.loadSigns(); SignManager.loadSigns();
PermissionManager.loadTemporaryPermissions(); PermissionManager.loadTemporaryPermissions();

View File

@ -1,7 +1,6 @@
package net.knarcraft.permissionsigns.container; package net.knarcraft.permissionsigns.container;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.Translator;
import net.knarcraft.permissionsigns.PermissionSigns; import net.knarcraft.permissionsigns.PermissionSigns;
import net.knarcraft.permissionsigns.config.Translatable; import net.knarcraft.permissionsigns.config.Translatable;
import net.knarcraft.permissionsigns.manager.EconomyManager; import net.knarcraft.permissionsigns.manager.EconomyManager;
@ -147,10 +146,10 @@ public class PermissionSign {
* @return <p>The lines used to draw this permission sign</p> * @return <p>The lines used to draw this permission sign</p>
*/ */
public @NotNull String[] getSignLines() { public @NotNull String[] getSignLines() {
Translator translator = PermissionSigns.getInstance().getTranslator(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
String[] lines = new String[4]; String[] lines = new String[4];
lines[0] = translator.getTranslatedMessage(Translatable.SIGN_PREFIX); lines[0] = stringFormatter.getUnformattedColoredMessage(Translatable.SIGN_PREFIX);
lines[1] = StringFormatter.replacePlaceholder(translator.getTranslatedMessage( lines[1] = StringFormatter.replacePlaceholder(stringFormatter.getUnformattedColoredMessage(
Translatable.SIGN_NAME_FORMAT), "{name}", getName()); Translatable.SIGN_NAME_FORMAT), "{name}", getName());
lines[2] = getDurationString(); lines[2] = getDurationString();
lines[3] = getCostString(); lines[3] = getCostString();
@ -176,7 +175,8 @@ public class PermissionSign {
*/ */
public @NotNull String getDurationString() { public @NotNull String getDurationString() {
if (duration == 0) { if (duration == 0) {
return PermissionSigns.getInstance().getTranslator().getTranslatedMessage(Translatable.SIGN_PERMANENT); return PermissionSigns.getInstance().getStringFormatter().getUnformattedColoredMessage(
Translatable.SIGN_PERMANENT);
} else { } else {
if (sortedUnits == null) { if (sortedUnits == null) {
initializeUnits(); initializeUnits();
@ -199,7 +199,8 @@ public class PermissionSign {
*/ */
public @NotNull String getCostString() { public @NotNull String getCostString() {
if (cost == 0) { if (cost == 0) {
return PermissionSigns.getInstance().getTranslator().getTranslatedMessage(Translatable.SIGN_COST_FREE); return PermissionSigns.getInstance().getStringFormatter().getUnformattedColoredMessage(
Translatable.SIGN_COST_FREE);
} else { } else {
return EconomyManager.format(cost); return EconomyManager.format(cost);
} }
@ -225,10 +226,10 @@ public class PermissionSign {
*/ */
private @NotNull String formatDurationString(double duration, @NotNull Translatable translatable, private @NotNull String formatDurationString(double duration, @NotNull Translatable translatable,
boolean castToInt) { boolean castToInt) {
Translator translator = PermissionSigns.getInstance().getTranslator(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
String durationFormat = translator.getTranslatedMessage(Translatable.SIGN_DURATION_FORMAT); String durationFormat = stringFormatter.getUnformattedColoredMessage(Translatable.SIGN_DURATION_FORMAT);
durationFormat = StringFormatter.replacePlaceholder(durationFormat, "{unit}", durationFormat = StringFormatter.replacePlaceholder(durationFormat, "{unit}",
translator.getTranslatedMessage(translatable)); stringFormatter.getUnformattedColoredMessage(translatable));
return StringFormatter.replacePlaceholder(durationFormat, "{duration}", return StringFormatter.replacePlaceholder(durationFormat, "{duration}",
castToInt ? String.valueOf((int) duration) : String.valueOf(duration)); castToInt ? String.valueOf((int) duration) : String.valueOf(duration));
} }

View File

@ -190,7 +190,7 @@ public class BlockListener implements Listener {
return; return;
} }
StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
if (!player.hasPermission("permissionsigns.admin.create")) { if (!player.hasPermission("permissionsigns.admin.create")) {
event.setCancelled(true); event.setCancelled(true);
stringFormatter.displayErrorMessage(player, Translatable.PERMISSION_SIGN_DESTROY_DENY); stringFormatter.displayErrorMessage(player, Translatable.PERMISSION_SIGN_DESTROY_DENY);

View File

@ -1,7 +1,7 @@
package net.knarcraft.permissionsigns.listener; package net.knarcraft.permissionsigns.listener;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.knarlib.formatting.StringReplacer;
import net.knarcraft.permissionsigns.PermissionSigns; import net.knarcraft.permissionsigns.PermissionSigns;
import net.knarcraft.permissionsigns.config.Translatable; import net.knarcraft.permissionsigns.config.Translatable;
import net.knarcraft.permissionsigns.container.PermissionSign; import net.knarcraft.permissionsigns.container.PermissionSign;
@ -77,7 +77,7 @@ public class SignListener implements Listener {
return; return;
} }
if (!player.hasPermission("permissionsigns.use")) { if (!player.hasPermission("permissionsigns.use")) {
PermissionSigns.getInstance().stringFormatter().displayErrorMessage(player, PermissionSigns.getInstance().getStringFormatter().displayErrorMessage(player,
Translatable.INTERACT_PERMISSION_MISSING); Translatable.INTERACT_PERMISSION_MISSING);
return; return;
} }
@ -105,22 +105,28 @@ public class SignListener implements Listener {
* @return <p>The information to display</p> * @return <p>The information to display</p>
*/ */
private String getSignInfoText(@NotNull PermissionSign sign) { private String getSignInfoText(@NotNull PermissionSign sign) {
Translator translator = PermissionSigns.getInstance().getTranslator(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
String rawSignInfo = translator.getTranslatedMessage(Translatable.SIGN_INFO); String rawSignInfo = stringFormatter.getUnformattedColoredMessage(Translatable.SIGN_INFO);
StringBuilder permissionString = new StringBuilder(); StringBuilder permissionString = new StringBuilder();
List<String> permissionNodes = sign.getPermissionNodes(); List<String> permissionNodes = sign.getPermissionNodes();
if (permissionNodes.size() == 1) { if (permissionNodes.size() == 1) {
permissionString = new StringBuilder(permissionNodes.get(0)); permissionString = new StringBuilder(permissionNodes.get(0));
} else { } else {
for (String permissionNode : permissionNodes) { for (String permissionNode : permissionNodes) {
String format = translator.getTranslatedMessage(Translatable.SIGN_INFO_PERMISSION_FORMAT); String format = stringFormatter.getUnformattedColoredMessage(Translatable.SIGN_INFO_PERMISSION_FORMAT);
permissionString.append(StringFormatter.replacePlaceholder(format, "{permission}", permissionNode)); permissionString.append(StringFormatter.replacePlaceholder(format, "{permission}", permissionNode));
} }
} }
return StringFormatter.replacePlaceholders(rawSignInfo, new String[]{"{Name}", "{Description}", "{Permissions}", StringReplacer replacer = new StringReplacer(rawSignInfo);
"{Cost}", "{Duration}"}, new String[]{sign.getName(), sign.getDescription(), replacer.add("{Name}", sign.getName());
permissionString.toString(), sign.getCostString(), sign.getDurationString()}); replacer.add("{Description}", sign.getDescription());
replacer.add("{Permissions}", permissionString.toString());
replacer.add("{Cost}", sign.getCostString());
replacer.add("{Duration}", sign.getDurationString());
return replacer.replace();
} }
/** /**
@ -142,7 +148,7 @@ public class SignListener implements Listener {
hasAllPermissions = false; hasAllPermissions = false;
} }
} }
StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
if (hasAllPermissions) { if (hasAllPermissions) {
stringFormatter.displayErrorMessage(player, Translatable.ALREADY_HAS_PERMISSIONS); stringFormatter.displayErrorMessage(player, Translatable.ALREADY_HAS_PERMISSIONS);
return; return;
@ -183,12 +189,12 @@ public class SignListener implements Listener {
} }
} }
StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); StringFormatter stringFormatter = PermissionSigns.getInstance().getStringFormatter();
String grantedPermissions = permissionsJoiner.toString(); String grantedPermissions = permissionsJoiner.toString();
if (permissionSign.getDuration() > 0) { if (permissionSign.getDuration() > 0) {
stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholders( stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholders(
Translatable.PERMISSIONS_GRANTED, new String[]{"{permissions}", "{time}"}, Translatable.PERMISSIONS_GRANTED, List.of("{permissions}", "{time}"),
new String[]{grantedPermissions, permissionSign.getDurationString()})); List.of(grantedPermissions, permissionSign.getDurationString())));
} else { } else {
stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholder( stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholder(
Translatable.PERMISSIONS_GRANTED_PERMANENTLY, "{permissions}", grantedPermissions)); Translatable.PERMISSIONS_GRANTED_PERMANENTLY, "{permissions}", grantedPermissions));
@ -225,7 +231,7 @@ public class SignListener implements Listener {
sign.setWaxed(true); sign.setWaxed(true);
sign.update(); sign.update();
PermissionSigns.getInstance().stringFormatter().displaySuccessMessage(player, PermissionSigns.getInstance().getStringFormatter().displaySuccessMessage(player,
Translatable.PERMISSION_SIGN_CREATED); Translatable.PERMISSION_SIGN_CREATED);
} }