Improves message formatting #6

This commit is contained in:
Kristian Knarvik 2023-07-07 18:45:11 +02:00
parent b0d76e8db7
commit 0b38cfaab5
9 changed files with 150 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package net.knarcraft.launchpad.command;
import net.knarcraft.launchpad.config.Message;
import net.knarcraft.launchpad.launchpad.ModificationAction;
import net.knarcraft.launchpad.launchpad.ModificationRequest;
import net.knarcraft.launchpad.launchpad.ModificationRequestHandler;
@ -19,7 +20,7 @@ public class LaunchpadCommand implements CommandExecutor {
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
if (!(commandSender instanceof Player player)) {
commandSender.sendMessage("This command can only be used by a player");
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage());
return false;
}
@ -46,7 +47,7 @@ public class LaunchpadCommand implements CommandExecutor {
case ABORT -> {
// Retrieving modification requests also removes them
ModificationRequestHandler.getRequests(player.getUniqueId());
commandSender.sendMessage("Launchpad modifications cleared");
commandSender.sendMessage(Message.SUCCESS_MODIFICATIONS_CLEARED.getMessage());
return true;
}
case VELOCITIES -> {
@ -57,7 +58,7 @@ public class LaunchpadCommand implements CommandExecutor {
}
}
commandSender.sendMessage("Right-click the block to modify launchpad properties for");
commandSender.sendMessage(Message.SUCCESS_CLICK_BLOCK.getMessage());
return true;
}

View File

@ -1,6 +1,7 @@
package net.knarcraft.launchpad.command;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.config.Message;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
@ -18,7 +19,7 @@ public class ReloadCommand implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
Launchpad.getInstance().reload();
commandSender.sendMessage("Launchpad reloaded");
commandSender.sendMessage(Message.SUCCESS_PLUGIN_RELOADED.getMessage());
return true;
}

View File

@ -0,0 +1,99 @@
package net.knarcraft.launchpad.config;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.util.ColorHelper;
import org.jetbrains.annotations.NotNull;
/**
* A message which ca be displayed to the user
*/
public enum Message {
/* ************** *
* Error messages *
* ************** */
/**
* The message displayed if the console tries to execute a player-only command
*/
ERROR_PLAYER_ONLY("&cThis command must be used by a player"),
/**
* The message displayed if the player tries to modify launchpad property for a block outside the whitelist
*/
ERROR_NOT_WHITELISTED("&cThe block could not be modified, as it's not whitelisted. If you want to " +
"abort changing a launchpad, use \"/launchpad abort\""),
/**
* The message displayed if an un-parse-able message is given by a user
*/
ERROR_MATERIAL_NOT_PARSE_ABLE("&cUnable to parse material: {material}"),
/* **************** *
* Success messages *
* **************** */
/**
* The message displayed if the Launchpad plugin is reloaded
*/
SUCCESS_PLUGIN_RELOADED("&aPlugin reloaded!"),
/**
* The message to display when a player uses /launchpad clear
*/
SUCCESS_MODIFICATIONS_CLEARED("&aCleared your launchpad modification queue"),
/**
* The message to display to prompt the player to click a launchpad
*/
SUCCESS_CLICK_BLOCK("&aClick the launchpad you want to create or modify"),
/**
* The message to display when a launchpad has been added or modified
*/
SUCCESS_MODIFIED_LAUNCHPAD("&aThe clicked block's launchpad properties have been modified"),
;
private final @NotNull String defaultMessage;
/**
* Instantiates a new message
*
* @param defaultMessage <p>The default value of the message</p>
*/
Message(@NotNull String defaultMessage) {
this.defaultMessage = defaultMessage;
}
/**
* Gets the message this enum represents
*
* @return <p>The formatted message</p>
*/
public @NotNull String getMessage() {
return formatMessage(this.defaultMessage);
}
/**
* Gets the message this enum represents
*
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement to use</p>
* @return <p>The formatted message</p>
*/
public @NotNull String getMessage(@NotNull String placeholder, @NotNull String replacement) {
return formatMessage(this.defaultMessage.replace(placeholder, replacement));
}
/**
* Gets the formatted version of the given message
*
* @param message <p>The message to format</p>
* @return <p>The formatted message</p>
*/
private @NotNull String formatMessage(@NotNull String message) {
String prefix = Launchpad.getInstance().getDescription().getPrefix();
return ColorHelper.translateAllColorCodes("#FFE34C[&r&l" + prefix + "#FFE34C]&r " + message);
}
}

View File

@ -1,6 +1,7 @@
package net.knarcraft.launchpad.listener;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.config.Message;
import net.knarcraft.launchpad.launchpad.LaunchpadBlock;
import net.knarcraft.launchpad.launchpad.LaunchpadBlockHandler;
import net.knarcraft.launchpad.launchpad.ModificationRequest;
@ -49,10 +50,9 @@ public class LaunchpadModifyListener implements Listener {
event.setUseItemInHand(Event.Result.DENY);
event.setUseInteractedBlock(Event.Result.DENY);
event.getPlayer().sendMessage("Modified launchpad!");
event.getPlayer().sendMessage(Message.SUCCESS_MODIFIED_LAUNCHPAD.getMessage());
} else {
event.getPlayer().sendMessage("The block could not be modified, as it's not whitelisted. If you want " +
"to abort changing a launchpad, use \"/launchpad abort\"");
event.getPlayer().sendMessage(Message.ERROR_NOT_WHITELISTED.getMessage());
}
}

View File

@ -195,7 +195,8 @@ public class ParticleSpawner implements Runnable {
// Spawn particles on the stored locations, relative to the launchpad
for (double[] circleCoordinate : circleCoordinates) {
spawnParticle(world, location.clone().add(circleCoordinate[0], getParticleConfig().getHeightOffset(), circleCoordinate[1]));
spawnParticle(world, location.clone().add(circleCoordinate[0], getParticleConfig().getHeightOffset(),
circleCoordinate[1]));
}
}

View File

@ -0,0 +1,29 @@
package net.knarcraft.launchpad.util;
import net.md_5.bungee.api.ChatColor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A helper class for converting colors
*/
public class 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

@ -1,6 +1,7 @@
package net.knarcraft.launchpad.util;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.config.Message;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
@ -56,7 +57,8 @@ public final class MaterialHelper {
if (matched != null) {
parsedMaterials.add(matched);
} else {
Launchpad.log(Level.WARNING, "&cUnable to parse material: " + materialString);
Launchpad.log(Level.WARNING, Message.ERROR_MATERIAL_NOT_PARSE_ABLE.getMessage("{material}",
materialString));
}
return parsedMaterials;
}
@ -78,7 +80,8 @@ public final class MaterialHelper {
if (tag != null) {
targetSet.addAll(tag.getValues());
} else {
Launchpad.log(Level.WARNING, "Unable to parse: " + materialName);
Launchpad.log(Level.WARNING, Message.ERROR_MATERIAL_NOT_PARSE_ABLE.getMessage("{material}",
materialName));
}
return true;
}

View File

@ -97,7 +97,7 @@ launchpad:
#- SMOKE_NORMAL # While cool, it leaves a short trail compared to other particles
- SNEEZE
- SNOWFLAKE
- SONIC_BOOM
#- SONIC_BOOM # Cool, but a bit short and extreme
- SOUL
- SOUL_FIRE_FLAME
- SPELL

View File

@ -5,6 +5,7 @@ api-version: '1.20'
author: EpicKnarvik97
description: A plugin that adds customizable launchpads to the game
website: https://git.knarcraft.net
prefix: Launchpad
commands:
reload:
@ -12,7 +13,10 @@ commands:
description: Used to reload the Launchpad plugin
permission: launchpad.reload
launchpad:
usage: /<command> <add | remove | abort | verticalVelocity | horizontalVelocity | fixedDirection> [value]
usage: |
/<command> <add | remove | abort>
/<command> <verticalVelocity | horizontalVelocity | fixedDirection> <value>
/<command> velocities <horizontalVelocity> <verticalVelocity>
description: Used to alter launchpads
permission: launchpad.modify