实现getPerkId
This commit is contained in:
parent
4c97f71e42
commit
413ce04c99
1 changed files with 26 additions and 21 deletions
|
@ -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);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue