Skip to content
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

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

Merged
merged 5 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
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;
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
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
Expand Up @@ -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
*/
Expand Down
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
Expand All @@ -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)
}
}
}
Expand All @@ -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

Expand All @@ -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())
}
}
}
Expand Down
Loading