Skip to content

Commit

Permalink
always check for block entity override
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Nov 20, 2023
1 parent c7ba5b2 commit d2b424d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 51 deletions.
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.getHoveredItemModName(stack, PluginConfig.CLIENT);
var name = listener.value().getHoveredItemModName(stack, PluginConfig.CLIENT);
if (name != null) {
tooltip.add(IWailaConfig.get().getFormatter().modName(name));
return;
Expand Down
86 changes: 47 additions & 39 deletions src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package mcp.mobius.waila.gui.hud;

import java.util.List;

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;
Expand All @@ -21,7 +18,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 {

Expand All @@ -35,7 +31,7 @@ public static void gatherBlock(DataAccessor accessor, Tooltip tooltip, TooltipPo
if (blockEntity != null && accessor.isTimeElapsed(rate) && Waila.CONFIG.get().getGeneral().isDisplayTooltip()) {
accessor.resetTimer();
if (!(registrar.blockData.get(block).isEmpty() && registrar.blockData.get(blockEntity).isEmpty())) {
PacketSender.c2s().send(new BlockDataRequestPlayC2SPacket.Payload((BlockHitResult) accessor.getHitResult()));
PacketSender.c2s().send(new BlockDataRequestPlayC2SPacket.Payload(accessor.getBlockHitResult()));
}
}

Expand All @@ -47,7 +43,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);
Expand All @@ -71,12 +68,13 @@ public static void gatherEntity(Entity entity, DataAccessor accessor, Tooltip to
accessor.resetTimer();

if (!registrar.entityData.get(trueEntity).isEmpty()) {
PacketSender.c2s().send(new EntityDataRequestPlayC2SPacket.Payload(entity.getId(), accessor.getHitResult().getLocation()));
PacketSender.c2s().send(new EntityDataRequestPlayC2SPacket.Payload(entity.getId(), accessor.getEntityHitResult().getLocation()));
}
}

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);
Expand All @@ -97,45 +95,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<IBlockComponentProvider> 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;
Expand All @@ -146,7 +145,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;
}
Expand All @@ -159,28 +158,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;
}

}
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.onHandleTooltip(TOOLTIP, DataAccessor.INSTANCE, PluginConfig.CLIENT);
listener.value().onHandleTooltip(TOOLTIP, DataAccessor.INSTANCE, PluginConfig.CLIENT);
}
}

Expand Down Expand Up @@ -267,7 +267,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.onBeforeTooltipRender(ctx, rect, DataAccessor.INSTANCE, PluginConfig.CLIENT, canceller);
listener.value().onBeforeTooltipRender(ctx, rect, DataAccessor.INSTANCE, PluginConfig.CLIENT, canceller);
if (canceller.isCanceled()) {
ctx.pose().popPose();
RenderSystem.enableDepthTest();
Expand Down Expand Up @@ -304,7 +304,7 @@ private static void render0(Minecraft client, GuiGraphics ctx, float delta) {

if (state.fireEvent()) {
for (var listener : Registrar.INSTANCE.eventListeners.get(Object.class)) {
listener.onAfterTooltipRender(ctx, rect, DataAccessor.INSTANCE, PluginConfig.CLIENT);
listener.value().onAfterTooltipRender(ctx, rect, DataAccessor.INSTANCE, PluginConfig.CLIENT);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,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.INSTANCE.tryAppendData(provider, accessor);
DataWriter.INSTANCE.tryAppendData(provider.value(), accessor);
}

for (var provider : registrar.blockData.get(state.getBlock())) {
DataWriter.INSTANCE.tryAppendData(provider, accessor);
DataWriter.INSTANCE.tryAppendData(provider.value(), accessor);
}

raw.putInt("x", pos.getX());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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.INSTANCE.tryAppendData(provider, accessor);
DataWriter.INSTANCE.tryAppendData(provider.value(), accessor);
}

raw.putInt("WailaEntityID", entity.getId());
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/mcp/mobius/waila/registry/Register.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class Register<T> {

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

private boolean reversed = false;

Expand All @@ -26,7 +26,7 @@ public void add(Class<?> key, T value, int priority) {
.add(new Entry<>(value, priority));
}

public List<T> get(Object obj) {
public List<Entry<T>> get(Object obj) {
if (obj == null) {
return ObjectLists.emptyList();
}
Expand All @@ -53,9 +53,10 @@ public List<T> get(Object obj) {
}

entries.sort(comparator);
List<T> values = entries.isEmpty() ? ObjectLists.emptyList() : entries.stream().map(Entry::value).toList();
cache.put(clazz, values);
return values;

List<Entry<T>> result = entries.isEmpty() ? ObjectLists.emptyList() : entries;
cache.put(clazz, result);
return result;
}

public Map<Class<?>, Set<Entry<T>>> getMap() {
Expand Down

0 comments on commit d2b424d

Please sign in to comment.