diff options
author | Aaron <51387595+AzureAaron@users.noreply.github.com> | 2023-08-09 20:01:18 -0400 |
---|---|---|
committer | Aaron <51387595+AzureAaron@users.noreply.github.com> | 2023-08-09 20:01:18 -0400 |
commit | a629801cc3ffa5913bc07af173e8a01673a70681 (patch) | |
tree | 37fa4fd6c9983277fb728cb27f2e256015fb81c1 /src/main/java/me/xmrvizzy/skyblocker/skyblock/item | |
parent | 79ba1ed6fc133bacf64ee8e3a5d1454d1eadde2f (diff) | |
download | Skyblocker-a629801cc3ffa5913bc07af173e8a01673a70681.tar.gz Skyblocker-a629801cc3ffa5913bc07af173e8a01673a70681.tar.bz2 Skyblocker-a629801cc3ffa5913bc07af173e8a01673a70681.zip |
Custom Armour Dye Colours
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/skyblock/item')
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorDyeColors.java | 87 | ||||
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomItemNames.java (renamed from src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRenaming.java) | 21 |
2 files changed, 99 insertions, 9 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorDyeColors.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorDyeColors.java new file mode 100644 index 00000000..dd5614d1 --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorDyeColors.java @@ -0,0 +1,87 @@ +package me.xmrvizzy.skyblocker.skyblock.item; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; + +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import me.xmrvizzy.skyblocker.config.SkyblockerConfig; +import me.xmrvizzy.skyblocker.utils.Utils; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.item.DyeableArmorItem; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.text.Text; + +public class CustomArmorDyeColors { + + public static void init() { + ClientCommandRegistrationCallback.EVENT.register(CustomArmorDyeColors::registerCommands); + } + + private static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { + dispatcher.register(ClientCommandManager.literal("skyblocker") + .then(ClientCommandManager.literal("dyeColor") + .executes(context -> customizeDyeColor(context.getSource(), null)) + .then(ClientCommandManager.argument("hexCode", StringArgumentType.string()) + .executes(context -> customizeDyeColor(context.getSource(), StringArgumentType.getString(context, "hexCode")))))); + } + + private static int customizeDyeColor(FabricClientCommandSource source, String hex) { + MinecraftClient client = source.getClient(); + ItemStack heldItem = client.player.getMainHandStack(); + NbtCompound nbt = (heldItem != null) ? heldItem.getNbt() : null; + + if (hex != null && !isHexadecimalColor(hex)) { + source.sendError(Text.translatable("skyblocker.customDyeColors.invalidHex")); + return Command.SINGLE_SUCCESS; + } + + if (Utils.isOnSkyblock() && heldItem != null && heldItem.getItem() instanceof DyeableArmorItem) { + if (nbt != null && nbt.contains("ExtraAttributes")) { + NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); + String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; + + if (itemUuid != null) { + Object2IntOpenHashMap<String> customDyeColors = SkyblockerConfig.get().general.customDyeColors; + + if (hex == null) { + if (customDyeColors.containsKey(itemUuid)) { + customDyeColors.removeInt(itemUuid); + SkyblockerConfig.save(); + source.sendFeedback(Text.translatable("skyblocker.customDyeColors.removed")); + } else { + source.sendFeedback(Text.translatable("skyblocker.customDyeColors.neverHad")); + } + } else { + customDyeColors.put(itemUuid, Integer.decode("0x" + hex.replace("#", "")).intValue()); + SkyblockerConfig.save(); + source.sendFeedback(Text.translatable("skyblocker.customDyeColors.added")); + } + } else { + source.sendError(Text.translatable("skyblocker.customDyeColors.noItemUuid")); + } + } + } else { + if (!(heldItem.getItem() instanceof DyeableArmorItem)) { + source.sendError(Text.translatable("skyblocker.customDyeColors.notDyeable")); + return Command.SINGLE_SUCCESS; + } else { + source.sendError(Text.translatable("skyblocker.customDyeColors.unableToSetColor")); + } + } + + return Command.SINGLE_SUCCESS; + } + + private static boolean isHexadecimalColor(String s) { + s = s.replace("#", ""); + + return s.chars() + .allMatch(c -> "0123456789ABCDEFabcdef".indexOf(c) >= 0) && s.length() == 6; + } +} diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRenaming.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomItemNames.java index db672803..4ecff508 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRenaming.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomItemNames.java @@ -1,10 +1,9 @@ package me.xmrvizzy.skyblocker.skyblock.item; -import java.util.Map; - import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import me.xmrvizzy.skyblocker.config.SkyblockerConfig; import me.xmrvizzy.skyblocker.utils.Utils; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; @@ -17,10 +16,10 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; -public class ItemRenaming { +public class CustomItemNames { public static void init() { - ClientCommandRegistrationCallback.EVENT.register(ItemRenaming::registerCommands); + ClientCommandRegistrationCallback.EVENT.register(CustomItemNames::registerCommands); } private static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { @@ -41,13 +40,17 @@ public class ItemRenaming { String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; if (itemUuid != null) { - Map<String, Text> customItemNames = SkyblockerConfig.get().general.customItemNames; + Object2ObjectOpenHashMap<String, Text> customItemNames = SkyblockerConfig.get().general.customItemNames; if (text == null) { - //Remove custom item name when the text argument isn't passed - customItemNames.remove(itemUuid); - SkyblockerConfig.save(); - source.sendFeedback(Text.translatable("skyblocker.customItemNames.removed")); + if (customItemNames.containsKey(itemUuid)) { + //Remove custom item name when the text argument isn't passed + customItemNames.remove(itemUuid); + SkyblockerConfig.save(); + source.sendFeedback(Text.translatable("skyblocker.customItemNames.removed")); + } else { + source.sendFeedback(Text.translatable("skyblocker.customItemNames.neverHad")); + } } else { //If the text is provided then set the item's custom name to it customItemNames.put(itemUuid, text); |