修改PerkType实现方式

This commit is contained in:
Light_Quanta 2024-08-08 03:13:38 +08:00
parent 413ce04c99
commit 83817b94c9
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
4 changed files with 23 additions and 30 deletions

View file

@ -32,15 +32,15 @@ public class PerkItem extends Item {
@Override @Override
public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltips, TooltipFlag isAdvanced) { public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltips, TooltipFlag isAdvanced) {
ChatFormatting chatFormatting = switch (this.getPerk().type.getSlot()) { ChatFormatting chatFormatting = switch (this.getPerk().type) {
case 0 -> ChatFormatting.YELLOW; case AMMO -> ChatFormatting.YELLOW;
case 1 -> ChatFormatting.GREEN; case FUNCTIONAL -> ChatFormatting.GREEN;
default -> ChatFormatting.RED; case DAMAGE -> ChatFormatting.RED;
}; };
tooltips.add(Component.translatable("perk.superbwarfare." + this.getPerk().descriptionId + ".desc").withStyle(ChatFormatting.GRAY)); tooltips.add(Component.translatable("perk.superbwarfare." + this.getPerk().descriptionId + ".desc").withStyle(ChatFormatting.GRAY));
tooltips.add(Component.literal("")); tooltips.add(Component.literal(""));
tooltips.add(Component.translatable("perk.superbwarfare.slot").withStyle(ChatFormatting.GOLD) tooltips.add(Component.translatable("perk.superbwarfare.slot").withStyle(ChatFormatting.GOLD)
.append(Component.translatable("perk.superbwarfare.slot_" + this.getPerk().type.getSlot()).withStyle(chatFormatting))); .append(Component.translatable("perk.superbwarfare.slot_" + this.getPerk().type.getName()).withStyle(chatFormatting)));
} }
} }

View file

@ -178,7 +178,7 @@ public abstract class GunItem extends Item {
} }
} }
public boolean canApplyPerk(ItemStack stack, Perk perk, int slot) { public boolean canApplyPerk(ItemStack stack, Perk perk, Perk.Type slot) {
return perk.type.getSlot() == slot; return perk.type == slot;
} }
} }

View file

@ -10,17 +10,17 @@ public class Perk {
} }
public enum Type { public enum Type {
AMMO(0), AMMO("Ammo"),
FUNCTIONAL(1), FUNCTIONAL("Func"),
DAMAGE(2); DAMAGE("Damage");
private final int slot; private final String type;
Type(int slot) { Type(String type) {
this.slot = slot; this.type = type;
} }
public int getSlot() { public String getName() {
return slot; return type;
} }
} }
} }

View file

@ -2,7 +2,6 @@ package net.mcreator.superbwarfare.perk;
import net.mcreator.superbwarfare.init.ModPerks; import net.mcreator.superbwarfare.init.ModPerks;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
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;
@ -50,22 +49,16 @@ public class PerkHelper {
return 0; return 0;
} }
ResourceLocation perkId = getPerkId(perk); return getPerkLevel(getPerkTag(stack, perk.type));
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 CompoundTag getPerkTag(ItemStack stack, Perk.Type type) {
return stack.getTag() != null ? stack.getTag().getList(TAG_PERK, 10) : new ListTag(); var tag = stack.getTag();
if (tag == null) return new CompoundTag();
var tagPerk = tag.getCompound(TAG_PERK);
if (!tagPerk.contains(type.getName())) return new CompoundTag();
return tag.getCompound(type.getName());
} }
} }