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

enh: Add ender chest preview #58

Merged
merged 2 commits into from
May 4, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.lambda.client.mixin.client.gui;

import com.lambda.client.module.modules.render.ContainerPreview;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryEnderChest;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(GuiChest.class)
public class MixinGuiChest {

@Final @Shadow private IInventory lowerChestInventory;

@Inject(method = "drawScreen", at = @At("RETURN"))
public void drawScreen(int mouseX, int mouseY, float partialTicks, CallbackInfo ci) {
if (lowerChestInventory.getName().equals("Ender Chest")) {
ContainerPreview.INSTANCE.setEnderChestItems(lowerChestInventory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import com.lambda.client.module.modules.render.CleanGUI;
import com.lambda.client.module.modules.render.MapPreview;
import com.lambda.client.module.modules.render.ShulkerPreview;
import com.lambda.client.module.modules.render.ContainerPreview;
import com.lambda.client.util.Wrapper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEnderChest;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.ItemMap;
import net.minecraft.item.ItemShulkerBox;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.world.storage.MapData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -20,12 +26,29 @@ public class MixinGuiScreen {

@Inject(method = "renderToolTip", at = @At("HEAD"), cancellable = true)
public void renderToolTip(ItemStack stack, int x, int y, CallbackInfo info) {
if (ShulkerPreview.INSTANCE.isEnabled() && stack.getItem() instanceof ItemShulkerBox) {
NBTTagCompound tagCompound = ShulkerPreview.INSTANCE.getShulkerData(stack);
if (ContainerPreview.INSTANCE.isEnabled() && (stack.getItem() instanceof ItemShulkerBox || Block.getBlockFromItem(stack.getItem()) instanceof BlockEnderChest)) {
NonNullList<ItemStack> items = NonNullList.withSize(27, ItemStack.EMPTY);

if (tagCompound != null) {
if (stack.getItem() instanceof ItemShulkerBox) {
NBTTagCompound tagCompound = ContainerPreview.INSTANCE.getShulkerData(stack);
if (tagCompound != null) {
ItemStackHelper.loadAllItems(tagCompound, items);
} else {
items = null;
}
} else {
// Item is an Ender Chest
IInventory inventory = ContainerPreview.INSTANCE.getEnderChestItems();
if (inventory != null) {
for (int i = 0; i < items.size(); i++) {
items.set(i, inventory.getStackInSlot(i));
}
}
}

if (items != null) {
info.cancel();
ShulkerPreview.INSTANCE.renderShulkerAndItems(stack, x, y, tagCompound);
ContainerPreview.INSTANCE.renderContainerAndItems(stack, x, y, items);
}
} else if (MapPreview.INSTANCE.isEnabled() && stack.getItem() instanceof ItemMap) {
MapData mapData = MapPreview.getMapData(stack);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lambda.client.module.modules.render

import com.lambda.client.mixin.client.gui.MixinGuiScreen
import com.lambda.client.module.Category
import com.lambda.client.module.Module
import com.lambda.client.util.color.ColorHolder
Expand All @@ -9,36 +8,39 @@ import com.lambda.client.util.graphics.RenderUtils2D
import com.lambda.client.util.graphics.VertexHelper
import com.lambda.client.util.graphics.font.FontRenderAdapter
import com.lambda.client.util.math.Vec2d
import com.lambda.client.util.threads.safeListener
import com.lambda.commons.extension.ceilToInt
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.inventory.ItemStackHelper
import net.minecraft.inventory.ContainerChest
import net.minecraft.inventory.IInventory
import net.minecraft.inventory.InventoryEnderChest
import net.minecraft.item.ItemShulkerBox
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.network.play.server.SPacketWindowItems
import net.minecraft.util.NonNullList

/**
* @see MixinGuiScreen.renderToolTip
*/
internal object ShulkerPreview : Module(
name = "ShulkerPreview",
internal object ContainerPreview : Module(
name = "ContainerPreview",
category = Category.RENDER,
description = "Previews shulkers in the game GUI"
description = "Previews shulkers and ender chests in the game GUI"
) {

private val useCustomFont by setting("Use Custom Font", false)
private val backgroundColorSetting by setting("Background Color", ColorHolder(16, 0, 16, 190))
private val borderTopColor by setting("Top Border Color", ColorHolder(144, 101, 237, 54))
private val borderBottomColor by setting("Bottom Border Color", ColorHolder(40, 0, 127, 80))
var enderChestItems: IInventory? = null

fun renderShulkerAndItems(stack: ItemStack, originalX: Int, originalY: Int, tagCompound: NBTTagCompound) {
val shulkerInventory = NonNullList.withSize(27, ItemStack.EMPTY)
fun renderContainerAndItems(stack: ItemStack, originalX: Int, originalY: Int, items: NonNullList<ItemStack>) {
GlStateManager.pushMatrix()
GlStateManager.translate(0.0, 0.0, 500.0)
ItemStackHelper.loadAllItems(tagCompound, shulkerInventory)

renderShulker(stack, originalX, originalY)
renderShulkerItems(shulkerInventory, originalX, originalY)
renderContainer(stack, originalX, originalY)
renderContainerItems(items, originalX, originalY)

GlStateManager.popMatrix()
}
Expand All @@ -56,7 +58,7 @@ internal object ShulkerPreview : Module(
return null
}

private fun renderShulker(stack: ItemStack, originalX: Int, originalY: Int) {
private fun renderContainer(stack: ItemStack, originalX: Int, originalY: Int) {
val width = 144.coerceAtLeast(FontRenderAdapter.getStringWidth(stack.displayName).ceilToInt() + 3)
val vertexHelper = VertexHelper(GlStateUtils.useVbo())

Expand Down Expand Up @@ -85,7 +87,7 @@ internal object ShulkerPreview : Module(
FontRenderAdapter.drawString(stack.displayName, x.toFloat(), y.toFloat() - 2.0f, customFont = useCustomFont)
}

private fun renderShulkerItems(shulkerInventory: NonNullList<ItemStack>, originalX: Int, originalY: Int) {
private fun renderContainerItems(shulkerInventory: NonNullList<ItemStack>, originalX: Int, originalY: Int) {
for (i in 0 until shulkerInventory.size) {
val x = originalX + (i % 9) * 16 + 11
val y = originalY + (i / 9) * 16 - 2
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.lambda.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"entity.MixinEntityLlama",
"entity.MixinEntityPig",
"gui.MixinGuiChat",
"gui.MixinGuiChest",
"gui.MixinGuiContainer",
"gui.MixinGuiIngameForge",
"gui.MixinGuiIngameMenu",
Expand Down