Skip to content

ContainerPreview: EChest caching, nested locking, item frames #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/main/java/com/lambda/mixin/gui/MixinGuiChest.java

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/java/com/lambda/mixin/gui/MixinGuiScreen.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lambda.mixin.gui;

import com.lambda.client.module.modules.render.ContainerPreview;
import com.lambda.client.module.modules.render.MapPreview;
import com.lambda.client.module.modules.render.NoRender;
import com.lambda.client.util.Wrapper;
@@ -25,8 +24,6 @@ public void renderToolTip(ItemStack stack, int x, int y, CallbackInfo ci) {
ci.cancel();
MapPreview.drawMap(stack, mapData, x, y);
}
} else if (ContainerPreview.INSTANCE.isEnabled()) {
ContainerPreview.INSTANCE.renderTooltips(stack, x, y, ci);
}
}

Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@

import com.lambda.client.event.LambdaEventBus;
import com.lambda.client.event.events.ChunkDataEvent;
import com.lambda.client.manager.managers.CachedContainerManager;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.network.play.server.SPacketChunkData;
import net.minecraft.network.play.server.SPacketWindowItems;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
@@ -20,4 +22,11 @@ public class MixinNetHandlerPlayClient {
public void handleChunkData(SPacketChunkData packetIn, CallbackInfo ci) {
LambdaEventBus.INSTANCE.post(new ChunkDataEvent(packetIn.isFullChunk(), this.world.getChunk(packetIn.getChunkX(), packetIn.getChunkZ())));
}

@Inject(method = "handleWindowItems", at = @At(value = "RETURN"))
public void handleItems(SPacketWindowItems packetIn, CallbackInfo ci) {
if (packetIn.getWindowId() != 0) {
CachedContainerManager.updateContainerInventory(packetIn.getWindowId());
}
}
}
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import com.lambda.client.event.events.PlayerMoveEvent;
import com.lambda.client.event.events.PushOutOfBlocksEvent;
import com.lambda.client.gui.mc.LambdaGuiBeacon;
import com.lambda.client.manager.managers.CachedContainerManager;
import com.lambda.client.manager.managers.MessageManager;
import com.lambda.client.manager.managers.PlayerPacketManager;
import com.lambda.client.module.modules.chat.PortalChat;
@@ -18,6 +19,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.entity.MoverType;
import net.minecraft.entity.player.EntityPlayer;
@@ -68,6 +70,13 @@ public MixinEntityPlayerSP(World worldIn, GameProfile gameProfileIn) {
@Shadow
protected abstract void updateAutoJump(float p_189810_1_, float p_189810_2_);

@Inject(method = "closeScreen", at = @At("HEAD"))
public void onCloseScreen(CallbackInfo ci) {
if (mc.currentScreen instanceof GuiChest) {
CachedContainerManager.onGuiChestClosed();
}
}

@Redirect(method = "onLivingUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/entity/EntityPlayerSP;closeScreen()V"))
public void closeScreen(EntityPlayerSP player) {
if (PortalChat.INSTANCE.isDisabled()) player.closeScreen();
10 changes: 10 additions & 0 deletions src/main/kotlin/com/lambda/client/event/ForgeEventProcessor.kt
Original file line number Diff line number Diff line change
@@ -102,6 +102,16 @@ internal object ForgeEventProcessor {
}
}

@SubscribeEvent
fun onDrawScreenEvent(event: GuiScreenEvent.DrawScreenEvent) {
LambdaEventBus.post(event)
}

@SubscribeEvent
fun onRenderTooltipEvent(event: RenderTooltipEvent.Pre) {
LambdaEventBus.post(event)
}

/**
* Includes events of subclasses like ChunkEvent and GetCollisionBoxesEvent
*/
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
package com.lambda.client.gui.hudgui.elements.player

