Skip to content

Commit

Permalink
MiniMessage usage improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Oct 9, 2021
1 parent 5d98617 commit 3cc6bae
Show file tree
Hide file tree
Showing 38 changed files with 315 additions and 372 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.9.0-SNAPSHOT</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.minimessage.MiniMessage;

public class BossBarUtils {
public record BossBarUtils(ConfigUtils config, MiniMessage mm) {
/**
* Based on the argument given in the command,
* it will return the color of the specified bossbar.
Expand Down Expand Up @@ -42,38 +43,36 @@ public BossBar.Overlay bossbarOverlay(String overlay){

// It will return if the given arguments are correct for the command to work correctly.
public boolean regularBossbarArgs(int length, Audience sender) {
ConfigUtils config = new ConfigUtils();
MiniMessageUtil mUtils = new MiniMessageUtil();
ConfigManager cManager = new ConfigManager();
// The command requires arguments to work
return switch (length) {
case 0 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.without-argument",
"<red>You need to enter the time, color and message arguments.</red>"))));
yield false;
}
case 1 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.only-time",
"<gray>You must enter the color and the message arguments.</gray>"))));
yield false;
}
case 2 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.overlay-missing",
"<gray>You must enter the overlay and the message arguments.</gray>"))));
yield false;
}
case 3 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.without-message",
"<gray>You need to enter the message to announce.</gray>"))));
Expand All @@ -84,45 +83,43 @@ public boolean regularBossbarArgs(int length, Audience sender) {
}

public boolean sendBossbarArgs(int length, Audience sender) {
ConfigUtils config = new ConfigUtils();
MiniMessageUtil mUtils = new MiniMessageUtil();
ConfigManager cManager = new ConfigManager();
return switch (length) {
case 0 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.without-argument",
"<red>You need to enter the time, color and message arguments.</red>"))));
yield false;
}
case 1 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.only-player",
"<gray>You must enter the message to be sent after the player's name.</gray>"))));
yield false;
}
case 2 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.only-time",
"<gray>You must enter the color, overlay and the message arguments.</gray>"))));
yield false;
}
case 3 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.overlay-missing",
"<gray>You must enter the overlay and the message arguments.</gray>"))));
yield false;
}
case 4 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.without-message",
"<gray>You need to enter the message to announce.</gray>"))));
Expand All @@ -133,45 +130,43 @@ public boolean sendBossbarArgs(int length, Audience sender) {
}

public boolean proxyBossbarArgs(int length, Audience sender) {
ConfigUtils config = new ConfigUtils();
MiniMessageUtil mUtils = new MiniMessageUtil();
ConfigManager cManager = new ConfigManager();
return switch (length) {
case 0 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.without-argument",
"<red>You need to enter the time, color and message arguments.</red>"))));
yield false;
}
case 1 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.general.no-server-provided",
"<red>No server provided to send the message</red>"))));
yield false;
}
case 2 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.only-time",
"<gray>You must enter the color, overlay and the message arguments.</gray>"))));
yield false;
}
case 3 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.overlay-missing",
"<gray>You must enter the overlay and the message arguments.</gray>"))));
yield false;
}
case 4 -> {
sender.sendMessage(
config.getPrefix().append(mUtils.parse(
config.getPrefix().append(mm.parse(
cManager.getConfig().getOrDefault(
"messages.bossbar.without-message",
"<gray>You need to enter the message to announce.</gray>"))));
Expand All @@ -185,9 +180,8 @@ public float validBossbarNumber(String number, Audience sender){
try {
return Integer.parseInt(number);
} catch (Exception e){
sender.sendMessage(
new ConfigUtils().getPrefix().append(
new MiniMessageUtil().parse("<dark_red>This is not a valid number")));
sender.sendMessage(config.getPrefix().append(
mm.parse("<dark_red>This is not a valid number")));
return 0.1f;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import de.leonhard.storage.Yaml;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;

public class ConfigUtils {
private Yaml config = new ConfigManager().getConfig();
private MiniMessage mm = MiniMessage.miniMessage();

public Component getPrefix(){
if (config.getOrDefault("messages.prefix.enabled", true)) {
return new MiniMessageUtil().parse(config.getOrDefault(
return mm.parse(config.getOrDefault(
"messages.prefix.line",
"<gray>[</gray><gradient:yellow:blue>TitleAnnouncer</gradient><gray>]</gray> "));
} else {
Expand All @@ -22,21 +24,21 @@ public Component getPrefix(){
-----------------------------*/

public void sendTitleError(Audience sender){
sender.sendMessage(getPrefix().append(new MiniMessageUtil().parse(
sender.sendMessage(getPrefix().append(mm.parse(
config.getOrDefault(
"messages.title.error",
"<dark_red>An error occurred while sending the title. Be sure to use the ';' to separate the title and the subtitle.</dark_red>"))));
}

public void sendNoArgumentMessage(Audience sender) {
sender.sendMessage(getPrefix().append(new MiniMessageUtil().parse(
sender.sendMessage(getPrefix().append(mm.parse(
config.getOrDefault(
"messages.title.without-argument",
"<red>You need to enter the title and subtitle arguments.</red>"))));
}

public void noTitlePlayerArgumentProvided(Audience sender){
sender.sendMessage(getPrefix().append(new MiniMessageUtil().parse(
sender.sendMessage(getPrefix().append(mm.parse(
config.getOrDefault(
"messages.title.only-player",
"<gray>You must enter the title and subtitle after the player's name to send the message correctly.</gray>"))));
Expand Down Expand Up @@ -66,15 +68,15 @@ public float getTitleSoundPitch(){

public void noActionbarArgumentProvided(Audience sender){
sender.sendMessage(
getPrefix().append(new MiniMessageUtil().parse(
getPrefix().append(mm.parse(
config.getOrDefault(
"messages.actionbar.without-argument",
"<red>You need to enter the message to announce.</red>"))));
}

public void noActionbarPlayerArgumentProvided(Audience sender){
sender.sendMessage(
getPrefix().append(new MiniMessageUtil().parse(
getPrefix().append(mm.parse(
config.getOrDefault(
"messages.actionbar.only-player",
"<gray>You must enter the message to be sent after the player's name.</gray>"))));
Expand Down Expand Up @@ -124,7 +126,7 @@ public float getBossbarSoundPitch(){
GENERAL CONFIGURATION
*/
public void sendConfirmation(ComponentType type, Audience sender){
sender.sendMessage(getPrefix().append(new MiniMessageUtil().parse(switch(type){
sender.sendMessage(getPrefix().append(mm.parse(switch(type){
case BOSSBAR -> config.getOrDefault(
"messages.bossbar.successfully",
"<green>Bossbar succesfully sended</green>");
Expand Down Expand Up @@ -171,51 +173,51 @@ public void playPaperSound(ComponentType type, Audience audience){
}

public void reloadMessage(Audience sender){
sender.sendMessage(new MiniMessageUtil().parse(
sender.sendMessage(mm.parse(
config.getOrDefault(
"messages.general.reload-config",
"<green>Config Reloaded</green>")));
}

public void invalidCommand(Audience sender){
sender.sendMessage(getPrefix().append(new MiniMessageUtil().parse(
sender.sendMessage(getPrefix().append(mm.parse(
config.getOrDefault(
"messages.general.invalid-command",
"<red>Unknown Command</red>"))));
}

public void helpPrefix(Audience sender){
sender.sendMessage(new MiniMessageUtil().parse(
sender.sendMessage(mm.parse(
config.getOrDefault(
"messages.general.help-message",
"<white>Available Commands:</white>")));
}

public void playerNotFoundMessage(Audience sender){
sender.sendMessage(getPrefix().append(new MiniMessageUtil().parse(
sender.sendMessage(getPrefix().append(mm.parse(
config.getOrDefault(
"messages.general.player-not-found",
"<red>Player not found</red>"))));
}

public void onlyPlayerExecute(Audience sender){
sender.sendMessage(getPrefix().append(new MiniMessageUtil().parse(
sender.sendMessage(getPrefix().append(mm.parse(
config.getOrDefault(
"messages.general.no-console",
"<red>The console cannot execute this command</red>")
)));
}

public void noServerArgumentProvided(Audience sender){
sender.sendMessage(getPrefix().append(new MiniMessageUtil().parse(
sender.sendMessage(getPrefix().append(mm.parse(
config.getOrDefault(
"messages.general.no-server-provided",
"<red>No server provided to send the message</red>")
)));
}

public void noServerFound(Audience sender){
sender.sendMessage(getPrefix().append(new MiniMessageUtil().parse(
sender.sendMessage(getPrefix().append(mm.parse(
config.getOrDefault(
"messages.general.server-not-found",
"<red>Server not found</red>")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,7 @@
package net.dreamerzero.titleannouncer.common.utils;

import java.util.List;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
/*
Component that parses the title or
actionbar with the MiniMessage format.
*/
public class MiniMessageUtil {

/**
* Will format the delivered text
* @param message
* @return The message with formatting
*/
public Component parse(
final String message) {

return MiniMessage.miniMessage().deserialize(message);
}

/**
* It will format the delivered text
* and replace a single placeholder.
* @param message
* @param template
* @return The formatted message and a replaced placeholder
*/
public Component parse(
final String message, Template template) {

return MiniMessage.miniMessage().parse(message, template);
}

/**
* Will format the delivered text
* with a list of placeholders.
* @param message
* @param template
* @return Message with formatting and a list of placeholders replaced
*/
public Component parse(
final String message, List<Template> template) {

return MiniMessage.miniMessage().parse(message, template);
}

public String replaceLegacy(String legacyText){
public static String replaceLegacy(String legacyText){
String newText = legacyText
.replaceAll("&1", "<dark_blue>")
.replaceAll("&2", "<dark_green>")
Expand Down
Loading

0 comments on commit 3cc6bae

Please sign in to comment.