添加盔甲插板的渲染

This commit is contained in:
17146 2024-10-01 17:17:05 +08:00
parent 2d214977ad
commit 1d6a07787f
4 changed files with 80 additions and 6 deletions

View file

@ -41,7 +41,7 @@ public class AmmoBarOverlay {
}
@SubscribeEvent(priority = EventPriority.NORMAL)
public static void eventHandler(RenderGuiEvent.Pre event) {
public static void onRenderGui(RenderGuiEvent.Pre event) {
if (!DisplayConfig.AMMO_HUD.get()) return;
int w = event.getWindow().getGuiScaledWidth();

View file

@ -0,0 +1,76 @@
package net.mcreator.superbwarfare.client.screens;
import net.mcreator.superbwarfare.ModUtils;
import net.mcreator.superbwarfare.init.ModTags;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderGuiEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class ArmorPlateOverlay {
private static final ResourceLocation ICON = ModUtils.loc("textures/screens/armor_plate_icon.png");
private static final ResourceLocation LEVEL1 = ModUtils.loc("textures/screens/armor_plate_level1.png");
private static final ResourceLocation LEVEL2 = ModUtils.loc("textures/screens/armor_plate_level2.png");
private static final ResourceLocation LEVEL3 = ModUtils.loc("textures/screens/armor_plate_level3.png");
private static final ResourceLocation LEVEL1_FRAME = ModUtils.loc("textures/screens/armor_plate_level1_frame.png");
private static final ResourceLocation LEVEL2_FRAME = ModUtils.loc("textures/screens/armor_plate_level2_frame.png");
private static final ResourceLocation LEVEL3_FRAME = ModUtils.loc("textures/screens/armor_plate_level3_frame.png");
@SubscribeEvent(priority = EventPriority.NORMAL)
public static void onRenderGui(RenderGuiEvent.Pre event) {
int h = event.getWindow().getGuiScaledHeight();
GuiGraphics guiGraphics = event.getGuiGraphics();
Player player = Minecraft.getInstance().player;
if (player == null) return;
if (player.isSpectator()) return;
ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST);
if (stack == ItemStack.EMPTY) return;
if (stack.getTag() == null || !stack.getTag().contains("ArmorPlate")) return;
double amount = stack.getTag().getDouble("ArmorPlate");
int armorLevel = 1;
if (stack.is(ModTags.Items.MILITARY_ARMOR)) {
armorLevel = 2;
} else if (stack.is(ModTags.Items.MILITARY_ARMOR_HEAVY)) {
armorLevel = 3;
}
ResourceLocation texture = switch (armorLevel) {
case 2 -> LEVEL2;
case 3 -> LEVEL3;
default -> LEVEL1;
};
ResourceLocation frame = switch (armorLevel) {
case 2 -> LEVEL2_FRAME;
case 3 -> LEVEL3_FRAME;
default -> LEVEL1_FRAME;
};
int length = armorLevel * 30;
guiGraphics.pose().pushPose();
// 渲染图标
guiGraphics.blit(ICON, 10, h - 16, 0, 0, 8, 8, 8, 8);
// 渲染框架
guiGraphics.blit(frame, 20, h - 15, 0, 0, length, 6, length, 6);
// 渲染盔甲值
guiGraphics.blit(texture, 20, h - 15, 0, 0, (int) amount, 6, length, 6);
guiGraphics.pose().popPose();
}
}

View file

@ -107,12 +107,12 @@ public class LivingEventHandler {
double armorValue = 0;
if (armor.getItem() != ItemStack.EMPTY.getItem()) {
if (armor != ItemStack.EMPTY) {
armorValue = armor.getOrCreateTag().getDouble("ArmorPlate");
armor.getOrCreateTag().putDouble("ArmorPlate", Math.max(armor.getOrCreateTag().getDouble("ArmorPlate") - damage, 0));
}
event.setAmount((float)(Math.max(damage - armorValue, 0)));
event.setAmount((float) (Math.max(damage - armorValue, 0)));
if (entity instanceof TargetEntity && sourceEntity instanceof Player player) {
player.displayClientMessage(Component.literal("Damage:" + new DecimalFormat("##.#").format(damage) +

View file

@ -1,6 +1,5 @@
package net.mcreator.superbwarfare.item;
import net.mcreator.superbwarfare.init.ModTags;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
@ -27,7 +26,7 @@ public class ArmorPlate extends Item {
ItemStack stack = playerIn.getItemInHand(handIn);
ItemStack armor = playerIn.getItemBySlot(EquipmentSlot.CHEST);
if (armor.getItem() == ItemStack.EMPTY.getItem()) return InteractionResultHolder.fail(stack);
if (armor == ItemStack.EMPTY) return InteractionResultHolder.fail(stack);
int armorLevel = 1;
if (armor.is(ModTags.Items.MILITARY_ARMOR)) {
@ -51,7 +50,6 @@ public class ArmorPlate extends Item {
@Override
public ItemStack finishUsingItem(ItemStack pStack, Level pLevel, LivingEntity pLivingEntity) {
if (!pLevel.isClientSide) {
ItemStack armor = pLivingEntity.getItemBySlot(EquipmentSlot.CHEST);
int armorLevel = 1;