import com.lambda.client.event.SafeClientEvent
import com.lambda.client.event.events.ConnectionEvent
import com.lambda.client.event.events.PacketEvent
import com.lambda.client.gui.hudgui.HudElement
import com.lambda.client.mixin.extension.windowID
import com.lambda.client.module.modules.client.ClickGUI
import com.lambda.client.module.modules.client.GuiColors
import com.lambda.client.manager.managers.CachedContainerManager
import com.lambda.client.util.color.ColorHolder
import com.lambda.client.util.graphics.GlStateUtils
import com.lambda.client.util.graphics.RenderUtils2D
import com.lambda.client.util.graphics.VertexHelper
import com.lambda.client.util.items.storageSlots
import com.lambda.client.util.math.Vec2d
import com.lambda.client.util.threads.runSafe
import com.lambda.client.util.threads.safeListener
import net.minecraft.client.gui.inventory.GuiContainer
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.client.renderer.Tessellator
import net.minecraft.client.renderer.vertex.DefaultVertexFormats
import net.minecraft.init.Blocks
import net.minecraft.inventory.ContainerChest
import net.minecraft.inventory.InventoryBasic
import net.minecraft.item.ItemStack
import net.minecraft.network.play.client.CPacketCloseWindow
import net.minecraft.network.play.server.SPacketOpenWindow
import net.minecraft.util.ResourceLocation
import net.minecraft.util.text.TextComponentTranslation
@@ -38,33 +30,32 @@ internal object InventoryViewer : HudElement(
private val showIcon by setting("Show Icon", false, { !mcTexture })
private val iconScale by setting("Icon Scale", 0.5f, 0.1f..1.0f, 0.1f, { !mcTexture && showIcon })
private val background by setting("Background", true, { !mcTexture })
private val alpha by setting("Alpha", 150, 0..255, 1, { !mcTexture })
private val backgroundColor by setting("Background Color", ColorHolder(0, 0, 0, 150), visibility = { !mcTexture && background })
private val outline by setting("Outline", true, visibility = { !mcTexture })
private val outlineColor by setting("Outline Color", ColorHolder(255, 255, 255, 150), visibility = { !mcTexture && outline })
private val outlineThickness by setting("Outline Thickness", 1.0f, 0.5f..5.0f, 0.5f, { !mcTexture && outline })
private val containerTexture = ResourceLocation("textures/gui/container/inventory.png")
private val lambdaIcon = ResourceLocation("lambda/lambda_icon.png")
private var enderChestContents: MutableList<ItemStack> = MutableList(27) { ItemStack(Blocks.AIR) }

override val hudWidth: Float = 162.0f
override val hudHeight: Float = 54.0f

private var openedEnderChest: Int = -1

override fun renderHud(vertexHelper: VertexHelper) {
super.renderHud(vertexHelper)
runSafe {
drawFrame(vertexHelper)
drawFrameTexture()
checkEnderChest()
drawItems()
}
}

private fun drawFrame(vertexHelper: VertexHelper) {
if (!mcTexture) {
if (background) {
RenderUtils2D.drawRectFilled(vertexHelper, posEnd = Vec2d(162.0, 54.0), color = GuiColors.backGround.apply { a = alpha })
RenderUtils2D.drawRectFilled(vertexHelper, posEnd = Vec2d(hudWidth.toDouble(), hudHeight.toDouble()), color = backgroundColor)
}
if (ClickGUI.windowOutline) {
RenderUtils2D.drawRectOutline(vertexHelper, posEnd = Vec2d(162.0, 54.0), lineWidth = ClickGUI.outlineWidth, color = GuiColors.outline.apply { a = alpha })
if (outline) {
RenderUtils2D.drawRectOutline(vertexHelper, posEnd = Vec2d(hudWidth.toDouble(), hudHeight.toDouble()), lineWidth = outlineThickness, color = outlineColor)
}
}
}
@@ -78,15 +69,15 @@ internal object InventoryViewer : HudElement(
mc.renderEngine.bindTexture(containerTexture)
buffer.begin(GL_TRIANGLE_STRIP, DefaultVertexFormats.POSITION_TEX)
buffer.pos(0.0, 0.0, 0.0).tex(0.02734375, 0.32421875).endVertex() // (7 / 256), (83 / 256)
buffer.pos(0.0, 54.0, 0.0).tex(0.02734375, 0.53125).endVertex() // (7 / 256), (136 / 256)
buffer.pos(162.0, 0.0, 0.0).tex(0.65625, 0.32421875).endVertex() // (168 / 256), (83 / 256)
buffer.pos(162.0, 54.0, 0.0).tex(0.65625, 0.53125).endVertex() // (168 / 256), (136 / 256)
buffer.pos(0.0, hudHeight.toDouble(), 0.0).tex(0.02734375, 0.53125).endVertex() // (7 / 256), (136 / 256)
buffer.pos(hudWidth.toDouble(), 0.0, 0.0).tex(0.65625, 0.32421875).endVertex() // (168 / 256), (83 / 256)
buffer.pos(hudWidth.toDouble(), hudHeight.toDouble(), 0.0).tex(0.65625, 0.53125).endVertex() // (168 / 256), (136 / 256)
tessellator.draw()
} else if (showIcon) {
mc.renderEngine.bindTexture(lambdaIcon)
GlStateManager.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

val center = Vec2d(81.0, 27.0)
val center = Vec2d(hudWidth / 2.0, hudHeight / 2.0)
val halfWidth = iconScale * 50.0
val halfHeight = iconScale * 50.0

@@ -103,61 +94,33 @@ internal object InventoryViewer : HudElement(


init {
safeListener<ConnectionEvent.Disconnect> {
openedEnderChest = -1
}

safeListener<PacketEvent.Receive> {
if (it.packet !is SPacketOpenWindow) return@safeListener
if (it.packet.guiId != "minecraft:container") return@safeListener
val title = it.packet.windowTitle
if (title !is TextComponentTranslation) return@safeListener
if (title.key != "container.enderchest") return@safeListener

openedEnderChest = it.packet.windowId
}

safeListener<PacketEvent.PostSend> {
if (it.packet !is CPacketCloseWindow) return@safeListener
if (it.packet.windowID != openedEnderChest) return@safeListener

checkEnderChest()
openedEnderChest = -1
}
}

private fun checkEnderChest() {
val guiScreen = mc.currentScreen

if (guiScreen !is GuiContainer) return

val container = guiScreen.inventorySlots

if (container is ContainerChest && container.lowerChestInventory is InventoryBasic) {
if (container.windowId == openedEnderChest) {
for (i in 0..26) enderChestContents[i] = container.inventory[i]
}
}
}

private fun SafeClientEvent.drawItems() {
if (enderChest == SlotType.ENDER_CHEST) {
for ((index, stack) in enderChestContents.withIndex()) {
if (stack.isEmpty) continue

val slotX = index % 9 * 18 + 1
val slotY = index / 9 * 18 + 1
RenderUtils2D.drawItem(stack, slotX, slotY)
CachedContainerManager.getEnderChestInventory().forEachIndexed { index, stack ->
if (stack.isEmpty) return@forEachIndexed
val slotX = index % 9 * (hudWidth / 9.0) + 1
val slotY = index / 9 * (hudWidth / 9.0) + 1
RenderUtils2D.drawItem(stack, slotX.toInt(), slotY.toInt())
}
} else {
for ((index, slot) in player.storageSlots.withIndex()) {
val itemStack = slot.stack
if (itemStack.isEmpty) continue

val slotX = index % 9 * 18 + 1
val slotY = index / 9 * 18 + 1
val slotX = index % 9 * (hudWidth / 9.0) + 1
val slotY = index / 9 * (hudWidth / 9.0) + 1

RenderUtils2D.drawItem(itemStack, slotX, slotY)
RenderUtils2D.drawItem(itemStack, slotX.toInt(), slotY.toInt())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.lambda.client.manager.managers

import com.lambda.client.LambdaMod
import com.lambda.client.event.events.ConnectionEvent
import com.lambda.client.event.listener.listener
import com.lambda.client.manager.Manager
import com.lambda.client.module.modules.player.PacketLogger
import com.lambda.client.module.modules.render.ContainerPreview.cacheEnderChests
import com.lambda.client.util.FolderUtils
import com.lambda.client.util.threads.defaultScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.minecraft.inventory.ContainerChest
import net.minecraft.inventory.IInventory
import net.minecraft.inventory.InventoryBasic
import net.minecraft.inventory.ItemStackHelper
import net.minecraft.item.ItemStack
import net.minecraft.nbt.CompressedStreamTools
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.NonNullList
import java.io.File
import java.io.IOException
import java.nio.file.Paths

object CachedContainerManager : Manager {
private val directory = Paths.get(FolderUtils.lambdaFolder, "cached-containers").toFile()
private var echestFile: File? = null
private var currentEnderChest: NonNullList<ItemStack>? = null

init {
listener<ConnectionEvent.Connect> {
val serverDirectory = if (mc.integratedServer != null && mc.integratedServer?.isServerRunning == true) {
mc.integratedServer?.folderName ?: run {
LambdaMod.LOG.info("Failed to get SP directory")
return@listener
}
} else {
mc.currentServerData?.serverIP?.replace(":", "_")
?: run {
LambdaMod.LOG.info("Failed to get server directory")
return@listener
}
}

val folder = File(directory, serverDirectory)
echestFile = folder.toPath().resolve(mc.session.profile.id.toString()).resolve("echest.nbt").toFile()

echestFile?.let { file ->
try {
if (!file.exists()) {
if (!file.parentFile.exists()) file.parentFile.mkdirs()
file.createNewFile()
}
} catch (e: IOException) {
LambdaMod.LOG.error("Failed to create ender chest file", e)
}
}
}

listener<ConnectionEvent.Disconnect> {
echestFile = null
currentEnderChest = null
}
}

fun getEnderChestInventory(): NonNullList<ItemStack> {
echestFile?.let { eFile ->
currentEnderChest?.let { return it }
if (!cacheEnderChests) return@let
try {
CompressedStreamTools.read(eFile)?.let { nbt ->
val inventory = NonNullList.withSize(27, ItemStack.EMPTY)
ItemStackHelper.loadAllItems(nbt, inventory)
currentEnderChest = inventory
return inventory
}
} catch (e: IOException) {
currentEnderChest = NonNullList.withSize(27, ItemStack.EMPTY)
LambdaMod.LOG.warn("${PacketLogger.chatName} Failed loading echest!", e)
}

}
return NonNullList.withSize(27, ItemStack.EMPTY)
}

private fun saveEchest(inventory: NonNullList<ItemStack>) {
if (!cacheEnderChests) return
val currentEchestFile = echestFile ?: return
val nonNullList = NonNullList.withSize(inventory.size, ItemStack.EMPTY)
inventory.forEachIndexed { index, itemStack ->
nonNullList[index] = itemStack
}
val nbt = NBTTagCompound()
ItemStackHelper.saveAllItems(nbt, nonNullList)

defaultScope.launch(Dispatchers.IO) {
try {
CompressedStreamTools.write(nbt, currentEchestFile)
} catch (e: Throwable) {
LambdaMod.LOG.warn("${PacketLogger.chatName} Failed saving echest!", e)
}
}
}

@JvmStatic
fun setEnderChestInventory(inv: IInventory) {
val inventory = NonNullList.withSize(inv.sizeInventory, ItemStack.EMPTY)
for (i in 0 until inv.sizeInventory) {
inventory[i] = inv.getStackInSlot(i)
}
currentEnderChest = inventory
saveEchest(inventory)
}

@JvmStatic
fun updateContainerInventory(windowId: Int) {
val container = mc.player.openContainer
if (container.windowId != windowId) return
if (container !is ContainerChest) return
val chest = container.lowerChestInventory
if (chest !is InventoryBasic) return
if (chest.name.contains("Ender Chest"))
setEnderChestInventory(chest)
// we can save other container inventories here but we also need the position
}

@JvmStatic
fun onGuiChestClosed() {
val container = mc.player.openContainer
if (container !is ContainerChest) return
val chest = container.lowerChestInventory
if (chest !is InventoryBasic) return
if (chest.name.contains("Ender Chest"))
setEnderChestInventory(chest)
// we can save other container inventories here but we also need the position
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -16,12 +16,12 @@ import kotlin.math.*
object RenderUtils2D {
val mc = Wrapper.minecraft

fun drawItem(itemStack: ItemStack, x: Int, y: Int, text: String? = null, drawOverlay: Boolean = true) {
fun drawItem(itemStack: ItemStack, x: Int, y: Int, text: String? = null, drawOverlay: Boolean = true, z: Float = 0.0f) {
GlStateUtils.blend(true)
GlStateUtils.depth(true)
RenderHelper.enableGUIStandardItemLighting()

mc.renderItem.zLevel = 0.0f
mc.renderItem.zLevel = z
mc.renderItem.renderItemAndEffectIntoGUI(itemStack, x, y)
if (drawOverlay) mc.renderItem.renderItemOverlayIntoGUI(mc.fontRenderer, itemStack, x, y, text)
mc.renderItem.zLevel = 0.0f
1 change: 0 additions & 1 deletion src/main/resources/mixins.lambda.json
Original file line number Diff line number Diff line change
@@ -42,7 +42,6 @@
"entity.MixinEntityLlama",
"entity.MixinEntityPig",
"gui.MixinGuiChat",
"gui.MixinGuiChest",
"gui.MixinGuiContainer",
"gui.MixinGuiIngameForge",
"gui.MixinGuiIngameMenu",