实现getPerkId

This commit is contained in:
Light_Quanta 2024-08-08 02:42:27 +08:00
parent 4c97f71e42
commit 413ce04c99
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959

View file

@ -1,16 +1,19 @@
package net.mcreator.superbwarfare.perk; package net.mcreator.superbwarfare.perk;
import net.mcreator.superbwarfare.init.ModPerks;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.registries.RegistryObject;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class PerkHelper { public class PerkHelper {
private static final String TAG_PERK_ID = "id"; private static final String TAG_PERK_ID = "id";
private static final String TAG_PERK_LEVEL = "level"; private static final String TAG_PERK_LEVEL = "level";
private static final String TAG_PERK = "Perks";
public static CompoundTag makePerk(@Nullable ResourceLocation pId, int pLevel) { public static CompoundTag makePerk(@Nullable ResourceLocation pId, int pLevel) {
CompoundTag compoundtag = new CompoundTag(); CompoundTag compoundtag = new CompoundTag();
@ -30,37 +33,39 @@ public class PerkHelper {
@Nullable @Nullable
public static ResourceLocation getPerkId(CompoundTag pCompoundTag) { public static ResourceLocation getPerkId(CompoundTag pCompoundTag) {
return ResourceLocation.tryParse(pCompoundTag.getString("id")); return ResourceLocation.tryParse(pCompoundTag.getString(TAG_PERK_ID));
} }
// TODO 实现通过注册表找到对应perk并返回对应的resourcelocation @Nullable
// @Nullable public static ResourceLocation getPerkId(Perk perk) {
// public static ResourceLocation getPerkId(Perk perk) { return ModPerks.PERKS.getEntries().stream()
// return .filter(p -> p.get().type == perk.type)
// } .findFirst()
.map(RegistryObject::getId)
.orElse(null);
}
public static int getItemPerkLevel(Perk perk, ItemStack stack) { public static int getItemPerkLevel(Perk perk, ItemStack stack) {
if (stack.isEmpty()) { 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; 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) { 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();
} }
} }