重写Ammo名称相关调用

This commit is contained in:
Light_Quanta 2025-04-15 22:47:54 +08:00
parent d3094fa6c6
commit 9ff7abfd34
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
7 changed files with 69 additions and 58 deletions

View file

@ -106,7 +106,7 @@ public class AmmoCountOverlay implements LayeredDraw.Layer {
var ammoBoxType = data == null ? "All" : data.type(); var ammoBoxType = data == null ? "All" : data.type();
boxAmmoCount = type.get(stack); boxAmmoCount = type.get(stack);
if (ammoBoxType.equals("All") || ammoBoxType.equals(type.name)) { if (ammoBoxType.equals("All") || ammoBoxType.equals(type.serializationName)) {
boxAnimator.forward(currentTime); boxAnimator.forward(currentTime);
boxAmmoSelected = true; boxAmmoSelected = true;
} else { } else {
@ -158,7 +158,7 @@ public class AmmoCountOverlay implements LayeredDraw.Layer {
// 弹药类型 // 弹药类型
guiGraphics.drawString( guiGraphics.drawString(
font, font,
Component.translatable(type.translatableKey).getString(), Component.translatable(type.translationKey).getString(),
ammoX + 35, ammoX + 35,
h + yOffset, h + yOffset,
fontColor, fontColor,

View file

@ -31,7 +31,7 @@ public class AmmoCommand {
var type = context.getArgument("type", Ammo.class); var type = context.getArgument("type", Ammo.class);
var value = type.get(player); var value = type.get(player);
context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.get", Component.translatable(type.translatableKey), value), true); context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.get", Component.translatable(type.translationKey), value), true);
return 0; return 0;
})))) }))))
.then(Commands.literal("set").requires(s -> s.hasPermission(2)).then(Commands.argument("players", EntityArgument.players()).then(Commands.argument("type", EnumArgument.enumArgument(Ammo.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> { .then(Commands.literal("set").requires(s -> s.hasPermission(2)).then(Commands.argument("players", EntityArgument.players()).then(Commands.argument("type", EnumArgument.enumArgument(Ammo.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> {
@ -43,7 +43,7 @@ public class AmmoCommand {
type.set(player, value); type.set(player, value);
} }
context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.set", Component.translatable(type.translatableKey), value, players.size()), true); context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.set", Component.translatable(type.translationKey), value, players.size()), true);
return 0; return 0;
}))))) })))))
.then(Commands.literal("add").requires(s -> s.hasPermission(2)).then(Commands.argument("players", EntityArgument.players()).then(Commands.argument("type", EnumArgument.enumArgument(Ammo.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> { .then(Commands.literal("add").requires(s -> s.hasPermission(2)).then(Commands.argument("players", EntityArgument.players()).then(Commands.argument("type", EnumArgument.enumArgument(Ammo.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> {
@ -55,7 +55,7 @@ public class AmmoCommand {
type.add(player, value); type.add(player, value);
} }
context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.add", Component.translatable(type.translatableKey), value, players.size()), true); context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.add", Component.translatable(type.translationKey), value, players.size()), true);
return 0; return 0;
}))))); })))));
} }

View file

