From 580d7105e37dc14993b5d46c6bd34e412fb0cb30 Mon Sep 17 00:00:00 2001 From: deirn Date: Mon, 20 Nov 2023 12:22:21 +0700 Subject: [PATCH] always check for block entity override --- .../java/mcp/mobius/waila/WailaClient.java | 2 +- .../waila/gui/hud/ComponentHandler.java | 82 ++++++++++--------- .../mobius/waila/gui/hud/TooltipRenderer.java | 6 +- .../mcp/mobius/waila/network/Packets.java | 6 +- .../mcp/mobius/waila/registry/Register.java | 11 +-- 5 files changed, 58 insertions(+), 49 deletions(-) diff --git a/src/main/java/mcp/mobius/waila/WailaClient.java b/src/main/java/mcp/mobius/waila/WailaClient.java index e836e8129..ddf9faf3b 100644 --- a/src/main/java/mcp/mobius/waila/WailaClient.java +++ b/src/main/java/mcp/mobius/waila/WailaClient.java @@ -93,7 +93,7 @@ protected static void onClientTick() { protected static void onItemTooltip(ItemStack stack, List tooltip) { if (PluginConfig.CLIENT.getBoolean(WailaConstants.CONFIG_SHOW_ITEM_MOD_NAME)) { for (var listener : Registrar.INSTANCE.eventListeners.get(Object.class)) { - var name = listener.getHoveredItemModName(stack, PluginConfig.CLIENT); + var name = listener.value().getHoveredItemModName(stack, PluginConfig.CLIENT); if (name != null) { tooltip.add(IWailaConfig.get().getFormatter().modName(name)); return; diff --git a/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java b/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java index fc6cafb33..21edd201b 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java @@ -1,12 +1,9 @@ package mcp.mobius.waila.gui.hud; -import java.util.List; - import io.netty.buffer.Unpooled; import lol.bai.badpackets.api.PacketSender; import mcp.mobius.waila.Waila; import mcp.mobius.waila.access.DataAccessor; -import mcp.mobius.waila.api.IBlockComponentProvider; import mcp.mobius.waila.api.ITooltipComponent; import mcp.mobius.waila.api.TooltipPosition; import mcp.mobius.waila.api.component.EmptyComponent; @@ -22,7 +19,6 @@ import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.EntityHitResult; import net.minecraft.world.phys.HitResult; -import org.jetbrains.annotations.Nullable; public class ComponentHandler { @@ -50,7 +46,8 @@ public static void gatherBlock(DataAccessor accessor, Tooltip tooltip, TooltipPo private static void handleBlock(DataAccessor accessor, Tooltip tooltip, Object obj, TooltipPosition position) { var registrar = Registrar.INSTANCE; var providers = registrar.blockComponent.get(position).get(obj); - for (var provider : providers) { + for (var entry : providers) { + var provider = entry.value(); try { switch (position) { case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT); @@ -85,7 +82,8 @@ public static void gatherEntity(Entity entity, DataAccessor accessor, Tooltip to } var providers = registrar.entityComponent.get(position).get(entity); - for (var provider : providers) { + for (var entry : providers) { + var provider = entry.value(); try { switch (position) { case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT); @@ -106,45 +104,46 @@ public static ITooltipComponent getIcon(HitResult target) { if (target.getType() == HitResult.Type.ENTITY) { var providers = registrar.entityIcon.get(data.getEntity()); for (var provider : providers) { - var icon = provider.getIcon(data, config); + var icon = provider.value().getIcon(data, config); if (icon != null) { return icon; } } } else { var state = data.getBlockState(); - if (state.isAir()) { - return EmptyComponent.INSTANCE; - } + if (state.isAir()) return EmptyComponent.INSTANCE; + + ITooltipComponent result = null; + var priority = 0; - var component = getBlockIcon(registrar.blockIcon.get(state.getBlock())); - if (component != null) { - return component; + for (var provider : registrar.blockIcon.get(state.getBlock())) { + var icon = provider.value().getIcon(DataAccessor.INSTANCE, PluginConfig.CLIENT); + if (icon != null) { + result = icon; + priority = provider.priority(); + break; + } } var blockEntity = data.getBlockEntity(); if (blockEntity != null) { - component = getBlockIcon(registrar.blockIcon.get(blockEntity)); - if (component != null) { - return component; + for (var provider : registrar.blockIcon.get(blockEntity)) { + if (provider.priority() >= priority) break; + + var icon = provider.value().getIcon(DataAccessor.INSTANCE, PluginConfig.CLIENT); + if (icon != null) { + result = icon; + break; + } } } + + if (result != null) return result; } return EmptyComponent.INSTANCE; } - @Nullable - private static ITooltipComponent getBlockIcon(List providers) { - for (var provider : providers) { - var icon = provider.getIcon(DataAccessor.INSTANCE, PluginConfig.CLIENT); - if (icon != null) { - return icon; - } - } - return null; - } - public static Entity getOverrideEntity(HitResult target) { if (target == null || target.getType() != HitResult.Type.ENTITY) { return null; @@ -155,7 +154,7 @@ public static Entity getOverrideEntity(HitResult target) { var overrideProviders = registrar.entityOverride.get(entity); for (var provider : overrideProviders) { - var override = provider.getOverride(DataAccessor.INSTANCE, PluginConfig.CLIENT); + var override = provider.value().getOverride(DataAccessor.INSTANCE, PluginConfig.CLIENT); if (override != null) { return override; } @@ -168,28 +167,37 @@ public static BlockState getOverrideBlock(HitResult target) { var registrar = Registrar.INSTANCE; Level world = Minecraft.getInstance().level; + if (world == null) return null; + var pos = ((BlockHitResult) target).getBlockPos(); - //noinspection ConstantConditions - var state = world.getBlockState(pos); + final var state = world.getBlockState(pos); + + BlockState override = null; + var priority = 0; var providers = registrar.blockOverride.get(state.getBlock()); for (var provider : providers) { - var override = provider.getOverride(DataAccessor.INSTANCE, PluginConfig.CLIENT); - if (override != null) { - return override; + var blockOverride = provider.value().getOverride(DataAccessor.INSTANCE, PluginConfig.CLIENT); + if (blockOverride != null) { + override = blockOverride; + priority = provider.priority(); + break; } } var blockEntity = world.getBlockEntity(pos); providers = registrar.blockOverride.get(blockEntity); for (var provider : providers) { - var override = provider.getOverride(DataAccessor.INSTANCE, PluginConfig.CLIENT); - if (override != null) { - return override; + if (provider.priority() >= priority) break; + + var beOverride = provider.value().getOverride(DataAccessor.INSTANCE, PluginConfig.CLIENT); + if (beOverride != null) { + override = beOverride; + break; } } - return state; + return override != null ? override : state; } } diff --git a/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java b/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java index 7ee2c5357..55e3f5296 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java @@ -102,7 +102,7 @@ public static Rectangle endBuild() { if (state.fireEvent()) { for (var listener : Registrar.INSTANCE.eventListeners.get(Object.class)) { - listener.onHandleTooltip(TOOLTIP, DataAccessor.INSTANCE, PluginConfig.CLIENT); + listener.value().onHandleTooltip(TOOLTIP, DataAccessor.INSTANCE, PluginConfig.CLIENT); } } @@ -272,7 +272,7 @@ private static void render0(Minecraft client, PoseStack matrices, float delta) { var canceller = EventCanceller.INSTANCE; canceller.setCanceled(false); for (var listener : Registrar.INSTANCE.eventListeners.get(Object.class)) { - listener.onBeforeTooltipRender(matrices, rect, DataAccessor.INSTANCE, PluginConfig.CLIENT, canceller); + listener.value().onBeforeTooltipRender(matrices, rect, DataAccessor.INSTANCE, PluginConfig.CLIENT, canceller); if (canceller.isCanceled()) { matrices.popPose(); RenderSystem.enableDepthTest(); @@ -311,7 +311,7 @@ private static void render0(Minecraft client, PoseStack matrices, float delta) { if (state.fireEvent()) { for (var listener : Registrar.INSTANCE.eventListeners.get(Object.class)) { - listener.onAfterTooltipRender(matrices, rect, DataAccessor.INSTANCE, PluginConfig.CLIENT); + listener.value().onAfterTooltipRender(matrices, rect, DataAccessor.INSTANCE, PluginConfig.CLIENT); } } diff --git a/src/main/java/mcp/mobius/waila/network/Packets.java b/src/main/java/mcp/mobius/waila/network/Packets.java index 12d8a2085..cb40ee4a1 100644 --- a/src/main/java/mcp/mobius/waila/network/Packets.java +++ b/src/main/java/mcp/mobius/waila/network/Packets.java @@ -131,7 +131,7 @@ public static void initServer() { IServerAccessor accessor = ServerAccessor.INSTANCE.set(world, player, new EntityHitResult(entity, hitPos), entity); for (var provider : registrar.entityData.get(entity)) { - tryAppendData(provider, accessor); + tryAppendData(provider.value(), accessor); } raw.putInt("WailaEntityID", entity.getId()); @@ -168,11 +168,11 @@ public static void initServer() { IServerAccessor accessor = ServerAccessor.INSTANCE.set(world, player, hitResult, blockEntity); for (var provider : registrar.blockData.get(blockEntity)) { - tryAppendData(provider, accessor); + tryAppendData(provider.value(), accessor); } for (var provider : registrar.blockData.get(state.getBlock())) { - tryAppendData(provider, accessor); + tryAppendData(provider.value(), accessor); } raw.putInt("x", pos.getX()); diff --git a/src/main/java/mcp/mobius/waila/registry/Register.java b/src/main/java/mcp/mobius/waila/registry/Register.java index 81f6915d0..2155aeac9 100644 --- a/src/main/java/mcp/mobius/waila/registry/Register.java +++ b/src/main/java/mcp/mobius/waila/registry/Register.java @@ -13,7 +13,7 @@ public class Register { private final Map, Set>> map = new Object2ObjectOpenHashMap<>(); - private final Map, List> cache = new Object2ObjectOpenHashMap<>(); + private final Map, List>> cache = new Object2ObjectOpenHashMap<>(); private boolean reversed = false; @@ -26,7 +26,7 @@ public void add(Class key, T value, int priority) { .add(new Entry<>(value, priority)); } - public List get(Object obj) { + public List> get(Object obj) { if (obj == null) { return ObjectLists.emptyList(); } @@ -53,9 +53,10 @@ public List get(Object obj) { } entries.sort(comparator); - List values = entries.isEmpty() ? ObjectLists.emptyList() : entries.stream().map(Entry::value).toList(); - cache.put(clazz, values); - return values; + + List> result = entries.isEmpty() ? ObjectLists.emptyList() : entries; + cache.put(clazz, result); + return result; } public Map, Set>> getMap() {