Skip to content

Commit

Permalink
Finish colour translations
Browse files Browse the repository at this point in the history
Update config with new MiniMessage support
  • Loading branch information
IanTapply22 committed Jun 7, 2024
1 parent 2d7a4bf commit 0b31ecb
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 23 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId>
<version>4.17.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/jeqo/bloons/commands/CommandEquip.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import net.jeqo.bloons.commands.manager.enums.CommandPermission;
import net.jeqo.bloons.utils.BalloonManagement;
import net.jeqo.bloons.utils.MessageTranslations;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Sound;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -36,13 +38,13 @@ public boolean execute(CommandSender sender, String[] args) {
MessageTranslations messageTranslations = new MessageTranslations(this.plugin);

if (!this.plugin.getConfig().contains("balloons." + balloonID)) {
Component balloonNotFoundMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("balloon-not-found"));
Component balloonNotFoundMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("balloon-not-found"));
player.sendMessage(balloonNotFoundMessage);
return false;
}

if (!player.hasPermission(this.plugin.getConfig().getString("balloons." + balloonID + ".permission", "balloons." + balloonID))) {
Component noPermissionMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("no-permission"));
Component noPermissionMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("no-permission"));
player.sendMessage(noPermissionMessage);
return false;
}
Expand All @@ -52,7 +54,7 @@ public boolean execute(CommandSender sender, String[] args) {
player.playSound(player.getLocation(), Sound.ENTITY_CHICKEN_EGG, 1, 1);

String balloonName = messageTranslations.getString("balloons." + balloonID + ".name");
Component equippedMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("equipped", balloonName));
Component equippedMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("equipped", balloonName));
player.sendMessage(equippedMessage);

return false;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/jeqo/bloons/commands/CommandForceEquip.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ public boolean execute(CommandSender sender, String[] args) {
MessageTranslations messageTranslations = new MessageTranslations(this.plugin);

if (player == null) {
Component playerNotFoundMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("player-not-found"));
Component playerNotFoundMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("player-not-found"));
sender.sendMessage(playerNotFoundMessage);
return false;
}

String balloonID = args[1];
if (!this.plugin.getConfig().contains("balloons." + balloonID)) {
Component balloonNotFoundMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("balloon-not-found"));
Component balloonNotFoundMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("balloon-not-found"));
sender.sendMessage(balloonNotFoundMessage);
return false;
}

SingleBalloon.checkBalloonRemovalOrAdd(player.getPlayer(), balloonID);
String balloonName = messageTranslations.getString("balloons." + balloonID + ".name");
Component equippedMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("equipped", balloonName));
Component equippedMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("equipped", balloonName));
player.sendMessage(equippedMessage);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ public boolean execute(CommandSender sender, String[] args) {
Player player = Bukkit.getPlayer(args[0]);
MessageTranslations messageTranslations = new MessageTranslations(this.plugin);
if (player == null) {
Component playerNotFoundMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("player-not-found"));
Component playerNotFoundMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("player-not-found"));
sender.sendMessage(playerNotFoundMessage);
return false;
}

SingleBalloon owner = Bloons.playerBalloons.get(player.getUniqueId());
if (owner == null) {
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO, 1, 1);
Component notEquippedMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("not-equipped"));
Component notEquippedMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("not-equipped"));
sender.sendMessage(notEquippedMessage);
return false;
}
BalloonManagement.removeBalloon(player, owner);
Component unequipSuccessfulMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("unequipped"));
Component unequipSuccessfulMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("unequipped"));
sender.sendMessage(unequipSuccessfulMessage);
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/jeqo/bloons/commands/CommandReload.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import net.jeqo.bloons.commands.manager.Command;
import net.jeqo.bloons.commands.manager.enums.CommandPermission;
import net.jeqo.bloons.utils.MessageTranslations;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -24,7 +26,7 @@ public boolean execute(CommandSender sender, String[] args) {
MessageTranslations messageTranslations = new MessageTranslations(this.plugin);

Bloons.getInstance().reloadConfig();
Component configReloadedMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("config-reloaded"));
Component configReloadedMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("config-reloaded"));
sender.sendMessage(configReloadedMessage);

return false;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/jeqo/bloons/commands/CommandUnequip.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public boolean execute(CommandSender sender, String[] args) {

if (singleBalloon == null) {
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO, 1, 1);
Component notEquippedMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("not-equipped"));
Component notEquippedMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("not-equipped"));
player.sendMessage(notEquippedMessage);
return false;
}

BalloonManagement.removeBalloon(player, singleBalloon);
Component unequipSuccessfulMessage = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("unequipped"));
Component unequipSuccessfulMessage = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("unequipped"));
player.sendMessage(unequipSuccessfulMessage);
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_HURT_SWEET_BERRY_BUSH, 1, 1);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public boolean onCommand(@NotNull CommandSender sender, org.bukkit.command.@NotN
}