@ -55,7 +55,7 @@ public class ModDataComponents {
public static void register(IEventBus eventBus) { public static void register(IEventBus eventBus) {
for (var type : Ammo.values()) { for (var type : Ammo.values()) {
type.dataComponent = register("ammo_" + type.name.toLowerCase(), builder -> builder.persistent(Codec.INT)); type.dataComponent = register("ammo_" + type.name, builder -> builder.persistent(Codec.INT));
} }
DATA_COMPONENT_TYPES.register(eventBus); DATA_COMPONENT_TYPES.register(eventBus);

View file

@ -57,7 +57,7 @@ public class AmmoSupplierItem extends Item {
} }
if (!level.isClientSide()) { if (!level.isClientSide()) {
player.displayClientMessage(Component.translatable("item.superbwarfare.ammo_supplier.supply", Component.translatable(this.type.translatableKey), ammoToAdd * count), true); player.displayClientMessage(Component.translatable("item.superbwarfare.ammo_supplier.supply", Component.translatable(this.type.translationKey), ammoToAdd * count), true);
level.playSound(null, player.blockPosition(), ModSounds.BULLET_SUPPLY.get(), SoundSource.PLAYERS, 1, 1); level.playSound(null, player.blockPosition(), ModSounds.BULLET_SUPPLY.get(), SoundSource.PLAYERS, 1, 1);
} }
return InteractionResultHolder.success(stack); return InteractionResultHolder.success(stack);

View file

@ -77,7 +77,7 @@ public class AmmoBox extends Item {
list.add("All"); list.add("All");
for (var ammoType : Ammo.values()) { for (var ammoType : Ammo.values()) {
list.add(ammoType.name); list.add(ammoType.serializationName);
} }
return list; return list;
@ -102,18 +102,10 @@ public class AmmoBox extends Item {
return true; return true;
} }
switch (type) { player.displayClientMessage(
case RIFLE -> Component.translatable("des.superbwarfare.ammo_box.type." + type.name).withStyle(type.color),
player.displayClientMessage(Component.translatable("des.superbwarfare.ammo_box.type.rifle").withStyle(ChatFormatting.GREEN), true); true
case HANDGUN -> );
player.displayClientMessage(Component.translatable("des.superbwarfare.ammo_box.type.handgun").withStyle(ChatFormatting.AQUA), true);
case SHOTGUN ->
player.displayClientMessage(Component.translatable("des.superbwarfare.ammo_box.type.shotgun").withStyle(ChatFormatting.RED), true);
case SNIPER ->
player.displayClientMessage(Component.translatable("des.superbwarfare.ammo_box.type.sniper").withStyle(ChatFormatting.GOLD), true);
case HEAVY ->
player.displayClientMessage(Component.translatable("des.superbwarfare.ammo_box.type.heavy").withStyle(ChatFormatting.LIGHT_PURPLE), true);
}
} }
return true; return true;
@ -127,24 +119,10 @@ public class AmmoBox extends Item {
tooltipComponents.add(Component.translatable("des.superbwarfare.ammo_box").withStyle(ChatFormatting.GRAY)); tooltipComponents.add(Component.translatable("des.superbwarfare.ammo_box").withStyle(ChatFormatting.GRAY));
tooltipComponents.add(Component.translatable("des.superbwarfare.ammo_box.handgun").withStyle(ChatFormatting.AQUA) for (var ammo : Ammo.values()) {
.append(Component.literal("").withStyle(ChatFormatting.RESET)) tooltipComponents.add(Component.translatable("des.superbwarfare.ammo_box." + ammo.name).withStyle(ammo.color)
.append(Component.literal(FormatTool.format0D(Ammo.HANDGUN.get(stack)) + ((type != Ammo.HANDGUN) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); .append(Component.literal("").withStyle(ChatFormatting.RESET))
.append(Component.literal(FormatTool.format0D(ammo.get(stack)) + ((type != ammo) ? " " : " ←-")).withStyle(ChatFormatting.BOLD)));
tooltipComponents.add(Component.translatable("des.superbwarfare.ammo_box.rifle").withStyle(ChatFormatting.GREEN) }
.append(Component.literal("").withStyle(ChatFormatting.RESET))
.append(Component.literal(FormatTool.format0D(Ammo.RIFLE.get(stack)) + ((type != Ammo.RIFLE) ? " " : " ←-")).withStyle(ChatFormatting.BOLD)));
tooltipComponents.add(Component.translatable("des.superbwarfare.ammo_box.shotgun").withStyle(ChatFormatting.RED)
.append(Component.literal("").withStyle(ChatFormatting.RESET))
.append(Component.literal(FormatTool.format0D(Ammo.SHOTGUN.get(stack)) + ((type != Ammo.SHOTGUN) ? " " : " ←-")).withStyle(ChatFormatting.BOLD)));
tooltipComponents.add(Component.translatable("des.superbwarfare.ammo_box.sniper").withStyle(ChatFormatting.GOLD)
.append(Component.literal("").withStyle(ChatFormatting.RESET))
.append(Component.literal(FormatTool.format0D(Ammo.SNIPER.get(stack)) + ((type != Ammo.SNIPER) ? " " : " ←-")).withStyle(ChatFormatting.BOLD)));
tooltipComponents.add(Component.translatable("des.superbwarfare.ammo_box.heavy").withStyle(ChatFormatting.LIGHT_PURPLE)
.append(Component.literal("").withStyle(ChatFormatting.RESET))
.append(Component.literal(FormatTool.format0D(Ammo.HEAVY.get(stack)) + ((type != Ammo.HEAVY) ? " " : " ←-")).withStyle(ChatFormatting.BOLD)));
} }
} }

