diff options
author | Kevin <92656833+kevinthegreat1@users.noreply.github.com> | 2023-10-14 17:03:49 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-14 17:03:49 -0400 |
commit | a41b069db0216c571bce269a0ea9866c1d701b87 (patch) | |
tree | 390fa5d88f79b9f126c46ce5b803aa310f7be0f5 /src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java | |
parent | ab3f55fc2e81f5594009ecea708a931468ec7ccf (diff) | |
parent | 4fbcf1b2a4790f3033e97c55cab344abdfaea8d2 (diff) | |
download | Skyblocker-a41b069db0216c571bce269a0ea9866c1d701b87.tar.gz Skyblocker-a41b069db0216c571bce269a0ea9866c1d701b87.tar.bz2 Skyblocker-a41b069db0216c571bce269a0ea9866c1d701b87.zip |
Merge pull request #350 from kevinthegreat1/nbt-utils
Refactor NBT Parsing
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java | 44 |
1 files changed, 16 insertions, 28 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java index d73e1545..ff88ef8d 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java @@ -2,8 +2,8 @@ package de.hysky.skyblocker.skyblock.item; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; - import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; @@ -11,56 +11,44 @@ import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallba import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; public class ItemProtection { - + public static void init() { ClientCommandRegistrationCallback.EVENT.register(ItemProtection::registerCommand); } public static boolean isItemProtected(ItemStack stack) { - if (stack == null || stack.isEmpty()) return false; - - NbtCompound nbt = stack.getNbt(); - - if (nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : ""; - - return SkyblockerConfigManager.get().general.protectedItems.contains(itemUuid); - } - - return false; + if (stack == null) return false; + String itemUuid = ItemUtils.getItemUuid(stack); + return SkyblockerConfigManager.get().general.protectedItems.contains(itemUuid); } - + private static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { dispatcher.register(ClientCommandManager.literal("skyblocker") .then(ClientCommandManager.literal("protectItem") .executes(context -> protectMyItem(context.getSource())))); } - + private static int protectMyItem(FabricClientCommandSource source) { ItemStack heldItem = source.getPlayer().getMainHandStack(); - NbtCompound nbt = (heldItem != null) ? heldItem.getNbt() : null; - - if (Utils.isOnSkyblock() && nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; - - if (itemUuid != null) { + + if (Utils.isOnSkyblock()) { + String itemUuid = ItemUtils.getItemUuid(heldItem); + + if (!itemUuid.isEmpty()) { ObjectOpenHashSet<String> protectedItems = SkyblockerConfigManager.get().general.protectedItems; - + if (!protectedItems.contains(itemUuid)) { protectedItems.add(itemUuid); SkyblockerConfigManager.save(); - + source.sendFeedback(Text.translatable("skyblocker.itemProtection.added", heldItem.getName())); } else { protectedItems.remove(itemUuid); SkyblockerConfigManager.save(); - + source.sendFeedback(Text.translatable("skyblocker.itemProtection.removed", heldItem.getName())); } } else { @@ -69,7 +57,7 @@ public class ItemProtection { } else { source.sendFeedback(Text.translatable("skyblocker.itemProtection.unableToProtect")); } - + return Command.SINGLE_SUCCESS; } } |