diff --git a/src/api/java/mcp/mobius/waila/api/IInstanceRegistry.java b/src/api/java/mcp/mobius/waila/api/IInstanceRegistry.java new file mode 100644 index 000000000..ab1219c19 --- /dev/null +++ b/src/api/java/mcp/mobius/waila/api/IInstanceRegistry.java @@ -0,0 +1,53 @@ +package mcp.mobius.waila.api; + +import java.util.List; + +import mcp.mobius.waila.api.__internal__.IApiService; +import org.jetbrains.annotations.ApiStatus; + +/** + * Registry for attaching object instances to a type and its subtypes. + * + * @param the provider type + */ +@ApiStatus.NonExtendable +public interface IInstanceRegistry { + + /** + * Creates an instance registry. + */ + static IInstanceRegistry create() { + return IApiService.INSTANCE.createInstanceRegistry(false); + } + + /** + * Creates an instance registry with the priority comparison reversed. + */ + static IInstanceRegistry createReversed() { + return IApiService.INSTANCE.createInstanceRegistry(true); + } + + /** + * Registers an instance. + * + * @param bound the upper bound type for the instance to apply + * @param instance the instance + * @param priority the priority + */ + void add(Class bound, T instance, int priority); + + /** + * Returns the instances that apply to the target sorted by their priority. + */ + List> get(Object target); + + @ApiStatus.NonExtendable + interface Entry { + + T instance(); + + int priority(); + + } + +} diff --git a/src/api/java/mcp/mobius/waila/api/__internal__/IApiService.java b/src/api/java/mcp/mobius/waila/api/__internal__/IApiService.java index df6389aad..0917c12b4 100644 --- a/src/api/java/mcp/mobius/waila/api/__internal__/IApiService.java +++ b/src/api/java/mcp/mobius/waila/api/__internal__/IApiService.java @@ -5,6 +5,7 @@ import com.mojang.blaze3d.vertex.BufferBuilder; import mcp.mobius.waila.api.IBlacklistConfig; +import mcp.mobius.waila.api.IInstanceRegistry; import mcp.mobius.waila.api.IJsonConfig; import mcp.mobius.waila.api.IModInfo; import mcp.mobius.waila.api.IPluginInfo; @@ -65,4 +66,6 @@ public interface IApiService { IRegistryFilter.Builder createRegistryFilterBuilder(ResourceKey> registryKey); + IInstanceRegistry createInstanceRegistry(boolean reversed); + } diff --git a/src/main/java/mcp/mobius/waila/WailaClient.java b/src/main/java/mcp/mobius/waila/WailaClient.java index 1c48b6e74..bc7dc95fe 100644 --- a/src/main/java/mcp/mobius/waila/WailaClient.java +++ b/src/main/java/mcp/mobius/waila/WailaClient.java @@ -92,7 +92,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.value().getHoveredItemModName(stack, PluginConfig.CLIENT); + var name = listener.instance().getHoveredItemModName(stack, PluginConfig.CLIENT); if (name != null) { tooltip.add(IWailaConfig.get().getFormatter().modName(name)); return; diff --git a/src/main/java/mcp/mobius/waila/debug/DumpGenerator.java b/src/main/java/mcp/mobius/waila/debug/DumpGenerator.java index fcc9fd2fd..2a3910ad8 100644 --- a/src/main/java/mcp/mobius/waila/debug/DumpGenerator.java +++ b/src/main/java/mcp/mobius/waila/debug/DumpGenerator.java @@ -10,7 +10,7 @@ import mcp.mobius.waila.Waila; import mcp.mobius.waila.api.IPluginInfo; import mcp.mobius.waila.plugin.PluginInfo; -import mcp.mobius.waila.registry.Register; +import mcp.mobius.waila.registry.InstanceRegistry; import mcp.mobius.waila.registry.Registrar; import mcp.mobius.waila.util.Log; import org.jetbrains.annotations.Nullable; @@ -86,7 +86,7 @@ public static Path generate(String name) { } } - private static void createSection(StringBuilder builder, String subsection, Register registry) { + private static void createSection(StringBuilder builder, String subsection, InstanceRegistry registry) { var map = registry.getMap(); if (map.isEmpty()) @@ -100,7 +100,7 @@ private static void createSection(StringBuilder builder, String subsection, builder.append("\n| `").append(k.getName()).append("` "); var i = new int[]{0}; v.stream() - .map(o -> o.value().getClass().getName()) + .map(o -> o.instance().getClass().getName()) .distinct() .sorted(String::compareToIgnoreCase) .forEachOrdered(s -> { 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 824adb1e0..586249fdb 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java @@ -40,11 +40,11 @@ public static void requestBlockData(ClientAccessor accessor) { var player = Minecraft.getInstance().player; for (var entry : registrar.blockDataCtx.get(block)) { - DataWriter.CLIENT.tryAppend(player, entry.value(), accessor, PluginConfig.CLIENT, IBlockComponentProvider::appendDataContext); + DataWriter.CLIENT.tryAppend(player, entry.instance(), accessor, PluginConfig.CLIENT, IBlockComponentProvider::appendDataContext); } for (var entry : registrar.blockDataCtx.get(blockEntity)) { - DataWriter.CLIENT.tryAppend(player, entry.value(), accessor, PluginConfig.CLIENT, IBlockComponentProvider::appendDataContext); + DataWriter.CLIENT.tryAppend(player, entry.instance(), accessor, PluginConfig.CLIENT, IBlockComponentProvider::appendDataContext); } DataWriter.CLIENT.send(PacketSender.c2s(), player); @@ -65,7 +65,7 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object var registrar = Registrar.INSTANCE; var providers = registrar.blockComponent.get(position).get(obj); for (var entry : providers) { - var provider = entry.value(); + var provider = entry.instance(); try { switch (position) { case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT); @@ -93,7 +93,7 @@ public static void requestEntityData(Entity entity, ClientAccessor accessor) { var player = Minecraft.getInstance().player; for (var entry : registrar.entityDataCtx.get(entity)) { - DataWriter.CLIENT.tryAppend(player, entry.value(), accessor, PluginConfig.CLIENT, IEntityComponentProvider::appendDataContext); + DataWriter.CLIENT.tryAppend(player, entry.instance(), accessor, PluginConfig.CLIENT, IEntityComponentProvider::appendDataContext); } DataWriter.CLIENT.send(PacketSender.c2s(), player); @@ -107,7 +107,7 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip var providers = registrar.entityComponent.get(position).get(entity); for (var entry : providers) { - var provider = entry.value(); + var provider = entry.instance(); try { switch (position) { case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT); @@ -128,7 +128,7 @@ 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.value().getIcon(data, config); + var icon = provider.instance().getIcon(data, config); if (icon != null) { return icon; } @@ -141,7 +141,7 @@ public static ITooltipComponent getIcon(HitResult target) { var priority = 0; for (var provider : registrar.blockIcon.get(state.getBlock())) { - var icon = provider.value().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT); + var icon = provider.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT); if (icon != null) { result = icon; priority = provider.priority(); @@ -154,7 +154,7 @@ public static ITooltipComponent getIcon(HitResult target) { for (var provider : registrar.blockIcon.get(blockEntity)) { if (provider.priority() >= priority) break; - var icon = provider.value().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT); + var icon = provider.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT); if (icon != null) { result = icon; break; @@ -178,7 +178,7 @@ public static Entity getOverrideEntity(HitResult target) { var overrideProviders = registrar.entityOverride.get(entity); for (var provider : overrideProviders) { - var override = provider.value().getOverride(ClientAccessor.INSTANCE, PluginConfig.CLIENT); + var override = provider.instance().getOverride(ClientAccessor.INSTANCE, PluginConfig.CLIENT); if (override != null) { return override; } @@ -201,7 +201,7 @@ public static BlockState getOverrideBlock(HitResult target) { var providers = registrar.blockOverride.get(state.getBlock()); for (var provider : providers) { - var blockOverride = provider.value().getOverride(ClientAccessor.INSTANCE, PluginConfig.CLIENT); + var blockOverride = provider.instance().getOverride(ClientAccessor.INSTANCE, PluginConfig.CLIENT); if (blockOverride != null) { override = blockOverride; priority = provider.priority(); @@ -214,7 +214,7 @@ public static BlockState getOverrideBlock(HitResult target) { for (var provider : providers) { if (provider.priority() >= priority) break; - var beOverride = provider.value().getOverride(ClientAccessor.INSTANCE, PluginConfig.CLIENT); + var beOverride = provider.instance().getOverride(ClientAccessor.INSTANCE, PluginConfig.CLIENT); if (beOverride != null) { override = beOverride; break; diff --git a/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java b/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java index 350b26500..7cdfec670 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java @@ -74,7 +74,7 @@ public static void tick() { picker.pick(PickerAccessor.of(client, camera, pickRange, frameTime), results, PluginConfig.CLIENT); } else { for (var entry : Registrar.INSTANCE.raycastVectorProviders.get(Object.class)) { - var provider = entry.value(); + var provider = entry.instance(); if (!provider.isEnabled(PluginConfig.CLIENT)) continue; castOrigin = provider.getOrigin(frameTime); 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 c51363a46..ea9d8c01a 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java @@ -100,7 +100,7 @@ public static Rectangle endBuild() { if (state.fireEvent()) { for (var listener : Registrar.INSTANCE.eventListeners.get(Object.class)) { - listener.value().onHandleTooltip(TOOLTIP, ClientAccessor.INSTANCE, PluginConfig.CLIENT); + listener.instance().onHandleTooltip(TOOLTIP, ClientAccessor.INSTANCE, PluginConfig.CLIENT); } } @@ -269,7 +269,7 @@ private static void render0(Minecraft client, GuiGraphics ctx, float delta) { var canceller = EventCanceller.INSTANCE; canceller.setCanceled(false); for (var listener : Registrar.INSTANCE.eventListeners.get(Object.class)) { - listener.value().onBeforeTooltipRender(ctx, rect, ClientAccessor.INSTANCE, PluginConfig.CLIENT, canceller); + listener.instance().onBeforeTooltipRender(ctx, rect, ClientAccessor.INSTANCE, PluginConfig.CLIENT, canceller); if (canceller.isCanceled()) { ctx.pose().popPose(); RenderSystem.enableDepthTest(); @@ -306,7 +306,7 @@ private static void render0(Minecraft client, GuiGraphics ctx, float delta) { if (state.fireEvent()) { for (var listener : Registrar.INSTANCE.eventListeners.get(Object.class)) { - listener.value().onAfterTooltipRender(ctx, rect, ClientAccessor.INSTANCE, PluginConfig.CLIENT); + listener.instance().onAfterTooltipRender(ctx, rect, ClientAccessor.INSTANCE, PluginConfig.CLIENT); } } diff --git a/src/main/java/mcp/mobius/waila/network/play/c2s/BlockDataRequestPlayC2SPacket.java b/src/main/java/mcp/mobius/waila/network/play/c2s/BlockDataRequestPlayC2SPacket.java index a1a523132..cb3c1a268 100644 --- a/src/main/java/mcp/mobius/waila/network/play/c2s/BlockDataRequestPlayC2SPacket.java +++ b/src/main/java/mcp/mobius/waila/network/play/c2s/BlockDataRequestPlayC2SPacket.java @@ -58,11 +58,11 @@ public void receive(MinecraftServer server, ServerPlayer player, ServerGamePacke IServerAccessor accessor = ServerAccessor.INSTANCE.set(world, player, hitResult, blockEntity); for (var provider : registrar.blockData.get(blockEntity)) { - DataWriter.SERVER.tryAppend(player, provider.value(), accessor, PluginConfig.SERVER, IDataProvider::appendData); + DataWriter.SERVER.tryAppend(player, provider.instance(), accessor, PluginConfig.SERVER, IDataProvider::appendData); } for (var provider : registrar.blockData.get(state.getBlock())) { - DataWriter.SERVER.tryAppend(player, provider.value(), accessor, PluginConfig.SERVER, IDataProvider::appendData); + DataWriter.SERVER.tryAppend(player, provider.instance(), accessor, PluginConfig.SERVER, IDataProvider::appendData); } raw.putInt("x", pos.getX()); diff --git a/src/main/java/mcp/mobius/waila/network/play/c2s/EntityDataRequestPlayC2SPacket.java b/src/main/java/mcp/mobius/waila/network/play/c2s/EntityDataRequestPlayC2SPacket.java index cc5c2e998..00a311aa5 100644 --- a/src/main/java/mcp/mobius/waila/network/play/c2s/EntityDataRequestPlayC2SPacket.java +++ b/src/main/java/mcp/mobius/waila/network/play/c2s/EntityDataRequestPlayC2SPacket.java @@ -52,7 +52,7 @@ public void receive(MinecraftServer server, ServerPlayer player, ServerGamePacke IServerAccessor accessor = ServerAccessor.INSTANCE.set(world, player, new EntityHitResult(entity, hitPos), entity); for (var provider : registrar.entityData.get(entity)) { - DataWriter.SERVER.tryAppend(player, provider.value(), accessor, PluginConfig.SERVER, IDataProvider::appendData); + DataWriter.SERVER.tryAppend(player, provider.instance(), accessor, PluginConfig.SERVER, IDataProvider::appendData); } raw.putInt("WailaEntityID", entity.getId()); diff --git a/src/main/java/mcp/mobius/waila/registry/Register.java b/src/main/java/mcp/mobius/waila/registry/InstanceRegistry.java similarity index 67% rename from src/main/java/mcp/mobius/waila/registry/Register.java rename to src/main/java/mcp/mobius/waila/registry/InstanceRegistry.java index 2155aeac9..d3910c2fe 100644 --- a/src/main/java/mcp/mobius/waila/registry/Register.java +++ b/src/main/java/mcp/mobius/waila/registry/InstanceRegistry.java @@ -9,10 +9,11 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet; import it.unimi.dsi.fastutil.objects.ObjectLists; +import mcp.mobius.waila.api.IInstanceRegistry; -public class Register { +public class InstanceRegistry implements IInstanceRegistry { - private final Map, Set>> map = new Object2ObjectOpenHashMap<>(); + private final Map, Set>> map = new Object2ObjectOpenHashMap<>(); private final Map, List>> cache = new Object2ObjectOpenHashMap<>(); private boolean reversed = false; @@ -21,17 +22,19 @@ public void reversed() { reversed = true; } - public void add(Class key, T value, int priority) { + @Override + public void add(Class key, T instance, int priority) { map.computeIfAbsent(key, k -> new ObjectLinkedOpenHashSet<>()) - .add(new Entry<>(value, priority)); + .add(new EntryImpl<>(instance, priority)); } - public List> get(Object obj) { - if (obj == null) { + @Override + public List> get(Object target) { + if (target == null) { return ObjectLists.emptyList(); } - var clazz = obj.getClass(); + var clazz = target.getClass(); if (clazz == Object.class) { return ObjectLists.emptyList(); } @@ -42,12 +45,12 @@ public List> get(Object obj) { List> entries = new ObjectArrayList<>(); map.forEach((k, v) -> { - if (k.isInstance(obj)) { + if (k.isInstance(target)) { entries.addAll(v); } }); - Comparator> comparator = Comparator.comparingInt(e -> e.priority); + Comparator> comparator = Comparator.comparingInt(Entry::priority); if (reversed) { comparator = comparator.reversed(); } @@ -59,11 +62,11 @@ public List> get(Object obj) { return result; } - public Map, Set>> getMap() { + public Map, Set>> getMap() { return map; } - public record Entry(T value, int priority) { + public record EntryImpl(T instance, int priority) implements Entry { } diff --git a/src/main/java/mcp/mobius/waila/registry/Registrar.java b/src/main/java/mcp/mobius/waila/registry/Registrar.java index 4d033990a..01e95bae0 100644 --- a/src/main/java/mcp/mobius/waila/registry/Registrar.java +++ b/src/main/java/mcp/mobius/waila/registry/Registrar.java @@ -47,28 +47,28 @@ public enum Registrar implements IRegistrar { private static final Log LOG = Log.create(); - public final Register blockOverride = new Register<>(); - public final Register blockIcon = new Register<>(); - public final Register blockDataCtx = new Register<>(); - public final Register> blockData = new Register<>(); - public final Map> blockComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> { + public final InstanceRegistry blockOverride = new InstanceRegistry<>(); + public final InstanceRegistry blockIcon = new InstanceRegistry<>(); + public final InstanceRegistry blockDataCtx = new InstanceRegistry<>(); + public final InstanceRegistry> blockData = new InstanceRegistry<>(); + public final Map> blockComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> { for (var key : TooltipPosition.values()) { - map.put(key, new Register<>()); + map.put(key, new InstanceRegistry<>()); } }); - public final Register entityOverride = new Register<>(); - public final Register entityIcon = new Register<>(); - public final Register entityDataCtx = new Register<>(); - public final Register> entityData = new Register<>(); - public final Map> entityComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> { + public final InstanceRegistry entityOverride = new InstanceRegistry<>(); + public final InstanceRegistry entityIcon = new InstanceRegistry<>(); + public final InstanceRegistry entityDataCtx = new InstanceRegistry<>(); + public final InstanceRegistry> entityData = new InstanceRegistry<>(); + public final Map> entityComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> { for (var key : TooltipPosition.values()) { - map.put(key, new Register<>()); + map.put(key, new InstanceRegistry<>()); } }); - public final Register eventListeners = Util.make(new Register<>(), Register::reversed); - public final Register raycastVectorProviders = new Register<>(); + public final InstanceRegistry eventListeners = Util.make(new InstanceRegistry<>(), InstanceRegistry::reversed); + public final InstanceRegistry raycastVectorProviders = new InstanceRegistry<>(); public final BlacklistConfig blacklist = new BlacklistConfig(); @@ -350,6 +350,7 @@ public void lock() { } Waila.BLACKLIST_CONFIG.save(); + } private void assertLock() { diff --git a/src/main/java/mcp/mobius/waila/service/ApiService.java b/src/main/java/mcp/mobius/waila/service/ApiService.java index feaa5ffd1..b32e1fb3f 100644 --- a/src/main/java/mcp/mobius/waila/service/ApiService.java +++ b/src/main/java/mcp/mobius/waila/service/ApiService.java @@ -6,6 +6,7 @@ import com.mojang.blaze3d.vertex.BufferBuilder; import mcp.mobius.waila.Waila; import mcp.mobius.waila.api.IBlacklistConfig; +import mcp.mobius.waila.api.IInstanceRegistry; import mcp.mobius.waila.api.IJsonConfig; import mcp.mobius.waila.api.IModInfo; import mcp.mobius.waila.api.IPluginInfo; @@ -19,6 +20,7 @@ import mcp.mobius.waila.gui.hud.TooltipRenderer; import mcp.mobius.waila.gui.hud.theme.ThemeType; import mcp.mobius.waila.plugin.PluginInfo; +import mcp.mobius.waila.registry.InstanceRegistry; import mcp.mobius.waila.registry.RegistryFilter; import mcp.mobius.waila.util.DisplayUtil; import mcp.mobius.waila.util.ModInfo; @@ -149,4 +151,11 @@ public IRegistryFilter.Builder createRegistryFilterBuilder(ResourceKey(registryKey); } + @Override + public IInstanceRegistry createInstanceRegistry(boolean reversed) { + var registry = new InstanceRegistry(); + if (reversed) registry.reversed(); + return registry; + } + }