Skip to content

Commit

Permalink
expose instance registry
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jan 31, 2024
1 parent 6a7398a commit 4dcf9da
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 47 deletions.
53 changes: 53 additions & 0 deletions src/api/java/mcp/mobius/waila/api/IInstanceRegistry.java
Original file line number Diff line number Diff line change
@@ -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 <T> the provider type
*/
@ApiStatus.NonExtendable
public interface IInstanceRegistry<T> {

/**
* Creates an instance registry.
*/
static <T> IInstanceRegistry<T> create() {
return IApiService.INSTANCE.createInstanceRegistry(false);
}

/**
* Creates an instance registry with the priority comparison reversed.
*/
static <T> IInstanceRegistry<T> 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<Entry<T>> get(Object target);

@ApiStatus.NonExtendable
interface Entry<T> {

T instance();

int priority();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,4 +66,6 @@ public interface IApiService {

<T> IRegistryFilter.Builder<T> createRegistryFilterBuilder(ResourceKey<? extends Registry<T>> registryKey);

<T> IInstanceRegistry<T> createInstanceRegistry(boolean reversed);

}
2 changes: 1 addition & 1 deletion src/main/java/mcp/mobius/waila/WailaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected static void onClientTick() {
protected static void onItemTooltip(ItemStack stack, List<Component> 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;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/mcp/mobius/waila/debug/DumpGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,7 +86,7 @@ public static Path generate(String name) {
}
}

private static <T> void createSection(StringBuilder builder, String subsection, Register<T> registry) {
private static <T> void createSection(StringBuilder builder, String subsection, InstanceRegistry<T> registry) {
var map = registry.getMap();

if (map.isEmpty())
Expand All @@ -100,7 +100,7 @@ private static <T> 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 -> {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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();
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public void receive(MinecraftServer server, ServerPlayer player, ServerGamePacke
IServerAccessor<BlockEntity> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void receive(MinecraftServer server, ServerPlayer player, ServerGamePacke
IServerAccessor<Entity> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
public class InstanceRegistry<T> implements IInstanceRegistry<T> {

private final Map<Class<?>, Set<Entry<T>>> map = new Object2ObjectOpenHashMap<>();
private final Map<Class<?>, Set<EntryImpl<T>>> map = new Object2ObjectOpenHashMap<>();
private final Map<Class<?>, List<Entry<T>>> cache = new Object2ObjectOpenHashMap<>();

private boolean reversed = false;
Expand All @@ -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<Entry<T>> get(Object obj) {
if (obj == null) {
@Override
public List<Entry<T>> get(Object target) {
if (target == null) {
return ObjectLists.emptyList();
}

var clazz = obj.getClass();
var clazz = target.getClass();
if (clazz == Object.class) {
return ObjectLists.emptyList();
}
Expand All @@ -42,12 +45,12 @@ public List<Entry<T>> get(Object obj) {

List<Entry<T>> entries = new ObjectArrayList<>();
map.forEach((k, v) -> {
if (k.isInstance(obj)) {
if (k.isInstance(target)) {
entries.addAll(v);
}
});

Comparator<Entry<T>> comparator = Comparator.comparingInt(e -> e.priority);
Comparator<Entry<T>> comparator = Comparator.comparingInt(Entry::priority);
if (reversed) {
comparator = comparator.reversed();
}
Expand All @@ -59,11 +62,11 @@ public List<Entry<T>> get(Object obj) {
return result;
}

public Map<Class<?>, Set<Entry<T>>> getMap() {
public Map<Class<?>, Set<EntryImpl<T>>> getMap() {
return map;
}

public record Entry<T>(T value, int priority) {
public record EntryImpl<T>(T instance, int priority) implements Entry<T> {

}

Expand Down
29 changes: 15 additions & 14 deletions src/main/java/mcp/mobius/waila/registry/Registrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ public enum Registrar implements IRegistrar {

private static final Log LOG = Log.create();

public final Register<IBlockComponentProvider> blockOverride = new Register<>();
public final Register<IBlockComponentProvider> blockIcon = new Register<>();
public final Register<IBlockComponentProvider> blockDataCtx = new Register<>();
public final Register<IDataProvider<BlockEntity>> blockData = new Register<>();
public final Map<TooltipPosition, Register<IBlockComponentProvider>> blockComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> {
public final InstanceRegistry<IBlockComponentProvider> blockOverride = new InstanceRegistry<>();
public final InstanceRegistry<IBlockComponentProvider> blockIcon = new InstanceRegistry<>();
public final InstanceRegistry<IBlockComponentProvider> blockDataCtx = new InstanceRegistry<>();
public final InstanceRegistry<IDataProvider<BlockEntity>> blockData = new InstanceRegistry<>();
public final Map<TooltipPosition, InstanceRegistry<IBlockComponentProvider>> 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<IEntityComponentProvider> entityOverride = new Register<>();
public final Register<IEntityComponentProvider> entityIcon = new Register<>();
public final Register<IEntityComponentProvider> entityDataCtx = new Register<>();
public final Register<IDataProvider<Entity>> entityData = new Register<>();
public final Map<TooltipPosition, Register<IEntityComponentProvider>> entityComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> {
public final InstanceRegistry<IEntityComponentProvider> entityOverride = new InstanceRegistry<>();
public final InstanceRegistry<IEntityComponentProvider> entityIcon = new InstanceRegistry<>();
public final InstanceRegistry<IEntityComponentProvider> entityDataCtx = new InstanceRegistry<>();
public final InstanceRegistry<IDataProvider<Entity>> entityData = new InstanceRegistry<>();
public final Map<TooltipPosition, InstanceRegistry<IEntityComponentProvider>> 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<IEventListener> eventListeners = Util.make(new Register<>(), Register::reversed);
public final Register<IRayCastVectorProvider> raycastVectorProviders = new Register<>();
public final InstanceRegistry<IEventListener> eventListeners = Util.make(new InstanceRegistry<>(), InstanceRegistry::reversed);
public final InstanceRegistry<IRayCastVectorProvider> raycastVectorProviders = new InstanceRegistry<>();

public final BlacklistConfig blacklist = new BlacklistConfig();

Expand Down Expand Up @@ -350,6 +350,7 @@ public void lock() {
}

Waila.BLACKLIST_CONFIG.save();

}

private void assertLock() {
Expand Down
Loading

0 comments on commit 4dcf9da

Please sign in to comment.