if (!player.hasPermission("bloons.menu")) {
Component noPermission = Component.text(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("no-permission"));
Component noPermission = messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("no-permission"));
player.sendMessage(noPermission);
return true;
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public boolean onCommand(@NotNull CommandSender sender, org.bukkit.command.@NotN
for (Command currentCommand : getCommands()) {
if (currentCommand.getCommandAliases().contains(subcommand)) {
if (!meetsRequirements(currentCommand, sender)) {
sender.sendMessage(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("no-permission"));
sender.sendMessage(messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("no-permission")));
return false;
}

Expand Down Expand Up @@ -194,8 +194,9 @@ private void setBalloonLore(ItemMeta meta, ConfigurationSection keySection) {
*/
private void setBalloonDisplayName(ItemMeta meta, ConfigurationSection keySection) {
String name = keySection.getString("name");
MessageTranslations messageTranslations = new MessageTranslations(this.plugin);
if (name != null) {
meta.displayName(Component.text(ColorManagement.fromHex(name)));
meta.displayName(messageTranslations.getSerializedString(name));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/jeqo/bloons/gui/menus/BalloonMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ private Inventory getBlankPage(String name){
ItemStack nextPage = new ItemStack(Material.valueOf(messageTranslations.getString("buttons.next-page.material")));
ItemMeta nextMeta = nextPage.getItemMeta();
assert nextMeta != null;
nextMeta.setDisplayName(ColorManagement.fromHex(messageTranslations.getString("buttons.next-page.name")));;
nextMeta.displayName(messageTranslations.getSerializedString(messageTranslations.getString("buttons.next-page.name")));
nextMeta.setCustomModelData(messageTranslations.getInt("buttons.next-page.custom-model-data"));
nextPage.setItemMeta(nextMeta);

ItemStack prevPage = new ItemStack(Material.valueOf(messageTranslations.getString("buttons.previous-page.material")));
ItemMeta prevMeta = prevPage.getItemMeta();
assert prevMeta != null;
prevMeta.setDisplayName(ColorManagement.fromHex(messageTranslations.getString("buttons.previous-page.name")));;
prevMeta.displayName(messageTranslations.getSerializedString(messageTranslations.getString("buttons.previous-page.name")));;
prevMeta.setCustomModelData(messageTranslations.getInt("buttons.previous-page.custom-model-data"));
prevPage.setItemMeta(prevMeta);

ItemStack removeBalloon = new ItemStack(Material.valueOf(messageTranslations.getString("buttons.unequip.material")));
ItemMeta removeMeta = removeBalloon.getItemMeta();
assert removeMeta != null;
removeMeta.setDisplayName(ColorManagement.fromHex(messageTranslations.getString("buttons.unequip.name")));;
removeMeta.displayName(messageTranslations.getSerializedString(messageTranslations.getString("buttons.unequip.name")));;
removeMeta.setCustomModelData(messageTranslations.getInt("buttons.unequip.custom-model-data"));
removeBalloon.setItemMeta(removeMeta);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.jeqo.bloons.utils.BalloonManagement;
import net.jeqo.bloons.utils.ColorManagement;
import net.jeqo.bloons.utils.MessageTranslations;
import net.kyori.adventure.text.Component;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -45,7 +46,8 @@ public void onClick(InventoryClickEvent event){
SingleBalloon.checkBalloonRemovalOrAdd(player, balloon);
player.playSound(player.getLocation(), Sound.ENTITY_CHICKEN_EGG, 1, 1);
String balloonName = event.getCurrentItem().getItemMeta().getDisplayName();
player.sendMessage(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("equipped", balloonName));
String translatedName = messageTranslations.convertBukkitColorsToAdventure(balloonName);
player.sendMessage(messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("equipped", translatedName)));
if (messageTranslations.getString("close-on-equip").equals("true")) {
player.closeInventory();
}
Expand Down Expand Up @@ -89,13 +91,13 @@ public void onClick(InventoryClickEvent event){
SingleBalloon balloonOwner1 = Bloons.playerBalloons.get(player.getUniqueId());
if (balloonOwner1 == null) {
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO, 1, 1);
player.sendMessage(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("not-equipped"));
player.sendMessage(messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("not-equipped")));
} else {
if (messageTranslations.getString("close-on-unequip").equals("true")) {
player.closeInventory();
}
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_HURT_SWEET_BERRY_BUSH, 1, 1);
player.sendMessage(messageTranslations.getMessage("prefix") + messageTranslations.getMessage("unequipped"));
player.sendMessage(messageTranslations.getSerializedString(messageTranslations.getMessage("prefix"), messageTranslations.getMessage("unequipped")));
}
BalloonManagement.removeBalloon(player, balloonOwner1);
}
Expand Down
38 changes: 36 additions & 2 deletions src/main/java/net/jeqo/bloons/utils/MessageTranslations.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
package net.jeqo.bloons.utils;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.plugin.java.JavaPlugin;

public record MessageTranslations(JavaPlugin instance) {

public String getMessage(String id, String arg) {
return ColorManagement.fromHex(String.format(this.instance.getConfig().getString("messages." + id, ""), arg));
return String.format(this.instance.getConfig().getString("messages." + id, ""), arg);
}

public String getMessage(String id) {
return ColorManagement.fromHex(this.instance.getConfig().getString("messages." + id, ""));
return this.instance.getConfig().getString("messages." + id, "");
}

public Component getSerializedString(String messagePrefix, String messageSuffix) {
MiniMessage mm = MiniMessage.miniMessage();
return mm.deserialize(messagePrefix + messageSuffix);
}

public Component getSerializedString(String message) {
MiniMessage mm = MiniMessage.miniMessage();
return mm.deserialize(message);
}

public String convertBukkitColorsToAdventure(String message) {
message = message.replace("§0", "<black>");
message = message.replace("§1", "<dark_blue>");
message = message.replace("§2", "<dark_green>");
message = message.replace("§3", "<dark_aqua>");
message = message.replace("§4", "<dark_red>");
message = message.replace("§5", "<dark_purple>");
message = message.replace("§6", "<gold>");
message = message.replace("§7", "<gray>");
message = message.replace("§8", "<dark_gray>");
message = message.replace("§9", "<blue>");
message = message.replace("§a", "<green>");
message = message.replace("§b", "<aqua>");
message = message.replace("§c", "<red>");
message = message.replace("§d", "<light_purple>");
message = message.replace("§e", "<yellow>");
message = message.replace("§f", "<white>");

return message;
}


public String getString(String path) {
return this.instance.getConfig().getString(path);
}
Expand Down

0 comments on commit 0b31ecb

Please sign in to comment.