View file

@ -464,13 +464,7 @@ public abstract class GunItem extends Item implements CustomRendererItem {
var type = Ammo.getType(ammoTypeInfo.value()); var type = Ammo.getType(ammoTypeInfo.value());
assert type != null; assert type != null;
return switch (type) { return type.displayName;
case RIFLE -> "Rifle Ammo";
case HANDGUN -> "Handgun Ammo";
case SHOTGUN -> "Shotgun Ammo";
case SNIPER -> "Sniper Ammo";
case HEAVY -> "Heavy Ammo";
};
} }
return ""; return "";
} }

View file

@ -2,6 +2,7 @@ package com.atsuishio.superbwarfare.tools;
import com.atsuishio.superbwarfare.capability.player.PlayerVariable; import com.atsuishio.superbwarfare.capability.player.PlayerVariable;
import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModAttachments;
import net.minecraft.ChatFormatting;
import net.minecraft.core.component.DataComponentType; import net.minecraft.core.component.DataComponentType;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
@ -9,23 +10,61 @@ import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.registries.DeferredHolder; import net.neoforged.neoforge.registries.DeferredHolder;
public enum Ammo { public enum Ammo {
HANDGUN("item.superbwarfare.ammo.handgun", "HandgunAmmo"), HANDGUN(ChatFormatting.GREEN),
RIFLE("item.superbwarfare.ammo.rifle", "RifleAmmo"), RIFLE(ChatFormatting.AQUA),
SHOTGUN("item.superbwarfare.ammo.shotgun", "ShotgunAmmo"), SHOTGUN(ChatFormatting.RED),
SNIPER("item.superbwarfare.ammo.sniper", "SniperAmmo"), SNIPER(ChatFormatting.GOLD),
HEAVY("item.superbwarfare.ammo.heavy", "HeavyAmmo"); HEAVY(ChatFormatting.LIGHT_PURPLE);
public final String translatableKey;
/**
* 翻译字段名称 item.superbwarfare.ammo.rifle
*/
public final String translationKey;
/**
* 大驼峰格式命名的序列化字段名称 RifleAmmo
*/
public final String serializationName;
/**
* 下划线格式命名的小写名称 rifle
*/
public final String name; public final String name;
/**
* 大驼峰格式命名的显示名称 Rifle Ammo
*/
public final String displayName;
public final ChatFormatting color;
public DeferredHolder<DataComponentType<?>, DataComponentType<Integer>> dataComponent; public DeferredHolder<DataComponentType<?>, DataComponentType<Integer>> dataComponent;
Ammo(String translatableKey, String name) { Ammo(ChatFormatting color) {
this.translatableKey = translatableKey; this.color = color;
var name = name().toLowerCase();
this.name = name; this.name = name;
this.translationKey = "item.superbwarfare.ammo." + name;
var builder = new StringBuilder();
var useUpperCase = true;
for (char c : name.toCharArray()) {
if (c == '_') {
useUpperCase = true;
} else if (useUpperCase) {
builder.append(Character.toUpperCase(c));
useUpperCase = false;
} else {
builder.append(c);
}
}
this.displayName = builder + " Ammo";
this.serializationName = builder + "Ammo";
} }
public static Ammo getType(String name) { public static Ammo getType(String name) {
for (Ammo type : values()) { for (Ammo type : values()) {
if (type.name.equals(name)) { if (type.serializationName.equals(name)) {
return type; return type;
} }
} }
@ -48,12 +87,12 @@ public enum Ammo {
// NBTTag // NBTTag
public int get(CompoundTag tag) { public int get(CompoundTag tag) {
return tag.getInt(this.name); return tag.getInt(this.serializationName);
} }
public void set(CompoundTag tag, int count) { public void set(CompoundTag tag, int count) {
if (count < 0) count = 0; if (count < 0) count = 0;
tag.putInt(this.name, count); tag.putInt(this.serializationName, count);
} }
public void add(CompoundTag tag, int count) { public void add(CompoundTag tag, int count) {
@ -108,6 +147,6 @@ public enum Ammo {
@Override @Override
public String toString() { public String toString() {
return this.name; return this.serializationName;
} }
} }