diff --git a/gradle.properties b/gradle.properties index 7e8ef6b84..0552aa585 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ mod_file_name = minihud-fabric mod_version = 0.32.55 # Required malilib version -malilib_version = 3f02eb2ef6 +malilib_version = 1.21-0.21.6 # Minecraft, Fabric Loader and API and mappings versions minecraft_version_out = 1.21 diff --git a/src/main/java/fi/dy/masa/minihud/data/EntitiesDataManager.java b/src/main/java/fi/dy/masa/minihud/data/EntitiesDataManager.java index ee83b1765..8d53aab2b 100644 --- a/src/main/java/fi/dy/masa/minihud/data/EntitiesDataManager.java +++ b/src/main/java/fi/dy/masa/minihud/data/EntitiesDataManager.java @@ -4,7 +4,7 @@ import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; import com.google.gson.JsonObject; -import com.llamalad7.mixinextras.lib.apache.commons.tuple.Pair; +import org.apache.commons.lang3.tuple.Pair; import com.mojang.datafixers.util.Either; import net.minecraft.block.BlockEntityProvider; @@ -36,6 +36,7 @@ import net.minecraft.world.World; import fi.dy.masa.malilib.interfaces.IClientTickHandler; +import fi.dy.masa.malilib.interfaces.IDataSyncer; import fi.dy.masa.malilib.network.ClientPlayHandler; import fi.dy.masa.malilib.network.IPluginClientPlayHandler; import fi.dy.masa.malilib.util.Constants; @@ -54,7 +55,7 @@ import fi.dy.masa.minihud.util.EntityUtils; @SuppressWarnings("deprecation") -public class EntitiesDataManager implements IClientTickHandler +public class EntitiesDataManager implements IClientTickHandler, IDataSyncer { private static final EntitiesDataManager INSTANCE = new EntitiesDataManager(); @@ -83,12 +84,14 @@ public static EntitiesDataManager getInstance() private ClientWorld clientWorld; @Nullable + @Override public World getWorld() { return WorldUtils.getBestWorld(mc); } - private ClientWorld getClientWorld() + @Override + public ClientWorld getClientWorld() { if (this.clientWorld == null) { @@ -186,6 +189,7 @@ public IPluginClientPlayHandler getNetworkHandler( return HANDLER; } + @Override public void reset(boolean isLogout) { if (isLogout) @@ -257,6 +261,7 @@ private void tickCache() } } + @Override public @Nullable NbtCompound getFromBlockEntityCacheNbt(BlockPos pos) { if (this.blockEntityCache.containsKey(pos)) @@ -267,6 +272,7 @@ private void tickCache() return null; } + @Override public @Nullable BlockEntity getFromBlockEntityCache(BlockPos pos) { if (this.blockEntityCache.containsKey(pos)) @@ -277,6 +283,7 @@ private void tickCache() return null; } + @Override public @Nullable NbtCompound getFromEntityCacheNbt(int entityId) { if (this.entityCache.containsKey(entityId)) @@ -287,6 +294,7 @@ private void tickCache() return null; } + @Override public @Nullable Entity getFromEntityCache(int entityId) { if (this.entityCache.containsKey(entityId)) @@ -346,12 +354,14 @@ public int getEntityCacheCount() return this.entityCache.size(); } + @Override public void onGameInit() { ClientPlayHandler.getInstance().registerClientPlayHandler(HANDLER); HANDLER.registerPlayPayload(ServuxEntitiesPacket.Payload.ID, ServuxEntitiesPacket.Payload.CODEC, IPluginClientPlayHandler.BOTH_CLIENT); } + @Override public void onWorldPre() { if (DataStorage.getInstance().hasIntegratedServer() == false) @@ -360,6 +370,7 @@ public void onWorldPre() } } + @Override public void onWorldJoin() { // NO-OP @@ -406,6 +417,7 @@ public void onPacketFailure() this.hasInValidServux = true; } + @Override public @Nullable Pair requestBlockEntity(World world, BlockPos pos) { if (this.blockEntityCache.containsKey(pos)) @@ -438,7 +450,8 @@ else if (world.getBlockState(pos).getBlock() instanceof BlockEntityProvider) return null; } - public @Nullable Pair requestEntity(int entityId) + @Override + public @Nullable Pair requestEntity(World world, int entityId) { if (this.entityCache.containsKey(entityId)) { @@ -473,6 +486,7 @@ else if (world.getBlockState(pos).getBlock() instanceof BlockEntityProvider) } @Nullable + @Override public Inventory getBlockInventory(World world, BlockPos pos, boolean useNbt) { if (this.blockEntityCache.containsKey(pos)) @@ -545,7 +559,8 @@ public Inventory getBlockInventory(World world, BlockPos pos, boolean useNbt) } @Nullable - public Inventory getEntityInventory(int entityId, boolean useNbt) + @Override + public Inventory getEntityInventory(World world, int entityId, boolean useNbt) { if (this.entityCache.containsKey(entityId) && this.getWorld() != null) { @@ -589,7 +604,7 @@ else if (entity instanceof PiglinEntity) if (Configs.Generic.ENTITY_DATA_SYNC.getBooleanValue()) { - this.requestEntity(entityId); + this.requestEntity(this.getWorld(), entityId); } return null; @@ -650,6 +665,7 @@ private void requestServuxEntityData(int entityId) } @Nullable + @Override public BlockEntity handleBlockEntityData(BlockPos pos, NbtCompound nbt, @Nullable Identifier type) { this.pendingBlockEntitiesQueue.remove(pos); @@ -732,6 +748,7 @@ public BlockEntity handleBlockEntityData(BlockPos pos, NbtCompound nbt, @Nullabl } @Nullable + @Override public Entity handleEntityData(int entityId, NbtCompound nbt) { this.pendingEntitiesQueue.remove(entityId); @@ -770,11 +787,13 @@ public Entity handleEntityData(int entityId, NbtCompound nbt) return entity; } + @Override public void handleBulkEntityData(int transactionId, NbtCompound nbt) { // todo } + @Override public void handleVanillaQueryNbt(int transactionId, NbtCompound nbt) { Either either = this.transactionToBlockPosOrEntityId.remove(transactionId); diff --git a/src/main/java/fi/dy/masa/minihud/event/RenderHandler.java b/src/main/java/fi/dy/masa/minihud/event/RenderHandler.java index 127d6a86c..ee0fe7b36 100644 --- a/src/main/java/fi/dy/masa/minihud/event/RenderHandler.java +++ b/src/main/java/fi/dy/masa/minihud/event/RenderHandler.java @@ -5,7 +5,7 @@ import java.util.concurrent.CompletableFuture; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import com.llamalad7.mixinextras.lib.apache.commons.tuple.Pair; +import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Triple; import org.joml.Matrix4f; @@ -82,6 +82,7 @@ import fi.dy.masa.minihud.data.HudDataManager; import fi.dy.masa.minihud.data.MobCapDataHandler; import fi.dy.masa.minihud.mixin.*; +import fi.dy.masa.minihud.renderer.InventoryOverlayHandler; import fi.dy.masa.minihud.renderer.OverlayRenderer; import fi.dy.masa.minihud.util.DataStorage; import fi.dy.masa.minihud.util.IServerEntityManager; @@ -179,12 +180,16 @@ public void onRenderGameOverlayPost(DrawContext drawContext) if (Configs.Generic.INVENTORY_PREVIEW_ENABLED.getBooleanValue() && Configs.Generic.INVENTORY_PREVIEW.getKeybind().isKeybindHeld()) { + /* var inventory = RayTraceUtils.getTargetInventory(mc, true); if (inventory != null) { fi.dy.masa.minihud.renderer.RenderUtils.renderInventoryOverlay(inventory, drawContext); } + */ + + InventoryOverlayHandler.getInstance().getRenderContext(drawContext, mc.getProfiler(), mc); // OG method (Works with Crafters also) //fi.dy.masa.minihud.renderer.RenderUtils.renderInventoryOverlay(mc, drawContext); @@ -221,7 +226,7 @@ else if (stack.isOf(Items.ENDER_CHEST) && Configs.Generic.SHULKER_DISPLAY_ENDER_ if (player != null) { - Pair pair = EntitiesDataManager.getInstance().requestEntity(player.getId()); + Pair pair = EntitiesDataManager.getInstance().requestEntity(world, player.getId()); NbtCompound nbt = new NbtCompound(); EnderChestInventory inv; @@ -1713,7 +1718,7 @@ public Pair getTargetEntity(World world, MinecraftClient mc } else { - pair = EntitiesDataManager.getInstance().requestEntity(lookedEntity.getId()); + pair = EntitiesDataManager.getInstance().requestEntity(world, lookedEntity.getId()); } // Remember the last entity so the "refresh time" is smoothed over. diff --git a/src/main/java/fi/dy/masa/minihud/gui/InventoryOverlayScreen.java b/src/main/java/fi/dy/masa/minihud/gui/InventoryOverlayScreen.java deleted file mode 100644 index b87283460..000000000 --- a/src/main/java/fi/dy/masa/minihud/gui/InventoryOverlayScreen.java +++ /dev/null @@ -1,248 +0,0 @@ -package fi.dy.masa.minihud.gui; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import net.minecraft.block.ShulkerBoxBlock; -import net.minecraft.block.entity.CrafterBlockEntity; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.gui.Drawable; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.entity.EquipmentSlot; -import net.minecraft.entity.passive.AbstractHorseEntity; -import net.minecraft.entity.passive.VillagerEntity; -import net.minecraft.entity.passive.WolfEntity; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.inventory.EnderChestInventory; -import net.minecraft.inventory.Inventory; -import net.minecraft.inventory.SimpleInventory; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.item.tooltip.TooltipType; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.text.Text; -import net.minecraft.world.World; - -import fi.dy.masa.malilib.render.InventoryOverlay; -import fi.dy.masa.malilib.render.RenderUtils; -import fi.dy.masa.malilib.util.GuiUtils; -import fi.dy.masa.malilib.util.InventoryUtils; -import fi.dy.masa.malilib.util.WorldUtils; -import fi.dy.masa.malilib.util.game.BlockUtils; -import fi.dy.masa.malilib.util.game.wrap.GameWrap; -import fi.dy.masa.malilib.util.nbt.NbtBlockUtils; -import fi.dy.masa.malilib.util.nbt.NbtKeys; -import fi.dy.masa.minihud.MiniHUD; -import fi.dy.masa.minihud.config.Configs; -import fi.dy.masa.minihud.data.EntitiesDataManager; -import fi.dy.masa.minihud.event.RenderHandler; -import fi.dy.masa.minihud.util.RayTraceUtils; - -public class InventoryOverlayScreen extends Screen implements Drawable -{ - private InventoryOverlay.Context previewData; - private int ticks; - - public InventoryOverlayScreen(InventoryOverlay.Context previewData) - { - super(Text.literal("Inventory Overlay")); - this.previewData = previewData; - } - - @Override - public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) - { - ticks++; - MinecraftClient mc = MinecraftClient.getInstance(); - World world = WorldUtils.getBestWorld(mc); - - if (previewData != null && world != null) - { - final int xCenter = GuiUtils.getScaledWindowWidth() / 2; - final int yCenter = GuiUtils.getScaledWindowHeight() / 2; - int x = xCenter - 52 / 2; - int y = yCenter - 92; - - int startSlot = 0; - int totalSlots = previewData.inv() == null ? 0 : previewData.inv().size(); - List armourItems = new ArrayList<>(); - if (previewData.entity() instanceof AbstractHorseEntity) - { - if (previewData.inv() == null) - { - MiniHUD.logger.error("InventoryOverlayScreen(): Horse inv() = null"); - return; - } - armourItems.add(previewData.entity().getEquippedStack(EquipmentSlot.BODY)); - armourItems.add(previewData.inv().getStack(0)); - startSlot = 1; - totalSlots = previewData.inv().size() - 1; - } - else if (previewData.entity() instanceof WolfEntity) - { - armourItems.add(previewData.entity().getEquippedStack(EquipmentSlot.BODY)); - //armourItems.add(ItemStack.EMPTY); - } - - final InventoryOverlay.InventoryRenderType type = (previewData.entity() instanceof VillagerEntity) ? InventoryOverlay.InventoryRenderType.VILLAGER : InventoryOverlay.getBestInventoryType(previewData.inv(), previewData.nbt() != null ? previewData.nbt() : new NbtCompound(), previewData); - final InventoryOverlay.InventoryProperties props = InventoryOverlay.getInventoryPropsTemp(type, totalSlots); - final int rows = (int) Math.ceil((double) totalSlots / props.slotsPerRow); - Set lockedSlots = new HashSet<>(); - int xInv = xCenter - (props.width / 2); - int yInv = yCenter - props.height - 6; - - if (rows > 6) - { - yInv -= (rows - 6) * 18; - y -= (rows - 6) * 18; - } - - /* - MiniHUD.logger.warn("render():0: type [{}], previewData.type [{}], previewData.inv [{}], previewData.be [{}], previewData.ent [{}], previewData.nbt [{}]", type.toString(), previewData.type().toString(), - previewData.inv() != null, previewData.be() != null, previewData.entity() != null, previewData.nbt() != null ? previewData.nbt().getString("id") : null); - MiniHUD.logger.error("0: -> inv.type [{}] // nbt.type [{}]", previewData.inv() != null ? InventoryOverlay.getInventoryType(previewData.inv()) : null, previewData.nbt() != null ? InventoryOverlay.getInventoryType(previewData.nbt()) : null); - MiniHUD.logger.error("1: -> inv.size [{}] // inv.isEmpty [{}]", previewData.inv() != null ? previewData.inv().size() : -1, previewData.inv() != null ? previewData.inv().isEmpty() : -1); - */ - - if (previewData.entity() != null) - { - x = xCenter - 55; - xInv = xCenter + 2; - yInv = Math.min(yInv, yCenter - 92); - } - if (previewData.be() instanceof CrafterBlockEntity cbe) - { - lockedSlots = BlockUtils.getDisabledSlots(cbe); - } - else if (previewData.nbt() != null && previewData.nbt().contains(NbtKeys.DISABLED_SLOTS)) - { - lockedSlots = NbtBlockUtils.getDisabledSlotsFromNbt(previewData.nbt()); - } - - if (!armourItems.isEmpty()) - { - Inventory horseInv = new SimpleInventory(armourItems.toArray(new ItemStack[0])); - InventoryOverlay.renderInventoryBackground(type, xInv, yInv, 1, horseInv.size(), mc); - InventoryOverlay.renderInventoryBackgroundSlots(type, horseInv, xInv + props.slotOffsetX, yInv + props.slotOffsetY, drawContext); - InventoryOverlay.renderInventoryStacks(type, horseInv, xInv + props.slotOffsetX, yInv + props.slotOffsetY, 1, 0, horseInv.size(), mc, drawContext, mouseX, mouseY); - xInv += 32 + 4; - } - - if (previewData.be() != null && previewData.be().getCachedState().getBlock() instanceof ShulkerBoxBlock sbb) - { - RenderUtils.setShulkerboxBackgroundTintColor(sbb, Configs.Generic.SHULKER_DISPLAY_BACKGROUND_COLOR.getBooleanValue()); - } - - // Inv Display - if (totalSlots > 0 && previewData.inv() != null) - { - InventoryOverlay.renderInventoryBackground(type, xInv, yInv, props.slotsPerRow, totalSlots, mc); - - // TODO 1.21.4+ - /* - if (type == InventoryOverlay.InventoryRenderType.BREWING_STAND) - { - InventoryOverlay.renderBrewerBackgroundSlots(previewData.inv(), xInv, yInv, drawContext); - } - */ - - //dumpInvStacks(previewData.inv(), world); - InventoryOverlay.renderInventoryStacks(type, previewData.inv(), xInv + props.slotOffsetX, yInv + props.slotOffsetY, props.slotsPerRow, startSlot, totalSlots, lockedSlots, mc, drawContext, mouseX, mouseY); - } - - // EnderItems Display - if (previewData.type() == InventoryOverlay.InventoryRenderType.PLAYER && - previewData.nbt() != null && previewData.nbt().contains(NbtKeys.ENDER_ITEMS)) - { - EnderChestInventory enderItems = InventoryUtils.getPlayerEnderItemsFromNbt(previewData.nbt(), world.getRegistryManager()); - - if (enderItems == null) - { - enderItems = new EnderChestInventory(); - } - - //dumpInvStacks(enderItems, world); - yInv = yCenter + 6; - InventoryOverlay.renderInventoryBackground(InventoryOverlay.InventoryRenderType.GENERIC, xInv, yInv, 9, 27, mc); - InventoryOverlay.renderInventoryStacks(InventoryOverlay.InventoryRenderType.GENERIC, enderItems, xInv + props.slotOffsetX, yInv + props.slotOffsetY, 9, 0, 27, mc, drawContext, mouseX, mouseY); - } - else if (previewData.entity() instanceof PlayerEntity player) - { - yInv = yCenter + 6; - InventoryOverlay.renderInventoryBackground(InventoryOverlay.InventoryRenderType.GENERIC, xInv, yInv, 9, 27, mc); - InventoryOverlay.renderInventoryStacks(InventoryOverlay.InventoryRenderType.GENERIC, player.getEnderChestInventory(), xInv + props.slotOffsetX, yInv + props.slotOffsetY, 9, 0, 27, mc, drawContext, mouseX, mouseY); - } - - // Entity Display - if (previewData.entity() != null) - { - InventoryOverlay.renderEquipmentOverlayBackground(x, y, previewData.entity(), drawContext); - InventoryOverlay.renderEquipmentStacks(previewData.entity(), x, y, mc, drawContext, mouseX, mouseY); - } - - // Refresh - if (ticks % 4 == 0) - { - // Refresh data - if (previewData.be() != null) - { - RenderHandler.getInstance().requestBlockEntityAt(world, previewData.be().getPos()); - previewData = RayTraceUtils.getTargetInventoryFromBlock(previewData.be().getWorld(), previewData.be().getPos(), previewData.be(), previewData.nbt()); - } - else if (previewData.entity() != null) - { - EntitiesDataManager.getInstance().requestEntity(previewData.entity().getId()); - previewData = RayTraceUtils.getTargetInventoryFromEntity(previewData.entity(), previewData.nbt()); - } - } - } - } - - @Override - public boolean shouldPause() - { - return false; - } - - public static void dumpInvStacks(Inventory inv, World world, boolean showTooltip) - { - System.out.print("dumpInvStacks() -->\n"); - - if (inv == null || inv.size() <= 0 || world == null) - { - System.out.print("EMPTY / NULL\n"); - return; - } - - for (int i = 0; i < inv.size(); i++) - { - ItemStack stack = inv.getStack(i); - - if (stack.isEmpty()) - { - System.out.printf("slot[%d]: [%s]\n", i, "minecraft:air [EMPTY]"); - } - else - { - System.out.printf("slot[%d]: [%s]\n", i, inv.getStack(i).encode(world.getRegistryManager())); - - if (showTooltip) - { - List toolTips = inv.getStack(i).getTooltip(Item.TooltipContext.create(world), GameWrap.getClientPlayer(), TooltipType.ADVANCED); - int j = 0; - - for (Text entry : toolTips) - { - System.out.printf("Tooltip[%d]: [%s]\n", j, entry.getString()); - j++; - } - } - } - } - - System.out.print("END\n"); - } -} diff --git a/src/main/java/fi/dy/masa/minihud/hotkeys/KeyCallbacks.java b/src/main/java/fi/dy/masa/minihud/hotkeys/KeyCallbacks.java index 5188bde3f..582941446 100644 --- a/src/main/java/fi/dy/masa/minihud/hotkeys/KeyCallbacks.java +++ b/src/main/java/fi/dy/masa/minihud/hotkeys/KeyCallbacks.java @@ -4,7 +4,7 @@ import net.minecraft.entity.Entity; import fi.dy.masa.malilib.gui.GuiBase; import fi.dy.masa.malilib.hotkeys.*; -import fi.dy.masa.malilib.render.InventoryOverlay; +import fi.dy.masa.malilib.render.InventoryOverlayScreen; import fi.dy.masa.minihud.config.Configs; import fi.dy.masa.minihud.config.RendererCallbacks; import fi.dy.masa.minihud.config.RendererToggle; @@ -12,16 +12,11 @@ import fi.dy.masa.minihud.gui.GuiConfigs.ConfigGuiTab; import fi.dy.masa.minihud.gui.GuiShapeEditor; import fi.dy.masa.minihud.gui.GuiShapeManager; -import fi.dy.masa.minihud.gui.InventoryOverlayScreen; -import fi.dy.masa.minihud.renderer.OverlayRendererBeaconRange; -import fi.dy.masa.minihud.renderer.OverlayRendererLightLevel; -import fi.dy.masa.minihud.renderer.OverlayRendererSpawnChunks; -import fi.dy.masa.minihud.renderer.OverlayRendererStructures; +import fi.dy.masa.minihud.renderer.*; import fi.dy.masa.minihud.renderer.shapes.ShapeBase; import fi.dy.masa.minihud.renderer.shapes.ShapeManager; import fi.dy.masa.minihud.util.DataStorage; import fi.dy.masa.minihud.util.DebugInfoUtils; -import fi.dy.masa.minihud.util.RayTraceUtils; public class KeyCallbacks { @@ -144,12 +139,16 @@ else if (Configs.Generic.INVENTORY_PREVIEW_ENABLED.getBooleanValue() && Configs.Generic.INVENTORY_PREVIEW.getKeybind().isKeybindHeld()) { //RayTraceUtils.InventoryPreviewData inventory = RayTraceUtils.getTargetInventory(mc); + /* InventoryOverlay.Context inventory = RayTraceUtils.getTargetInventory(mc, false); if (inventory != null) { mc.setScreen(new InventoryOverlayScreen(inventory)); } + */ + + InventoryOverlayHandler.getInstance().refreshInventoryOverlay(mc, Configs.Generic.SHULKER_DISPLAY_BACKGROUND_COLOR.getBooleanValue()); } else { diff --git a/src/main/java/fi/dy/masa/minihud/renderer/InventoryOverlayHandler.java b/src/main/java/fi/dy/masa/minihud/renderer/InventoryOverlayHandler.java new file mode 100644 index 000000000..e5e8b79e0 --- /dev/null +++ b/src/main/java/fi/dy/masa/minihud/renderer/InventoryOverlayHandler.java @@ -0,0 +1,525 @@ +package fi.dy.masa.minihud.renderer; + +import org.apache.commons.lang3.tuple.Pair; +import org.jetbrains.annotations.Nullable; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockEntityProvider; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.block.entity.EnderChestBlockEntity; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.entity.Entity; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.mob.PiglinEntity; +import net.minecraft.entity.passive.AbstractHorseEntity; +import net.minecraft.entity.passive.VillagerEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.EnderChestInventory; +import net.minecraft.inventory.Inventory; +import net.minecraft.inventory.SimpleInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.registry.Registries; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.hit.EntityHitResult; +import net.minecraft.util.hit.HitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.profiler.Profiler; +import net.minecraft.world.World; + +import fi.dy.masa.malilib.interfaces.IDataSyncer; +import fi.dy.masa.malilib.interfaces.IInventoryOverlayHandler; +import fi.dy.masa.malilib.render.InventoryOverlay; +import fi.dy.masa.malilib.util.Constants; +import fi.dy.masa.malilib.util.EntityUtils; +import fi.dy.masa.malilib.util.InventoryUtils; +import fi.dy.masa.malilib.util.WorldUtils; +import fi.dy.masa.malilib.util.nbt.NbtBlockUtils; +import fi.dy.masa.malilib.util.nbt.NbtKeys; +import fi.dy.masa.minihud.Reference; +import fi.dy.masa.minihud.config.Configs; +import fi.dy.masa.minihud.data.EntitiesDataManager; +import fi.dy.masa.minihud.mixin.IMixinAbstractHorseEntity; +import fi.dy.masa.minihud.mixin.IMixinPiglinEntity; +import fi.dy.masa.minihud.util.RayTraceUtils; + +public class InventoryOverlayHandler implements IInventoryOverlayHandler +{ + private static final InventoryOverlayHandler INSTANCE = new InventoryOverlayHandler(); + public static InventoryOverlayHandler getInstance() { return INSTANCE; } + + private InventoryOverlay.Context context; + private Refresher refresher; + private IDataSyncer syncer; + + private Pair lastBlockEntityContext; + private Pair lastEntityContext; + + + public InventoryOverlayHandler() + { + this.lastBlockEntityContext = null; + this.lastEntityContext = null; + this.context = null; + this.refresher = null; + this.syncer = null; + } + + @Override + public String getModId() + { + return Reference.MOD_ID; + } + + @Override + public IDataSyncer getDataSyncer() + { + if (this.syncer == null) + { + this.syncer = EntitiesDataManager.getInstance(); + } + + return this.syncer; + } + + @Override + public void setDataSyncer(IDataSyncer syncer) + { + this.syncer = syncer; + } + + @Override + public InventoryOverlay.Refresher getRefreshHandler() + { + if (this.refresher == null) + { + this.refresher = new Refresher(); + } + + return this.refresher; + } + + @Override + public boolean isEmpty() + { + return this.context == null; + } + + @Override + public @Nullable InventoryOverlay.Context getRenderContextNullable() + { + return this.context; + } + + @Override + public @Nullable InventoryOverlay.Context getRenderContext(DrawContext drawContext, Profiler profiler, MinecraftClient mc) + { + profiler.push(this.getClass().getName() + "_inventory_overlay"); + this.getTargetInventory(mc); + + if (!this.isEmpty()) + { + this.renderInventoryOverlay(this.getRenderContextNullable(), drawContext, mc, Configs.Generic.SHULKER_DISPLAY_BACKGROUND_COLOR.getBooleanValue()); + } + + profiler.pop(); + + return this.getRenderContextNullable(); + } + + @Override + public @Nullable InventoryOverlay.Context getTargetInventory(MinecraftClient mc) + { + World world = WorldUtils.getBestWorld(mc); + Entity cameraEntity = EntityUtils.getCameraEntity(); + this.context = null; + + if (mc.player == null || world == null) + { + return null; + } + + if (cameraEntity == mc.player && world instanceof ServerWorld) + { + // We need to get the player from the server world (if available, ie. in single player), + // so that the player itself won't be included in the ray trace + Entity serverPlayer = world.getPlayerByUuid(mc.player.getUuid()); + + if (serverPlayer != null) + { + cameraEntity = serverPlayer; + } + } + + HitResult trace = RayTraceUtils.getRayTraceFromEntity(world, cameraEntity, false); + NbtCompound nbt = new NbtCompound(); + + if (trace == null || trace.getType() == HitResult.Type.MISS) + { + return null; + } + + if (trace.getType() == HitResult.Type.BLOCK) + { + BlockPos pos = ((BlockHitResult) trace).getBlockPos(); + BlockState state = world.getBlockState(pos); + Block blockTmp = state.getBlock(); + BlockEntity be = null; + + // Keep screen from getting 'stuck' if trying to use toggle on a lectern + /* + if (blockTmp instanceof LecternBlock && !newScreen) + { + return null; + } + */ + + //MiniHUD.logger.warn("getTarget():1: pos [{}], state [{}]", pos.toShortString(), state.toString()); + + if (blockTmp instanceof BlockEntityProvider) + { + if (world instanceof ServerWorld) + { + be = world.getWorldChunk(pos).getBlockEntity(pos); + + if (be != null) + { + nbt = be.createNbtWithIdentifyingData(world.getRegistryManager()); + } + } + else + { + Pair pair = this.requestBlockEntityAt(world, pos); + + if (pair != null) + { + nbt = pair.getRight(); + + if (Configs.Generic.ENTITY_DATA_LOAD_NBT.getBooleanValue()) + { + be = pair.getLeft(); + } + } + } + + //MiniHUD.logger.warn("getTarget():2: pos [{}], be [{}], nbt [{}]", pos.toShortString(), be != null, nbt != null); + InventoryOverlay.Context ctx = getTargetInventoryFromBlock(world, pos, be, nbt); + //dumpContext(ctx); + + if (this.lastBlockEntityContext != null && !this.lastBlockEntityContext.getLeft().equals(pos)) + { + this.lastBlockEntityContext = null; + } + + if (ctx != null && ctx.inv() != null) + { + this.lastBlockEntityContext = Pair.of(pos, ctx); + this.context = ctx; + return ctx; + } + else if (this.lastBlockEntityContext != null && this.lastBlockEntityContext.getLeft().equals(pos)) + { + this.context = this.lastBlockEntityContext.getRight(); + return this.context; + } + } + + return null; + } + else if (trace.getType() == HitResult.Type.ENTITY) + { + Entity entity = ((EntityHitResult) trace).getEntity(); + + if (world instanceof ServerWorld) + { + entity.saveSelfNbt(nbt); + } + else + { + Pair pair = EntitiesDataManager.getInstance().requestEntity(world, entity.getId()); + + if (pair != null) + { + nbt = pair.getRight(); + + if (Configs.Generic.ENTITY_DATA_LOAD_NBT.getBooleanValue()) + { + entity = pair.getLeft(); + } + } + } + + //MiniHUD.logger.error("getTarget(): Entity [{}] raw NBT [{}]", entity.getId(), nbt.toString()); + InventoryOverlay.Context ctx = getTargetInventoryFromEntity(world.getEntityById(entity.getId()), nbt); + //dumpContext(ctx); + + if (this.lastEntityContext != null && !this.lastEntityContext.getLeft().equals(entity.getId())) + { + this.lastEntityContext = null; + } + + if (ctx != null && ctx.inv() != null) + { + this.lastEntityContext = Pair.of(entity.getId(), ctx); + this.context = ctx; + return ctx; + } + // Non-Inventory/Empty Entity + else if (ctx != null && + (ctx.type() == InventoryOverlay.InventoryRenderType.WOLF || + ctx.type() == InventoryOverlay.InventoryRenderType.VILLAGER || + ctx.type() == InventoryOverlay.InventoryRenderType.HORSE || + ctx.type() == InventoryOverlay.InventoryRenderType.PLAYER || + ctx.type() == InventoryOverlay.InventoryRenderType.ARMOR_STAND || + ctx.type() == InventoryOverlay.InventoryRenderType.LIVING_ENTITY)) + { + this.lastEntityContext = Pair.of(entity.getId(), ctx); + this.context = ctx; + return ctx; + } + else if (this.lastEntityContext != null && this.lastEntityContext.getLeft().equals(entity.getId())) + { + this.context = this.lastEntityContext.getRight(); + return this.context; + } + } + + return null; + } + + @Override + public @Nullable InventoryOverlay.Context getTargetInventoryFromBlock(World world, BlockPos pos, @Nullable BlockEntity be, NbtCompound nbt) + { + Inventory inv; + + if (world == null) return null; + + if (be != null) + { + if (nbt.isEmpty()) + { + nbt = be.createNbtWithIdentifyingData(world.getRegistryManager()); + } + + inv = InventoryUtils.getInventory(world, pos); + } + else + { + if (nbt.isEmpty()) + { + Pair pair = this.requestBlockEntityAt(world, pos); + + if (pair != null) + { + nbt = pair.getRight(); + + if (Configs.Generic.ENTITY_DATA_LOAD_NBT.getBooleanValue()) + { + be = pair.getLeft(); + } + } + } + + inv = EntitiesDataManager.getInstance().getBlockInventory(world, pos, false); + } + + BlockEntityType beType = nbt != null ? NbtBlockUtils.getBlockEntityTypeFromNbt(nbt) : null; + + if ((beType != null && beType.equals(BlockEntityType.ENDER_CHEST)) || + be instanceof EnderChestBlockEntity) + { + if (MinecraftClient.getInstance().player != null) + { + PlayerEntity player = world.getPlayerByUuid(MinecraftClient.getInstance().player.getUuid()); + + if (player != null) + { + // Fetch your own EnderItems from Server ... + Pair enderPair = EntitiesDataManager.getInstance().requestEntity(world, player.getId()); + EnderChestInventory enderItems; + + if (enderPair != null && enderPair.getRight() != null && enderPair.getRight().contains(NbtKeys.ENDER_ITEMS)) + { + enderItems = InventoryUtils.getPlayerEnderItemsFromNbt(enderPair.getRight(), world.getRegistryManager()); + } + else + { + enderItems = player.getEnderChestInventory(); + } + + if (enderItems != null) + { + inv = enderItems; + } + } + } + } + + if (nbt != null && !nbt.isEmpty()) + { + //MiniHUD.logger.warn("getTargetInventoryFromBlock(): rawNbt: [{}]", nbt.toString()); + + Inventory inv2 = InventoryUtils.getNbtInventory(nbt, inv != null ? inv.size() : -1, world.getRegistryManager()); + + if (inv == null) + { + inv = inv2; + } + } + + //MiniHUD.logger.warn("getTarget():3: pos [{}], inv [{}], be [{}], nbt [{}]", pos.toShortString(), inv != null, be != null, nbt != null ? nbt.getString("id") : new NbtCompound()); + + if (inv == null || nbt == null) + { + return null; + } + + this.context = new InventoryOverlay.Context(InventoryOverlay.getBestInventoryType(inv, nbt), inv, be != null ? be : world.getBlockEntity(pos), null, nbt, this.getRefreshHandler()); + + return this.context; + } + + @Override + public @Nullable InventoryOverlay.Context getTargetInventoryFromEntity(Entity entity, NbtCompound nbt) + { + Inventory inv = null; + LivingEntity entityLivingBase = null; + + if (entity instanceof LivingEntity) + { + entityLivingBase = (LivingEntity) entity; + } + + if (entity instanceof Inventory) + { + inv = (Inventory) entity; + } + else if (entity instanceof PlayerEntity player) + { + inv = new SimpleInventory(player.getInventory().main.toArray(new ItemStack[36])); + } + else if (entity instanceof VillagerEntity) + { + inv = ((VillagerEntity) entity).getInventory(); + } + else if (entity instanceof AbstractHorseEntity) + { + inv = ((IMixinAbstractHorseEntity) entity).minihud_getHorseInventory(); + } + else if (entity instanceof PiglinEntity) + { + inv = ((IMixinPiglinEntity) entity).minihud_inventory(); + } + if (!nbt.isEmpty()) + { + Inventory inv2; + + //MiniHUD.logger.warn("getTargetInventoryFromEntity(): rawNbt: [{}]", nbt.toString()); + + // Fix for empty horse inv + if (inv != null && + nbt.contains(NbtKeys.ITEMS) && + nbt.getList(NbtKeys.ITEMS, Constants.NBT.TAG_COMPOUND).size() > 1) + { + if (entity instanceof AbstractHorseEntity) + { + inv2 = InventoryUtils.getNbtInventoryHorseFix(nbt, -1, entity.getRegistryManager()); + } + else + { + inv2 = InventoryUtils.getNbtInventory(nbt, -1, entity.getRegistryManager()); + } + + inv = null; + } + // Fix for saddled horse, no inv + else if (inv != null && + inv.size() == 1 && + nbt.contains(NbtKeys.SADDLE)) + { + inv2 = InventoryUtils.getNbtInventoryHorseFix(nbt, -1, entity.getRegistryManager()); + inv = null; + } + // Fix for empty Villager/Piglin inv + else if (inv != null && inv.size() == 8 && + nbt.contains(NbtKeys.INVENTORY) && + !nbt.getList(NbtKeys.INVENTORY, Constants.NBT.TAG_COMPOUND).isEmpty()) + { + inv2 = InventoryUtils.getNbtInventory(nbt, 8, entity.getRegistryManager()); + inv = null; + } + else + { + inv2 = InventoryUtils.getNbtInventory(nbt, inv != null ? inv.size() : -1, entity.getRegistryManager()); + + if (inv2 != null) + { + inv = null; + } + } + + //MiniHUD.logger.error("getTargetInventoryFromEntity(): inv.size [{}], inv2.size [{}]", inv != null ? inv.size() : "null", inv2 != null ? inv2.size() : "null"); + + if (inv2 != null) + { + inv = inv2; + } + } + + if (inv == null && entityLivingBase == null) + { + return null; + } + + this.context = new InventoryOverlay.Context(inv != null ? InventoryOverlay.getBestInventoryType(inv, nbt) : InventoryOverlay.getInventoryType(nbt), + inv, null, entityLivingBase, nbt, this.getRefreshHandler()); + + return this.context; + } + + private static void dumpContext(InventoryOverlay.Context ctx) + { + System.out.print("Context Dump --> "); + + if (ctx == null) + { + System.out.print("NULL!\n"); + return; + } + + System.out.printf("\nTYPE: [%s]\n", ctx.type().name()); + System.out.printf("BE : [%s]\n", ctx.be() != null ? Registries.BLOCK_ENTITY_TYPE.getId(ctx.be().getType()) : ""); + System.out.printf("ENT : [%s]\n", ctx.entity() != null ? Registries.ENTITY_TYPE.getId(ctx.entity().getType()) : ""); + System.out.printf("INV : [%s]\n", ctx.inv() != null ? "size: "+ctx.inv().size()+"/ empty: "+ctx.inv().isEmpty() : ""); + System.out.printf("NBT : [%s]\n", ctx.nbt() != null ? ctx.nbt().toString() : ""); + + System.out.print("--> EOF\n"); + } + + public static class Refresher implements InventoryOverlay.Refresher + { + public Refresher() {} + + @Override + public InventoryOverlay.Context onContextRefresh(InventoryOverlay.Context data, World world) + { + // Refresh data + if (data.be() != null) + { + InventoryOverlayHandler.getInstance().requestBlockEntityAt(world, data.be().getPos()); + data = InventoryOverlayHandler.getInstance().getTargetInventoryFromBlock(data.be().getWorld(), data.be().getPos(), data.be(), data.nbt()); + } + else if (data.entity() != null) + { + InventoryOverlayHandler.getInstance().getDataSyncer().requestEntity(world, data.entity().getId()); + data = InventoryOverlayHandler.getInstance().getTargetInventoryFromEntity(data.entity(), data.nbt()); + } + + return data; + } + } +} diff --git a/src/main/java/fi/dy/masa/minihud/renderer/OverlayRendererVillagerInfo.java b/src/main/java/fi/dy/masa/minihud/renderer/OverlayRendererVillagerInfo.java index 154b56519..2579ac5e4 100644 --- a/src/main/java/fi/dy/masa/minihud/renderer/OverlayRendererVillagerInfo.java +++ b/src/main/java/fi/dy/masa/minihud/renderer/OverlayRendererVillagerInfo.java @@ -101,7 +101,7 @@ public void update(Vec3d cameraPos, Entity entity, MinecraftClient mc) { if (librarian.isClient()) { - EntitiesDataManager.getInstance().requestEntity(librarian.getId()); + EntitiesDataManager.getInstance().requestEntity(world, librarian.getId()); } List overlay = new ArrayList<>(); TradeOfferList offers = ((IMixinMerchantEntity) librarian).minihud_offers(); @@ -186,7 +186,7 @@ else if (Configs.Generic.VILLAGER_OFFER_HIGHEST_LEVEL_ONLY.getBooleanValue()) { if (villager.getWorld().isClient) { - EntitiesDataManager.getInstance().requestEntity(villager.getId()); + EntitiesDataManager.getInstance().requestEntity(world, villager.getId()); } int conversionTimer = ((IMixinZombieVillagerEntity) villager).minihud_conversionTimer(); diff --git a/src/main/java/fi/dy/masa/minihud/renderer/RenderUtils.java b/src/main/java/fi/dy/masa/minihud/renderer/RenderUtils.java index 212cad47e..e6f2de4ce 100644 --- a/src/main/java/fi/dy/masa/minihud/renderer/RenderUtils.java +++ b/src/main/java/fi/dy/masa/minihud/renderer/RenderUtils.java @@ -34,7 +34,6 @@ import fi.dy.masa.malilib.util.game.BlockUtils; import fi.dy.masa.minihud.config.Configs; import fi.dy.masa.minihud.data.EntitiesDataManager; -import fi.dy.masa.minihud.gui.InventoryOverlayScreen; import fi.dy.masa.minihud.mixin.IMixinAbstractHorseEntity; import fi.dy.masa.minihud.renderer.shapes.SideQuad; import fi.dy.masa.minihud.util.RayTraceUtils; @@ -513,12 +512,14 @@ else if (value == 0.0) } } + /* public static void renderInventoryOverlay(InventoryOverlay.Context inventory, DrawContext drawContext) { var screen = new InventoryOverlayScreen(inventory); screen.init(MinecraftClient.getInstance(), 0, 0); screen.render(drawContext, 0, 0, 0); } + */ // OG Method (Works) public static void renderInventoryOverlay(MinecraftClient mc, DrawContext drawContext) @@ -574,7 +575,7 @@ else if (trace.getType() == HitResult.Type.ENTITY) if (entity.getWorld().isClient && Configs.Generic.ENTITY_DATA_SYNC.getBooleanValue()) { - EntitiesDataManager.getInstance().requestEntity(entity.getId()); + EntitiesDataManager.getInstance().requestEntity(world, entity.getId()); } if (entity instanceof LivingEntity) diff --git a/src/main/java/fi/dy/masa/minihud/util/RayTraceUtils.java b/src/main/java/fi/dy/masa/minihud/util/RayTraceUtils.java index 746bb3563..cc1992eea 100644 --- a/src/main/java/fi/dy/masa/minihud/util/RayTraceUtils.java +++ b/src/main/java/fi/dy/masa/minihud/util/RayTraceUtils.java @@ -3,30 +3,9 @@ import java.util.List; import java.util.Optional; import javax.annotation.Nonnull; -import com.llamalad7.mixinextras.lib.apache.commons.tuple.Pair; -import org.jetbrains.annotations.Nullable; -import net.minecraft.block.Block; -import net.minecraft.block.BlockEntityProvider; -import net.minecraft.block.BlockState; -import net.minecraft.block.LecternBlock; -import net.minecraft.block.entity.BlockEntity; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.EnderChestBlockEntity; -import net.minecraft.client.MinecraftClient; import net.minecraft.entity.Entity; -import net.minecraft.entity.LivingEntity; -import net.minecraft.entity.mob.PiglinEntity; -import net.minecraft.entity.passive.AbstractHorseEntity; -import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.inventory.EnderChestInventory; -import net.minecraft.inventory.Inventory; -import net.minecraft.inventory.SimpleInventory; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.registry.Registries; -import net.minecraft.server.world.ServerWorld; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.hit.EntityHitResult; import net.minecraft.util.hit.HitResult; @@ -37,24 +16,8 @@ import net.minecraft.world.RaycastContext; import net.minecraft.world.World; -import fi.dy.masa.malilib.render.InventoryOverlay; -import fi.dy.masa.malilib.util.Constants; -import fi.dy.masa.malilib.util.EntityUtils; -import fi.dy.masa.malilib.util.InventoryUtils; -import fi.dy.masa.malilib.util.WorldUtils; -import fi.dy.masa.malilib.util.nbt.NbtBlockUtils; -import fi.dy.masa.malilib.util.nbt.NbtKeys; -import fi.dy.masa.minihud.config.Configs; -import fi.dy.masa.minihud.data.EntitiesDataManager; -import fi.dy.masa.minihud.event.RenderHandler; -import fi.dy.masa.minihud.mixin.IMixinAbstractHorseEntity; -import fi.dy.masa.minihud.mixin.IMixinPiglinEntity; - public class RayTraceUtils { - private static Pair lastBlockEntityContext = null; - private static Pair lastEntityContext = null; - @Nonnull public static HitResult getRayTraceFromEntity(World worldIn, Entity entityIn, boolean useLiquids) { @@ -111,350 +74,4 @@ public static HitResult getRayTraceFromEntity(World worldIn, Entity entityIn, bo return result; } - - public static @Nullable InventoryOverlay.Context getTargetInventory(MinecraftClient mc, boolean newScreen) - { - World world = WorldUtils.getBestWorld(mc); - Entity cameraEntity = EntityUtils.getCameraEntity(); - - if (mc.player == null || world == null) - { - return null; - } - - if (cameraEntity == mc.player && world instanceof ServerWorld) - { - // We need to get the player from the server world (if available, ie. in single player), - // so that the player itself won't be included in the ray trace - Entity serverPlayer = world.getPlayerByUuid(mc.player.getUuid()); - - if (serverPlayer != null) - { - cameraEntity = serverPlayer; - } - } - - HitResult trace = getRayTraceFromEntity(world, cameraEntity, false); - NbtCompound nbt = new NbtCompound(); - - if (trace.getType() == HitResult.Type.BLOCK) - { - BlockPos pos = ((BlockHitResult) trace).getBlockPos(); - BlockState state = world.getBlockState(pos); - Block blockTmp = state.getBlock(); - BlockEntity be = null; - - // Keep screen from getting 'stuck' if trying to use toggle on a lectern - if (blockTmp instanceof LecternBlock && !newScreen) - { - return null; - } - - //MiniHUD.logger.warn("getTarget():1: pos [{}], state [{}]", pos.toShortString(), state.toString()); - - if (blockTmp instanceof BlockEntityProvider) - { - if (world instanceof ServerWorld) - { - be = world.getWorldChunk(pos).getBlockEntity(pos); - - if (be != null) - { - nbt = be.createNbtWithIdentifyingData(world.getRegistryManager()); - } - } - else - { - Pair pair = RenderHandler.getInstance().requestBlockEntityAt(world, pos); - - if (pair != null) - { - nbt = pair.getRight(); - - if (Configs.Generic.ENTITY_DATA_LOAD_NBT.getBooleanValue()) - { - be = pair.getLeft(); - } - } - } - - //MiniHUD.logger.warn("getTarget():2: pos [{}], be [{}], nbt [{}]", pos.toShortString(), be != null, nbt != null); - InventoryOverlay.Context ctx = getTargetInventoryFromBlock(world, pos, be, nbt); - //dumpContext(ctx); - - if (lastBlockEntityContext != null && !lastBlockEntityContext.getLeft().equals(pos)) - { - lastBlockEntityContext = null; - } - - if (ctx != null && ctx.inv() != null) - { - lastBlockEntityContext = Pair.of(pos, ctx); - return ctx; - } - else if (lastBlockEntityContext != null && lastBlockEntityContext.getLeft().equals(pos)) - { - return lastBlockEntityContext.getRight(); - } - } - - return null; - } - else if (trace.getType() == HitResult.Type.ENTITY) - { - Entity entity = ((EntityHitResult) trace).getEntity(); - - if (world instanceof ServerWorld) - { - entity.saveSelfNbt(nbt); - } - else - { - Pair pair = EntitiesDataManager.getInstance().requestEntity(entity.getId()); - - if (pair != null) - { - nbt = pair.getRight(); - - if (Configs.Generic.ENTITY_DATA_LOAD_NBT.getBooleanValue()) - { - entity = pair.getLeft(); - } - } - } - - //MiniHUD.logger.error("getTarget(): Entity [{}] raw NBT [{}]", entity.getId(), nbt.toString()); - InventoryOverlay.Context ctx = getTargetInventoryFromEntity(world.getEntityById(entity.getId()), nbt); - //dumpContext(ctx); - - if (lastEntityContext != null && !lastEntityContext.getLeft().equals(entity.getId())) - { - lastEntityContext = null; - } - - if (ctx != null && ctx.inv() != null) - { - lastEntityContext = Pair.of(entity.getId(), ctx); - return ctx; - } - // Non-Inventory/Empty Entity - else if (ctx != null && - (ctx.type() == InventoryOverlay.InventoryRenderType.WOLF || - ctx.type() == InventoryOverlay.InventoryRenderType.VILLAGER || - ctx.type() == InventoryOverlay.InventoryRenderType.HORSE || - ctx.type() == InventoryOverlay.InventoryRenderType.PLAYER || - ctx.type() == InventoryOverlay.InventoryRenderType.ARMOR_STAND || - ctx.type() == InventoryOverlay.InventoryRenderType.LIVING_ENTITY)) - { - lastEntityContext = Pair.of(entity.getId(), ctx); - return ctx; - } - else if (lastEntityContext != null && lastEntityContext.getLeft().equals(entity.getId())) - { - return lastEntityContext.getRight(); - } - } - - return null; - } - - private static void dumpContext(InventoryOverlay.Context ctx) - { - System.out.print("Context Dump --> "); - - if (ctx == null) - { - System.out.print("NULL!\n"); - return; - } - - System.out.printf("\nTYPE: [%s]\n", ctx.type().name()); - System.out.printf("BE : [%s]\n", ctx.be() != null ? Registries.BLOCK_ENTITY_TYPE.getId(ctx.be().getType()) : ""); - System.out.printf("ENT : [%s]\n", ctx.entity() != null ? Registries.ENTITY_TYPE.getId(ctx.entity().getType()) : ""); - System.out.printf("INV : [%s]\n", ctx.inv() != null ? "size: "+ctx.inv().size()+"/ empty: "+ctx.inv().isEmpty() : ""); - System.out.printf("NBT : [%s]\n", ctx.nbt() != null ? ctx.nbt().toString() : ""); - - System.out.print("--> EOF\n"); - } - - public static @Nullable InventoryOverlay.Context getTargetInventoryFromBlock(World world, BlockPos pos, @Nullable BlockEntity be, NbtCompound nbt) - { - Inventory inv; - - if (be != null) - { - if (nbt.isEmpty()) - { - nbt = be.createNbtWithIdentifyingData(world.getRegistryManager()); - } - - inv = InventoryUtils.getInventory(world, pos); - } - else - { - if (nbt.isEmpty()) - { - Pair pair = RenderHandler.getInstance().requestBlockEntityAt(world, pos); - - if (pair != null) - { - nbt = pair.getRight(); - - if (Configs.Generic.ENTITY_DATA_LOAD_NBT.getBooleanValue()) - { - be = pair.getLeft(); - } - } - } - - inv = EntitiesDataManager.getInstance().getBlockInventory(world, pos, false); - } - - BlockEntityType beType = nbt != null ? NbtBlockUtils.getBlockEntityTypeFromNbt(nbt) : null; - - if ((beType != null && beType.equals(BlockEntityType.ENDER_CHEST)) || - be instanceof EnderChestBlockEntity) - { - if (MinecraftClient.getInstance().player != null) - { - PlayerEntity player = world.getPlayerByUuid(MinecraftClient.getInstance().player.getUuid()); - - if (player != null) - { - // Fetch your own EnderItems from Server ... - Pair enderPair = EntitiesDataManager.getInstance().requestEntity(player.getId()); - EnderChestInventory enderItems; - - if (enderPair != null && enderPair.getRight() != null && enderPair.getRight().contains(NbtKeys.ENDER_ITEMS)) - { - enderItems = InventoryUtils.getPlayerEnderItemsFromNbt(enderPair.getRight(), world.getRegistryManager()); - } - else - { - enderItems = player.getEnderChestInventory(); - } - - if (enderItems != null) - { - inv = enderItems; - } - } - } - } - - if (nbt != null && !nbt.isEmpty()) - { - //MiniHUD.logger.warn("getTargetInventoryFromBlock(): rawNbt: [{}]", nbt.toString()); - - Inventory inv2 = InventoryUtils.getNbtInventory(nbt, inv != null ? inv.size() : -1, world.getRegistryManager()); - - if (inv == null) - { - inv = inv2; - } - } - - //MiniHUD.logger.warn("getTarget():3: pos [{}], inv [{}], be [{}], nbt [{}]", pos.toShortString(), inv != null, be != null, nbt != null ? nbt.getString("id") : new NbtCompound()); - - if (inv == null || nbt == null) - { - return null; - } - - return new InventoryOverlay.Context(InventoryOverlay.getBestInventoryType(inv, nbt), inv, be != null ? be : world.getBlockEntity(pos), null, nbt); - } - - public static @Nullable InventoryOverlay.Context getTargetInventoryFromEntity(Entity entity, NbtCompound nbt) - { - Inventory inv = null; - LivingEntity entityLivingBase = null; - - if (entity instanceof LivingEntity) - { - entityLivingBase = (LivingEntity) entity; - } - - if (entity instanceof Inventory) - { - inv = (Inventory) entity; - } - else if (entity instanceof PlayerEntity player) - { - inv = new SimpleInventory(player.getInventory().main.toArray(new ItemStack[36])); - } - else if (entity instanceof VillagerEntity) - { - inv = ((VillagerEntity) entity).getInventory(); - } - else if (entity instanceof AbstractHorseEntity) - { - inv = ((IMixinAbstractHorseEntity) entity).minihud_getHorseInventory(); - } - else if (entity instanceof PiglinEntity) - { - inv = ((IMixinPiglinEntity) entity).minihud_inventory(); - } - if (!nbt.isEmpty()) - { - Inventory inv2; - - //MiniHUD.logger.warn("getTargetInventoryFromEntity(): rawNbt: [{}]", nbt.toString()); - - // Fix for empty horse inv - if (inv != null && - nbt.contains(NbtKeys.ITEMS) && - nbt.getList(NbtKeys.ITEMS, Constants.NBT.TAG_COMPOUND).size() > 1) - { - if (entity instanceof AbstractHorseEntity) - { - inv2 = InventoryUtils.getNbtInventoryHorseFix(nbt, -1, entity.getRegistryManager()); - } - else - { - inv2 = InventoryUtils.getNbtInventory(nbt, -1, entity.getRegistryManager()); - } - inv = null; - } - // Fix for saddled horse, no inv - else if (inv != null && - inv.size() == 1 && - nbt.contains(NbtKeys.SADDLE)) - { - inv2 = InventoryUtils.getNbtInventoryHorseFix(nbt, -1, entity.getRegistryManager()); - inv = null; - } - // Fix for empty Villager/Piglin inv - else if (inv != null && inv.size() == 8 && - nbt.contains(NbtKeys.INVENTORY) && - !nbt.getList(NbtKeys.INVENTORY, Constants.NBT.TAG_COMPOUND).isEmpty()) - { - inv2 = InventoryUtils.getNbtInventory(nbt, 8, entity.getRegistryManager()); - inv = null; - } - else - { - inv2 = InventoryUtils.getNbtInventory(nbt, inv != null ? inv.size() : -1, entity.getRegistryManager()); - - if (inv2 != null) - { - inv = null; - } - } - - //MiniHUD.logger.error("getTargetInventoryFromEntity(): inv.size [{}], inv2.size [{}]", inv != null ? inv.size() : "null", inv2 != null ? inv2.size() : "null"); - - if (inv2 != null) - { - inv = inv2; - } - } - - if (inv == null && entityLivingBase == null) - { - return null; - } - - return new InventoryOverlay.Context(inv != null ? InventoryOverlay.getBestInventoryType(inv, nbt) : InventoryOverlay.getInventoryType(nbt), - inv, null, entityLivingBase, nbt); - } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index aa1abbc72..a3ea5fe54 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -35,6 +35,6 @@ "depends": { "minecraft": ">=1.21 <=1.21.1", - "malilib": ">=0.21.5" + "malilib": ">=0.21.6" } }