From a29d7f20d077b01db68c30babea5d85e71343204 Mon Sep 17 00:00:00 2001 From: HeroBrineGoat <76707404+MasterOfTheFish@users.noreply.github.com> Date: Tue, 25 Jan 2022 23:11:56 -0500 Subject: [PATCH] Added custom dye color command and message --- .../command/CosmeticsCommand.java | 25 +++++++++++++++++-- .../hmccosmetics/message/Messages.java | 2 ++ .../hmccosmetics/util/StringUtils.java | 14 +++++++++++ src/main/resources/messages.yml | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/command/CosmeticsCommand.java b/src/main/java/io/github/fisher2911/hmccosmetics/command/CosmeticsCommand.java index ce10a0f9..f0ad1aa3 100644 --- a/src/main/java/io/github/fisher2911/hmccosmetics/command/CosmeticsCommand.java +++ b/src/main/java/io/github/fisher2911/hmccosmetics/command/CosmeticsCommand.java @@ -9,6 +9,7 @@ import io.github.fisher2911.hmccosmetics.message.Messages; import io.github.fisher2911.hmccosmetics.message.Placeholder; import io.github.fisher2911.hmccosmetics.user.User; import io.github.fisher2911.hmccosmetics.user.UserManager; +import io.github.fisher2911.hmccosmetics.util.StringUtils; import me.mattstudios.mf.annotations.Command; import me.mattstudios.mf.annotations.Completion; import me.mattstudios.mf.annotations.Default; @@ -16,6 +17,7 @@ import me.mattstudios.mf.annotations.Permission; import me.mattstudios.mf.annotations.SubCommand; import me.mattstudios.mf.base.CommandBase; import org.bukkit.Bukkit; +import org.bukkit.Color; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -61,7 +63,7 @@ public class CosmeticsCommand extends CommandBase { @SubCommand("dye") @Permission(io.github.fisher2911.hmccosmetics.message.Permission.DYE_COMMAND) - public void dyeArmor(final Player player, @Completion("#types") String typeString) { + public void dyeArmor(final Player player, @Completion("#types") String typeString, final @me.mattstudios.mf.annotations.Optional String dyeColor) { final Optional optionalUser = this.userManager.get(player.getUniqueId()); @@ -74,7 +76,26 @@ public class CosmeticsCommand extends CommandBase { final User user = optionalUser.get(); - this.cosmeticsMenu.openDyeSelectorGui(user, type); + if (dyeColor == null) { + this.cosmeticsMenu.openDyeSelectorGui(user, type); + return; + } + + final java.awt.Color awtColor = java.awt.Color.decode(dyeColor); + Color color = Color.fromRGB(awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue()); + + final ArmorItem armorItem = user.getPlayerArmor().getItem(type); + + armorItem.setDye(color.asRGB()); + + this.userManager.setItem(user, armorItem); + + this.messageHandler.sendMessage( + player, + Messages.SET_DYE_COLOR, + Map.of(Placeholder.ITEM, StringUtils.formatArmorItemType(typeString)) + ); + } catch (final IllegalArgumentException exception) { this.messageHandler.sendMessage( player, diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/message/Messages.java b/src/main/java/io/github/fisher2911/hmccosmetics/message/Messages.java index ec0bf57b..2ecd335a 100644 --- a/src/main/java/io/github/fisher2911/hmccosmetics/message/Messages.java +++ b/src/main/java/io/github/fisher2911/hmccosmetics/message/Messages.java @@ -21,6 +21,8 @@ public class Messages { new Message("set-off-hand", "Set off hand"); public static final Message REMOVED_OFF_HAND = new Message("removed-off-hand", "Removed off hand"); + public static final Message SET_DYE_COLOR = + new Message("set-dye-color", "Set dye color of " + Placeholder.ITEM); public static final Message MUST_BE_PLAYER = new Message("must-be-player", "You must be a player to do this!"); public static final Message RELOADED = diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/util/StringUtils.java b/src/main/java/io/github/fisher2911/hmccosmetics/util/StringUtils.java index 41df7068..88c13ef8 100644 --- a/src/main/java/io/github/fisher2911/hmccosmetics/util/StringUtils.java +++ b/src/main/java/io/github/fisher2911/hmccosmetics/util/StringUtils.java @@ -8,6 +8,7 @@ import net.kyori.adventure.text.Component; import org.bukkit.entity.Player; import org.jetbrains.annotations.Nullable; +import java.util.Locale; import java.util.Map; public class StringUtils { @@ -51,4 +52,17 @@ public class StringUtils { public static String parseStringToString(final String parsed) { return Adventure.SERIALIZER.serialize(Adventure.MINI_MESSAGE.parse(parsed)); } + + public static String formatArmorItemType(String type) { + type = type.toLowerCase(); + final String[] parts = type.split(" "); + + final String firstPart = parts[0].substring(0, 1).toUpperCase() + parts[0].substring(1); + + if (parts.length == 1) { + return firstPart; + } + + return firstPart + parts[1].substring(0, 1).toUpperCase() + parts[1].substring(1); + } } diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 65a2e945..5d167fba 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -6,6 +6,7 @@ set-backpack: "Applied backpack!" removed-backpack: "Removed backpack!" set-off-hand: "Applied offhand!" removed-off-hand: "Removed offhand!" +set-dye-color: "Set color of %item%!" must-be-player: "You must be a player to do this!" reloaded: "Config files reloaded!" invalid-type: "Invalid cosmetic type, please use hat, backpack or off_hand!"