调整自定义tooltip
This commit is contained in:
parent
6ba751c1dd
commit
fea9657fe6
2 changed files with 178 additions and 32 deletions
|
@ -13,6 +13,7 @@ import net.minecraft.client.gui.Font;
|
||||||
import net.minecraft.client.gui.GuiGraphics;
|
import net.minecraft.client.gui.GuiGraphics;
|
||||||
import net.minecraft.client.gui.screens.Screen;
|
import net.minecraft.client.gui.screens.Screen;
|
||||||
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
|
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
|
||||||
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.util.Mth;
|
import net.minecraft.util.Mth;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
@ -36,22 +37,44 @@ public class ClientImageTooltip implements ClientTooltipComponent {
|
||||||
public void renderImage(@NotNull Font font, int x, int y, GuiGraphics guiGraphics) {
|
public void renderImage(@NotNull Font font, int x, int y, GuiGraphics guiGraphics) {
|
||||||
guiGraphics.pose().pushPose();
|
guiGraphics.pose().pushPose();
|
||||||
|
|
||||||
int yo = 0;
|
renderDamageAndRpmTooltip(font, guiGraphics, x, y);
|
||||||
if (renderWeaponDamageTooltip(font, guiGraphics, x, y + yo)) yo += 10;
|
renderLevelAndUpgradePointTooltip(font, guiGraphics, x, y + 10);
|
||||||
if (renderWeaponLevelTooltip(font, guiGraphics, x, y + yo)) yo += 10;
|
|
||||||
if (renderWeaponBypassTooltip(font, guiGraphics, x, y + yo)) yo += 10;
|
|
||||||
if (renderWeaponEditTooltip(font, guiGraphics, x, y + yo)) yo += 20;
|
|
||||||
|
|
||||||
|
int yo = 20;
|
||||||
|
if (shouldRenderBypassAndHeadshotTooltip()) {
|
||||||
|
renderBypassAndHeadshotTooltip(font, guiGraphics, x, y + yo);
|
||||||
|
yo += 10;
|
||||||
|
}
|
||||||
|
if (shouldRenderEditTooltip()) {
|
||||||
|
renderWeaponEditTooltip(font, guiGraphics, x, y + yo);
|
||||||
|
yo += 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldRenderPerks()) {
|
||||||
if (!Screen.hasShiftDown()) {
|
if (!Screen.hasShiftDown()) {
|
||||||
renderPerksShortcut(guiGraphics, x, y + yo);
|
renderPerksShortcut(guiGraphics, x, y + yo);
|
||||||
yo += 20;
|
} else {
|
||||||
|
renderPerks(font, guiGraphics, x, y + yo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
guiGraphics.pose().popPose();
|
guiGraphics.pose().popPose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean shouldRenderBypassAndHeadshotTooltip() {
|
||||||
|
return ItemNBTTool.getDouble(stack, "BypassesArmor", 0) > 0 || ItemNBTTool.getDouble(stack, "headshot", 0) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldRenderEditTooltip() {
|
||||||
|
return stack.is(ModTags.Items.CAN_CUSTOM_GUN);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldRenderPerks() {
|
||||||
|
return PerkHelper.getPerkByType(stack, Perk.Type.AMMO) != null || PerkHelper.getPerkByType(stack, Perk.Type.DAMAGE) != null || PerkHelper.getPerkByType(stack, Perk.Type.FUNCTIONAL) != null;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO 等nbt重置后,修改nbt位置
|
// TODO 等nbt重置后,修改nbt位置
|
||||||
private boolean renderWeaponDamageTooltip(Font font, GuiGraphics guiGraphics, int x, int y) {
|
private void renderDamageAndRpmTooltip(Font font, GuiGraphics guiGraphics, int x, int y) {
|
||||||
double damage = ItemNBTTool.getDouble(stack, "damage", 0) * TooltipTool.perkDamage(stack);
|
double damage = ItemNBTTool.getDouble(stack, "damage", 0) * TooltipTool.perkDamage(stack);
|
||||||
var damageComponent = Component.translatable("des.superbwarfare.tips.damage").withStyle(ChatFormatting.GRAY)
|
var damageComponent = Component.translatable("des.superbwarfare.tips.damage").withStyle(ChatFormatting.GRAY)
|
||||||
.append(Component.literal("").withStyle(ChatFormatting.RESET))
|
.append(Component.literal("").withStyle(ChatFormatting.RESET))
|
||||||
|
@ -68,11 +91,9 @@ public class ClientImageTooltip implements ClientTooltipComponent {
|
||||||
int xo = font.width(damageComponent.getVisualOrderText());
|
int xo = font.width(damageComponent.getVisualOrderText());
|
||||||
guiGraphics.drawString(font, rpmComponent, x + xo + 16, y, 0xFFFFFF);
|
guiGraphics.drawString(font, rpmComponent, x + xo + 16, y, 0xFFFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean renderWeaponLevelTooltip(Font font, GuiGraphics guiGraphics, int x, int y) {
|
private void renderLevelAndUpgradePointTooltip(Font font, GuiGraphics guiGraphics, int x, int y) {
|
||||||
int level = ItemNBTTool.getInt(stack, "Level", 0);
|
int level = ItemNBTTool.getInt(stack, "Level", 0);
|
||||||
double rate = ItemNBTTool.getDouble(stack, "Exp", 0) / (20 * Math.pow(level, 2) + 160 * level + 20);
|
double rate = ItemNBTTool.getDouble(stack, "Exp", 0) / (20 * Math.pow(level, 2) + 160 * level + 20);
|
||||||
|
|
||||||
|
@ -104,11 +125,9 @@ public class ClientImageTooltip implements ClientTooltipComponent {
|
||||||
|
|
||||||
int xo = font.width(levelComponent.getVisualOrderText());
|
int xo = font.width(levelComponent.getVisualOrderText());
|
||||||
guiGraphics.drawString(font, upgradeComponent, x + xo + 16, y, 0xFFFFFF);
|
guiGraphics.drawString(font, upgradeComponent, x + xo + 16, y, 0xFFFFFF);
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean renderWeaponBypassTooltip(Font font, GuiGraphics guiGraphics, int x, int y) {
|
private void renderBypassAndHeadshotTooltip(Font font, GuiGraphics guiGraphics, int x, int y) {
|
||||||
double perkBypassArmorRate = 0;
|
double perkBypassArmorRate = 0;
|
||||||
var perk = PerkHelper.getPerkByType(stack, Perk.Type.AMMO);
|
var perk = PerkHelper.getPerkByType(stack, Perk.Type.AMMO);
|
||||||
|
|
||||||
|
@ -121,9 +140,7 @@ public class ClientImageTooltip implements ClientTooltipComponent {
|
||||||
.append(Component.literal("").withStyle(ChatFormatting.RESET))
|
.append(Component.literal("").withStyle(ChatFormatting.RESET))
|
||||||
.append(Component.literal(new DecimalFormat("##.##").format(bypassRate * 100) + "%").withStyle(ChatFormatting.GOLD));
|
.append(Component.literal(new DecimalFormat("##.##").format(bypassRate * 100) + "%").withStyle(ChatFormatting.GOLD));
|
||||||
|
|
||||||
if (bypassRate > 0) {
|
|
||||||
guiGraphics.drawString(font, bypassComponent, x, y, 0xFFFFFF);
|
guiGraphics.drawString(font, bypassComponent, x, y, 0xFFFFFF);
|
||||||
}
|
|
||||||
|
|
||||||
double headshot = ItemNBTTool.getDouble(stack, "headshot", 0);
|
double headshot = ItemNBTTool.getDouble(stack, "headshot", 0);
|
||||||
var headshotComponent = Component.translatable("des.superbwarfare.tips.headshot").withStyle(ChatFormatting.GRAY)
|
var headshotComponent = Component.translatable("des.superbwarfare.tips.headshot").withStyle(ChatFormatting.GRAY)
|
||||||
|
@ -131,22 +148,13 @@ public class ClientImageTooltip implements ClientTooltipComponent {
|
||||||
.append(Component.literal(new DecimalFormat("##.#").format(headshot) + "x").withStyle(ChatFormatting.AQUA));
|
.append(Component.literal(new DecimalFormat("##.#").format(headshot) + "x").withStyle(ChatFormatting.AQUA));
|
||||||
|
|
||||||
int xo = bypassRate > 0 ? font.width(bypassComponent.getVisualOrderText()) : 0;
|
int xo = bypassRate > 0 ? font.width(bypassComponent.getVisualOrderText()) : 0;
|
||||||
if (headshot > 0) {
|
|
||||||
guiGraphics.drawString(font, headshotComponent, x + xo + 16, y, 0xFFFFFF);
|
guiGraphics.drawString(font, headshotComponent, x + xo + 16, y, 0xFFFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bypassRate > 0 || headshot > 0;
|
private void renderWeaponEditTooltip(Font font, GuiGraphics guiGraphics, int x, int y) {
|
||||||
}
|
|
||||||
|
|
||||||
private boolean renderWeaponEditTooltip(Font font, GuiGraphics guiGraphics, int x, int y) {
|
|
||||||
if (stack.is(ModTags.Items.CAN_CUSTOM_GUN)) {
|
|
||||||
var editComponent = Component.translatable("des.superbwarfare.tips.edit",
|
var editComponent = Component.translatable("des.superbwarfare.tips.edit",
|
||||||
"[" + ModKeyMappings.EDIT_MODE.getKey().getDisplayName().getString() + "]").withStyle(ChatFormatting.LIGHT_PURPLE);
|
"[" + ModKeyMappings.EDIT_MODE.getKey().getDisplayName().getString() + "]").withStyle(ChatFormatting.LIGHT_PURPLE).withStyle(ChatFormatting.ITALIC);
|
||||||
guiGraphics.drawString(font, editComponent, x, y + 10, 0xFFFFFF);
|
guiGraphics.drawString(font, editComponent, x, y + 10, 0xFFFFFF);
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderPerksShortcut(GuiGraphics guiGraphics, int x, int y) {
|
private void renderPerksShortcut(GuiGraphics guiGraphics, int x, int y) {
|
||||||
|
@ -181,13 +189,151 @@ public class ClientImageTooltip implements ClientTooltipComponent {
|
||||||
guiGraphics.pose().popPose();
|
guiGraphics.pose().popPose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void renderPerks(Font font, GuiGraphics guiGraphics, int x, int y) {
|
||||||
|
guiGraphics.pose().pushPose();
|
||||||
|
|
||||||
|
guiGraphics.drawString(font, Component.translatable("perk.superbwarfare.tips").withStyle(ChatFormatting.GOLD).withStyle(ChatFormatting.UNDERLINE), x, y + 10, 0xFFFFFF);
|
||||||
|
|
||||||
|
int yOffset = -10;
|
||||||
|
|
||||||
|
Perk ammoPerk = PerkHelper.getPerkByType(stack, Perk.Type.AMMO);
|
||||||
|
if (ammoPerk != null && PerkHelper.getPerkItem(ammoPerk).isPresent()) {
|
||||||
|
yOffset += 30;
|
||||||
|
var ammoItem = PerkHelper.getPerkItem(ammoPerk).get().get();
|
||||||
|
guiGraphics.renderItem(ammoItem.getDefaultInstance(), x, y + 4 + yOffset);
|
||||||
|
|
||||||
|
CompoundTag ammoTag = PerkHelper.getPerkTag(stack, Perk.Type.AMMO);
|
||||||
|
if (!ammoTag.isEmpty()) {
|
||||||
|
var ids = ammoTag.getString("id").split(":");
|
||||||
|
if (ids.length > 1) {
|
||||||
|
String id = ids[1];
|
||||||
|
var ammoComponent = Component.translatable("item.superbwarfare." + id).withStyle(ChatFormatting.YELLOW)
|
||||||
|
.append(Component.literal(" ").withStyle(ChatFormatting.RESET))
|
||||||
|
.append(Component.literal(" Lvl. " + ammoTag.getInt("level")).withStyle(ChatFormatting.WHITE));
|
||||||
|
var ammoDesComponent = Component.translatable("des.superbwarfare." + id).withStyle(ChatFormatting.GRAY);
|
||||||
|
|
||||||
|
guiGraphics.drawString(font, ammoComponent, x + 20, y + yOffset + 2, 0xFFFFFF);
|
||||||
|
guiGraphics.drawString(font, ammoDesComponent, x + 20, y + yOffset + 12, 0xFFFFFF);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Perk funcPerk = PerkHelper.getPerkByType(stack, Perk.Type.FUNCTIONAL);
|
||||||
|
if (funcPerk != null && PerkHelper.getPerkItem(funcPerk).isPresent()) {
|
||||||
|
yOffset += 30;
|
||||||
|
var funcItem = PerkHelper.getPerkItem(funcPerk).get().get();
|
||||||
|
guiGraphics.renderItem(funcItem.getDefaultInstance(), x, y + 4 + yOffset);
|
||||||
|
|
||||||
|
CompoundTag funcTag = PerkHelper.getPerkTag(stack, Perk.Type.FUNCTIONAL);
|
||||||
|
if (!funcTag.isEmpty()) {
|
||||||
|
var ids = funcTag.getString("id").split(":");
|
||||||
|
if (ids.length > 1) {
|
||||||
|
String id = ids[1];
|
||||||
|
var funcComponent = Component.translatable("item.superbwarfare." + id).withStyle(ChatFormatting.GREEN)
|
||||||
|
.append(Component.literal(" ").withStyle(ChatFormatting.RESET))
|
||||||
|
.append(Component.literal(" Lvl. " + funcTag.getInt("level")).withStyle(ChatFormatting.WHITE));
|
||||||
|
var funcDesComponent = Component.translatable("des.superbwarfare." + id).withStyle(ChatFormatting.GRAY);
|
||||||
|
|
||||||
|
guiGraphics.drawString(font, funcComponent, x + 20, y + yOffset + 2, 0xFFFFFF);
|
||||||
|
guiGraphics.drawString(font, funcDesComponent, x + 20, y + yOffset + 12, 0xFFFFFF);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Perk damagePerk = PerkHelper.getPerkByType(stack, Perk.Type.DAMAGE);
|
||||||
|
if (damagePerk != null && PerkHelper.getPerkItem(damagePerk).isPresent()) {
|
||||||
|
yOffset += 30;
|
||||||
|
var damageItem = PerkHelper.getPerkItem(damagePerk).get().get();
|
||||||
|
guiGraphics.renderItem(damageItem.getDefaultInstance(), x, y + 4 + yOffset);
|
||||||
|
|
||||||
|
CompoundTag damageTag = PerkHelper.getPerkTag(stack, Perk.Type.DAMAGE);
|
||||||
|
if (!damageTag.isEmpty()) {
|
||||||
|
var ids = damageTag.getString("id").split(":");
|
||||||
|
if (ids.length > 1) {
|
||||||
|
String id = ids[1];
|
||||||
|
var damageComponent = Component.translatable("item.superbwarfare." + id).withStyle(ChatFormatting.RED)
|
||||||
|
.append(Component.literal(" ").withStyle(ChatFormatting.RESET))
|
||||||
|
.append(Component.literal(" Lvl. " + damageTag.getInt("level")).withStyle(ChatFormatting.WHITE));
|
||||||
|
var damageDesComponent = Component.translatable("des.superbwarfare." + id).withStyle(ChatFormatting.GRAY);
|
||||||
|
|
||||||
|
guiGraphics.drawString(font, damageComponent, x + 20, y + yOffset + 2, 0xFFFFFF);
|
||||||
|
guiGraphics.drawString(font, damageDesComponent, x + 20, y + yOffset + 12, 0xFFFFFF);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
guiGraphics.pose().popPose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getMaxDesWidth(Font font) {
|
||||||
|
if (!shouldRenderPerks()) return 0;
|
||||||
|
|
||||||
|
int width = 0;
|
||||||
|
|
||||||
|
CompoundTag ammoTag = PerkHelper.getPerkTag(stack, Perk.Type.AMMO);
|
||||||
|
if (!ammoTag.isEmpty()) {
|
||||||
|
var ids = ammoTag.getString("id").split(":");
|
||||||
|
if (ids.length > 1) {
|
||||||
|
String id = ids[1];
|
||||||
|
var ammoDesComponent = Component.translatable("des.superbwarfare." + id).withStyle(ChatFormatting.GRAY);
|
||||||
|
width = Math.max(width, font.width(ammoDesComponent));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CompoundTag funcTag = PerkHelper.getPerkTag(stack, Perk.Type.FUNCTIONAL);
|
||||||
|
if (!funcTag.isEmpty()) {
|
||||||
|
var ids = funcTag.getString("id").split(":");
|
||||||
|
if (ids.length > 1) {
|
||||||
|
String id = ids[1];
|
||||||
|
var funcDesComponent = Component.translatable("des.superbwarfare." + id).withStyle(ChatFormatting.GRAY);
|
||||||
|
width = Math.max(width, font.width(funcDesComponent));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CompoundTag damageTag = PerkHelper.getPerkTag(stack, Perk.Type.DAMAGE);
|
||||||
|
if (!damageTag.isEmpty()) {
|
||||||
|
var ids = damageTag.getString("id").split(":");
|
||||||
|
if (ids.length > 1) {
|
||||||
|
String id = ids[1];
|
||||||
|
var damageDesComponent = Component.translatable("des.superbwarfare." + id).withStyle(ChatFormatting.GRAY);
|
||||||
|
width = Math.max(width, font.width(damageDesComponent));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return width + 30;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
|
int height = Math.max(20, this.height);
|
||||||
|
|
||||||
|
if (shouldRenderBypassAndHeadshotTooltip()) height += 10;
|
||||||
|
if (shouldRenderEditTooltip()) height += 20;
|
||||||
|
if (shouldRenderPerks()) {
|
||||||
|
if (!Screen.hasShiftDown()) {
|
||||||
|
height += 20;
|
||||||
|
} else {
|
||||||
|
height += 20;
|
||||||
|
if (PerkHelper.getPerkByType(stack, Perk.Type.AMMO) != null) {
|
||||||
|
height += 30;
|
||||||
|
}
|
||||||
|
if (PerkHelper.getPerkByType(stack, Perk.Type.FUNCTIONAL) != null) {
|
||||||
|
height += 30;
|
||||||
|
}
|
||||||
|
if (PerkHelper.getPerkByType(stack, Perk.Type.DAMAGE) != null) {
|
||||||
|
height += 30;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getWidth(@NotNull Font font) {
|
public int getWidth(@NotNull Font font) {
|
||||||
|
if (Screen.hasShiftDown()) {
|
||||||
|
return Math.max(width, getMaxDesWidth(font));
|
||||||
|
}
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -220,6 +220,6 @@ public class AK12Item extends GunItem implements GeoItem, AnimatedItem {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Optional<TooltipComponent> getTooltipImage(@NotNull ItemStack pStack) {
|
public @NotNull Optional<TooltipComponent> getTooltipImage(@NotNull ItemStack pStack) {
|
||||||
return Optional.of(new ImageTooltip(128, 128, pStack));
|
return Optional.of(new ImageTooltip(128, 20, pStack));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue