aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java
diff options
context:
space:
mode:
authorKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-10-09 21:10:55 -0400
committerKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-10-11 16:16:33 -0400
commit80bdbf4299966243ae3141b10178a2cd6f061f93 (patch)
tree1b7b54e6455de84119de3b29422504c8baabb9e4 /src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java
parent0eb2976ff87fe7b933ce51cdc0464ca968c234d5 (diff)
downloadSkyblocker-80bdbf4299966243ae3141b10178a2cd6f061f93.tar.gz
Skyblocker-80bdbf4299966243ae3141b10178a2cd6f061f93.tar.bz2
Skyblocker-80bdbf4299966243ae3141b10178a2cd6f061f93.zip
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.java49
1 files changed, 19 insertions, 30 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..b9d00ea0 100644
--- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java
+++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java
@@ -1,66 +1,55 @@
-package de.hysky.skyblocker.skyblock.item;
+package me.xmrvizzy.skyblocker.skyblock.item;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
-import de.hysky.skyblocker.config.SkyblockerConfigManager;
-import de.hysky.skyblocker.utils.Utils;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
+import me.xmrvizzy.skyblocker.config.SkyblockerConfigManager;
+import me.xmrvizzy.skyblocker.utils.ItemUtils;
+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.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 +58,7 @@ public class ItemProtection {
} else {
source.sendFeedback(Text.translatable("skyblocker.itemProtection.unableToProtect"));
}
-
+
return Command.SINGLE_SUCCESS;
}
}