优化format

This commit is contained in:
17146 2025-02-03 23:03:23 +08:00
parent 20ccf8de29
commit bab7200b1d
5 changed files with 59 additions and 19 deletions

View file

@ -6,6 +6,7 @@ import com.atsuishio.superbwarfare.entity.vehicle.*;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.network.ModVariables; import com.atsuishio.superbwarfare.network.ModVariables;
import com.atsuishio.superbwarfare.tools.FormatTool;
import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
@ -30,8 +31,6 @@ import org.joml.Math;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.joml.Vector4f; import org.joml.Vector4f;
import java.text.DecimalFormat;
import static com.atsuishio.superbwarfare.client.RenderHelper.preciseBlit; import static com.atsuishio.superbwarfare.client.RenderHelper.preciseBlit;
import static com.atsuishio.superbwarfare.client.overlay.CrossHairOverlay.*; import static com.atsuishio.superbwarfare.client.overlay.CrossHairOverlay.*;
@ -107,12 +106,12 @@ public class HelicopterHudOverlay {
preciseBlit(guiGraphics, ModUtils.loc("textures/screens/helicopter/heli_power.png"), (float) w / 2 + 130f, ((float) h / 2 - 64 + 124 - power * 980), 0, 0, 4, power * 980, 4, power * 980); preciseBlit(guiGraphics, ModUtils.loc("textures/screens/helicopter/heli_power.png"), (float) w / 2 + 130f, ((float) h / 2 - 64 + 124 - power * 980), 0, 0, 4, power * 980, 4, power * 980);
lerpVy = (float) Mth.lerp(0.021f * event.getPartialTick(), lerpVy, mobileVehicle.getDeltaMovement().y()); lerpVy = (float) Mth.lerp(0.021f * event.getPartialTick(), lerpVy, mobileVehicle.getDeltaMovement().y());
preciseBlit(guiGraphics, ModUtils.loc("textures/screens/helicopter/heli_vy_move.png"), (float) w / 2 + 138, ((float) h / 2 - 3 - Math.max(lerpVy * 20, -24) * 2.5f), 0, 0, 8, 8, 8, 8); preciseBlit(guiGraphics, ModUtils.loc("textures/screens/helicopter/heli_vy_move.png"), (float) w / 2 + 138, ((float) h / 2 - 3 - Math.max(lerpVy * 20, -24) * 2.5f), 0, 0, 8, 8, 8, 8);
guiGraphics.drawString(Minecraft.getInstance().font, Component.literal(new DecimalFormat("##m/s").format(lerpVy * 20) ), guiGraphics.drawString(Minecraft.getInstance().font, Component.literal(FormatTool.format0D(lerpVy * 20, "m/s")),
w / 2 + 146, (int) (h / 2 - 3 - Math.max(lerpVy * 20, -24) * 2.5), (lerpVy * 20 < -24 || ((lerpVy * 20 < -10 || (lerpVy * 20 < -1 && length(mobileVehicle.getDeltaMovement().x, mobileVehicle.getDeltaMovement().y, mobileVehicle.getDeltaMovement().z) * 72 > 100)) && height < 36) || (length(mobileVehicle.getDeltaMovement().x, mobileVehicle.getDeltaMovement().y, mobileVehicle.getDeltaMovement().z) * 72 > 40 && blockInWay < 72) ? -65536 : 0x66FF00), false); w / 2 + 146, (int) (h / 2 - 3 - Math.max(lerpVy * 20, -24) * 2.5), (lerpVy * 20 < -24 || ((lerpVy * 20 < -10 || (lerpVy * 20 < -1 && length(mobileVehicle.getDeltaMovement().x, mobileVehicle.getDeltaMovement().y, mobileVehicle.getDeltaMovement().z) * 72 > 100)) && height < 36) || (length(mobileVehicle.getDeltaMovement().x, mobileVehicle.getDeltaMovement().y, mobileVehicle.getDeltaMovement().z) * 72 > 40 && blockInWay < 72) ? -65536 : 0x66FF00), false);
guiGraphics.drawString(Minecraft.getInstance().font, Component.literal(new DecimalFormat("##").format(mobileVehicle.getY())), guiGraphics.drawString(Minecraft.getInstance().font, Component.literal(FormatTool.format0D(mobileVehicle.getY())),
w / 2 + 104, h / 2, 0x66FF00, false); w / 2 + 104, h / 2, 0x66FF00, false);
preciseBlit(guiGraphics, ModUtils.loc("textures/screens/helicopter/speed_frame.png"), (float) w / 2 - 144, (float) h / 2 - 6, 0, 0, 50, 18, 50, 18); preciseBlit(guiGraphics, ModUtils.loc("textures/screens/helicopter/speed_frame.png"), (float) w / 2 - 144, (float) h / 2 - 6, 0, 0, 50, 18, 50, 18);
guiGraphics.drawString(Minecraft.getInstance().font, Component.literal(new DecimalFormat("##KM/H").format(length(mobileVehicle.getDeltaMovement().x, mobileVehicle.getDeltaMovement().y, mobileVehicle.getDeltaMovement().z) * 72)), guiGraphics.drawString(Minecraft.getInstance().font, Component.literal(FormatTool.format0D(length(mobileVehicle.getDeltaMovement().x, mobileVehicle.getDeltaMovement().y, mobileVehicle.getDeltaMovement().z) * 72, "km/h")),
w / 2 - 140, h / 2, 0x66FF00, false); w / 2 - 140, h / 2, 0x66FF00, false);
if (mobileVehicle instanceof Ah6Entity ah6Entity) { if (mobileVehicle instanceof Ah6Entity ah6Entity) {

View file

@ -1,6 +1,7 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.entity.MortarEntity; import com.atsuishio.superbwarfare.entity.MortarEntity;
import com.atsuishio.superbwarfare.tools.FormatTool;
import com.atsuishio.superbwarfare.tools.TraceTool; import com.atsuishio.superbwarfare.tools.TraceTool;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
@ -12,8 +13,6 @@ import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
import java.text.DecimalFormat;
@Mod.EventBusSubscriber(value = Dist.CLIENT) @Mod.EventBusSubscriber(value = Dist.CLIENT)
public class MortarInfoOverlay { public class MortarInfoOverlay {
@ -28,10 +27,10 @@ public class MortarInfoOverlay {
} }
if (lookingEntity instanceof MortarEntity mortar) { if (lookingEntity instanceof MortarEntity mortar) {
event.getGuiGraphics().drawString(Minecraft.getInstance().font, Component.translatable("tips.superbwarfare.mortar.yaw") event.getGuiGraphics().drawString(Minecraft.getInstance().font, Component.translatable("tips.superbwarfare.mortar.yaw")
.append(Component.literal(new DecimalFormat("##.#°").format(mortar.getEntityData().get(MortarEntity.Y_ROT)))), .append(Component.literal(FormatTool.format1D(mortar.getEntityData().get(MortarEntity.Y_ROT), "°"))),
w / 2 + 12, h / 2 - 36, -1, false); w / 2 + 12, h / 2 - 36, -1, false);
event.getGuiGraphics().drawString(Minecraft.getInstance().font, Component.translatable("tips.superbwarfare.mortar.pitch") event.getGuiGraphics().drawString(Minecraft.getInstance().font, Component.translatable("tips.superbwarfare.mortar.pitch")
.append(Component.literal(new DecimalFormat("##.#°").format(mortar.getEntityData().get(MortarEntity.PITCH)))), .append(Component.literal(FormatTool.format1D(mortar.getEntityData().get(MortarEntity.PITCH), "°"))),
w / 2 + 12, h / 2 - 28, -1, false); w / 2 + 12, h / 2 - 28, -1, false);
} }
} }

View file

@ -1,5 +1,6 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.tools.FormatTool;
import com.atsuishio.superbwarfare.tools.TraceTool; import com.atsuishio.superbwarfare.tools.TraceTool;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
@ -14,8 +15,6 @@ import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
import java.text.DecimalFormat;
@Mod.EventBusSubscriber(value = Dist.CLIENT) @Mod.EventBusSubscriber(value = Dist.CLIENT)
public class SpyglassRangeOverlay { public class SpyglassRangeOverlay {
@ -40,7 +39,7 @@ public class SpyglassRangeOverlay {
if (lookAtEntity) { if (lookAtEntity) {
event.getGuiGraphics().drawString(Minecraft.getInstance().font, Component.translatable("tips.superbwarfare.drone.range") event.getGuiGraphics().drawString(Minecraft.getInstance().font, Component.translatable("tips.superbwarfare.drone.range")
.append(Component.literal(new DecimalFormat("##.#M ").format(entityRange) + lookingEntity.getDisplayName().getString())), .append(Component.literal(FormatTool.format1D(entityRange, "M ") + lookingEntity.getDisplayName().getString())),
w / 2 + 12, h / 2 - 28, -1, false); w / 2 + 12, h / 2 - 28, -1, false);
} else { } else {
if (blockRange > 512) { if (blockRange > 512) {
@ -48,7 +47,7 @@ public class SpyglassRangeOverlay {
.append(Component.literal("---M")), w / 2 + 12, h / 2 - 28, -1, false); .append(Component.literal("---M")), w / 2 + 12, h / 2 - 28, -1, false);
} else { } else {
event.getGuiGraphics().drawString(Minecraft.getInstance().font, Component.translatable("tips.superbwarfare.drone.range") event.getGuiGraphics().drawString(Minecraft.getInstance().font, Component.translatable("tips.superbwarfare.drone.range")
.append(Component.literal(new DecimalFormat("##.#M").format(blockRange))), .append(Component.literal(FormatTool.format1D(blockRange, "M"))),
w / 2 + 12, h / 2 - 28, -1, false); w / 2 + 12, h / 2 - 28, -1, false);
} }
} }

View file

@ -3,6 +3,7 @@ package com.atsuishio.superbwarfare.network.message;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.tools.FormatTool;
import com.atsuishio.superbwarfare.tools.GunsTool; import com.atsuishio.superbwarfare.tools.GunsTool;
import com.atsuishio.superbwarfare.tools.SoundTool; import com.atsuishio.superbwarfare.tools.SoundTool;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
@ -12,7 +13,6 @@ import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.NetworkEvent;
import java.text.DecimalFormat;
import java.util.function.Supplier; import java.util.function.Supplier;
public class AdjustZoomFovMessage { public class AdjustZoomFovMessage {
@ -59,7 +59,7 @@ public class AdjustZoomFovMessage {
if (GunsTool.getGunIntTag(stack, "RPM", 0) == 1095) { if (GunsTool.getGunIntTag(stack, "RPM", 0) == 1095) {
GunsTool.setGunIntTag(stack, "RPM", 1100); GunsTool.setGunIntTag(stack, "RPM", 1100);
} }
player.displayClientMessage(Component.literal(new DecimalFormat("RPM: ##").format(GunsTool.getGunIntTag(stack, "RPM", 0))), true); player.displayClientMessage(Component.literal("RPM: " + FormatTool.format0D(GunsTool.getGunIntTag(stack, "RPM", 0))), true);
int rpm = GunsTool.getGunIntTag(stack, "RPM", 0); int rpm = GunsTool.getGunIntTag(stack, "RPM", 0);
if (rpm > minRpm && rpm < maxRpm) { if (rpm > minRpm && rpm < maxRpm) {
SoundTool.playLocalSound(player, ModSounds.ADJUST_FOV.get(), 1f, 0.7f); SoundTool.playLocalSound(player, ModSounds.ADJUST_FOV.get(), 1f, 0.7f);
@ -77,5 +77,4 @@ public class AdjustZoomFovMessage {
}); });
context.get().setPacketHandled(true); context.get().setPacketHandled(true);
} }
} }

View file

@ -0,0 +1,44 @@
package com.atsuishio.superbwarfare.tools;
import java.text.DecimalFormat;
public class FormatTool {
public static final DecimalFormat DECIMAL_FORMAT_0 = new DecimalFormat("##");
public static final DecimalFormat DECIMAL_FORMAT_1 = new DecimalFormat("##.#");
public static final DecimalFormat DECIMAL_FORMAT_2 = new DecimalFormat("##.##");
public static final DecimalFormat DECIMAL_FORMAT_1Z = new DecimalFormat("##.0");
public static String format0D(double num) {
return format0D(num, "");
}
public static String format0D(double num, String str) {
return DECIMAL_FORMAT_0.format(num) + str;
}
public static String format1D(double num) {
return format1D(num, "");
}
public static String format1D(double num, String str) {
return DECIMAL_FORMAT_1.format(num) + str;
}
public static String format2D(double num) {
return format2D(num, "");
}
public static String format2D(double num, String str) {
return DECIMAL_FORMAT_2.format(num) + str;
}
public static String format1DZ(double num) {
return format1DZ(num, "");
}
public static String format1DZ(double num, String str) {
return DECIMAL_FORMAT_1Z.format(num) + str;
}
}