From 413ce04c99bd5e53cf0b7e9272523b4f61294ee5 Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Thu, 8 Aug 2024 02:42:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0getPerkId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/perk/PerkHelper.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/mcreator/superbwarfare/perk/PerkHelper.java b/src/main/java/net/mcreator/superbwarfare/perk/PerkHelper.java index e053192a6..644a82837 100644 --- a/src/main/java/net/mcreator/superbwarfare/perk/PerkHelper.java +++ b/src/main/java/net/mcreator/superbwarfare/perk/PerkHelper.java @@ -1,16 +1,19 @@ package net.mcreator.superbwarfare.perk; +import net.mcreator.superbwarfare.init.ModPerks; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.Mth; import net.minecraft.world.item.ItemStack; +import net.minecraftforge.registries.RegistryObject; import javax.annotation.Nullable; public class PerkHelper { private static final String TAG_PERK_ID = "id"; private static final String TAG_PERK_LEVEL = "level"; + private static final String TAG_PERK = "Perks"; public static CompoundTag makePerk(@Nullable ResourceLocation pId, int pLevel) { CompoundTag compoundtag = new CompoundTag(); @@ -30,37 +33,39 @@ public class PerkHelper { @Nullable public static ResourceLocation getPerkId(CompoundTag pCompoundTag) { - return ResourceLocation.tryParse(pCompoundTag.getString("id")); + return ResourceLocation.tryParse(pCompoundTag.getString(TAG_PERK_ID)); } - // TODO 实现通过注册表找到对应perk并返回对应的resourcelocation -// @Nullable -// public static ResourceLocation getPerkId(Perk perk) { -// return -// } + @Nullable + public static ResourceLocation getPerkId(Perk perk) { + return ModPerks.PERKS.getEntries().stream() + .filter(p -> p.get().type == perk.type) + .findFirst() + .map(RegistryObject::getId) + .orElse(null); + } public static int getItemPerkLevel(Perk perk, ItemStack stack) { if (stack.isEmpty()) { - return 0; - } else { -// ResourceLocation resourcelocation = getEnchantmentId(pEnchantment); -// ListTag listtag = getPerkTags(stack); -// -// for(int i = 0; i < listtag.size(); ++i) { -// CompoundTag compoundtag = listtag.getCompound(i); -// ResourceLocation resourcelocation1 = getEnchantmentId(compoundtag); -// if (resourcelocation1 != null && resourcelocation1.equals(resourcelocation)) { -// return getEnchantmentLevel(compoundtag); -// } -// } - return 0; } + + ResourceLocation perkId = getPerkId(perk); + ListTag perkTags = getPerkTags(stack); + + for (int i = 0; i < perkTags.size(); ++i) { + CompoundTag compoundtag = perkTags.getCompound(i); + ResourceLocation tagPerkId = getPerkId(compoundtag); + if (tagPerkId != null && tagPerkId.equals(perkId)) { + return getPerkLevel(compoundtag); + } + } + + return 0; } public static ListTag getPerkTags(ItemStack stack) { - return stack.getTag() != null ? stack.getTag().getList("Perks", 10) : new ListTag(); + return stack.getTag() != null ? stack.getTag().getList(TAG_PERK, 10) : new ListTag(); } - }