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

refactor: Use ItemIdentifierStack in ItemStackRenderer #40

Merged
merged 2 commits into from
May 19, 2024
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
4 changes: 3 additions & 1 deletion src/main/java/logisticspipes/gui/hud/HudChassisPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import logisticspipes.utils.gui.GuiGraphics;
import logisticspipes.utils.gui.hud.BasicHUDButton;
import logisticspipes.utils.item.ItemIdentifierInventory;
import logisticspipes.utils.item.ItemIdentifierStack;
import logisticspipes.utils.item.ItemStackRenderer;
import logisticspipes.utils.item.ItemStackRenderer.DisplayAmount;

Expand Down Expand Up @@ -297,7 +298,8 @@ public void renderButton(boolean hover, boolean clicked, boolean shifted) {
false,
shifted,
renderInColor);
itemStackRenderer.setItemstack(module).setDisplayAmount(DisplayAmount.NEVER);
itemStackRenderer.setItemIdentifierStack(ItemIdentifierStack.getFromStack(module))
.setDisplayAmount(DisplayAmount.NEVER);

itemStackRenderer.renderInGui();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import logisticspipes.routing.PipeRoutingConnectionType;
import logisticspipes.utils.MathVector;
import logisticspipes.utils.gui.GuiGraphics;
import logisticspipes.utils.item.ItemIdentifierStack;
import logisticspipes.utils.item.ItemStackRenderer;
import logisticspipes.utils.item.ItemStackRenderer.DisplayAmount;
import logisticspipes.utils.tuples.Pair;
Expand Down Expand Up @@ -406,7 +407,8 @@ public void setHUDSatellite(boolean state) {}
GL11.glScalef(scaleX, scaleY, scaleZ);

ItemStackRenderer itemStackRenderer = new ItemStackRenderer(5, 6, 0.0F, false, true, true);
itemStackRenderer.setItemstack(item).setDisplayAmount(DisplayAmount.NEVER);
itemStackRenderer.setItemIdentifierStack(ItemIdentifierStack.getFromStack(item))
.setDisplayAmount(DisplayAmount.NEVER);
itemStackRenderer.setScaleX(scaleX).setScaleY(scaleY).setScaleZ(scaleZ);

itemStackRenderer.renderInGui();
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/logisticspipes/renderer/LogisticsRenderPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,8 @@ private void renderSolids(CoreUnroutedPipe pipe, double x, double y, double z, f
|| item.getContainer().zCoord != pipe.container.zCoord) {
continue;
}
ItemStack itemstack = item.getItemIdentifierStack().makeNormalStack();
doRenderItem(
itemstack,
item.getItemIdentifierStack(),
pipe.container.getWorldObj(),
x + pos.getXD(),
y + pos.getYD(),
Expand All @@ -224,9 +223,8 @@ private void renderSolids(CoreUnroutedPipe pipe, double x, double y, double z, f
if (item == null || item.getValue1() == null) {
continue;
}
ItemStack itemstack = item.getValue1().makeNormalStack();
doRenderItem(
itemstack,
item.getValue1(),
pipe.container.getWorldObj(),
x + pos.getXD(),
y + pos.getYD(),
Expand All @@ -253,17 +251,17 @@ private void renderSolids(CoreUnroutedPipe pipe, double x, double y, double z, f
GL11.glPopMatrix();
}

public void doRenderItem(ItemStack itemstack, World worldObj, double x, double y, double z, float light,
public void doRenderItem(ItemIdentifierStack itemstack, World worldObj, double x, double y, double z, float light,
float renderScale, double boxScale, float partialTickTime) {
if (LogisticsRenderPipe.config.isUseNewRenderer() && boxScale != 0) {
LogisticsRenderPipe.boxRenderer.doRenderItem(itemstack, light, x, y, z, boxScale);
LogisticsRenderPipe.boxRenderer.doRenderItem(itemstack.makeNormalStack(), light, x, y, z, boxScale);
}

GL11.glPushMatrix();
GL11.glTranslated(x, y, z);
GL11.glScalef(renderScale, renderScale, renderScale);
GL11.glTranslatef(0.0F, -0.1F, 0.0F);
itemRenderer.setItemstack(itemstack).setWorldObj(worldObj).setPartialTickTime(partialTickTime);
itemRenderer.setItemIdentifierStack(itemstack).setWorldObj(worldObj).setPartialTickTime(partialTickTime);
itemRenderer.renderInWorld();
GL11.glPopMatrix();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/logisticspipes/utils/gui/ItemDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public void renderItemArea(double zLevel) {

// use GuiGraphics to render the ItemStacks
ItemStackRenderer itemstackRenderer = new ItemStackRenderer(x, y, 100.0F, true, false, true);
itemstackRenderer.setItemstack(itemstack).setDisplayAmount(DisplayAmount.HIDE_ONE);
itemstackRenderer.setItemIdentifierStack(itemIdentifierStack).setDisplayAmount(DisplayAmount.HIDE_ONE);
itemstackRenderer.renderInGui();

x += panelxSize;
Expand Down
64 changes: 28 additions & 36 deletions src/main/java/logisticspipes/utils/item/ItemStackRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

package logisticspipes.utils.item;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.minecraft.block.Block;
import net.minecraft.block.BlockPane;
Expand All @@ -19,7 +21,6 @@
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
Expand All @@ -46,7 +47,7 @@ public class ItemStackRenderer {
private TextureManager texManager;
private FontRenderer fontRenderer;

private ItemStack itemstack;
private ItemIdentifierStack itemIdentifierStack;
private int posX;
private int posY;
private float zLevel;
Expand All @@ -57,10 +58,11 @@ public class ItemStackRenderer {
private boolean renderEffects;
private boolean ignoreDepth;
private boolean renderInColor;
private EntityItem entityitem;
private World worldObj;
private float partialTickTime;

private final Map<ItemIdentifier, EntityItem> entityCache = new HashMap<>();

public ItemStackRenderer(int posX, int posY, float zLevel, boolean renderEffects, boolean ignoreDepth,
boolean renderInColor) {
this.posX = posX;
Expand Down Expand Up @@ -138,8 +140,8 @@ public static void renderItemIdentifierStackListIntoGui(List<ItemIdentifierStack
int column = 0;
int row = 0;

for (ItemIdentifierStack identifierStack : _allItems) {
if (identifierStack == null) {
for (ItemIdentifierStack itemIdentifierStack : _allItems) {
if (itemIdentifierStack == null) {
column++;
if (column >= columns) {
row++;
Expand All @@ -148,7 +150,7 @@ public static void renderItemIdentifierStackListIntoGui(List<ItemIdentifierStack
ppi++;
continue;
}
ItemIdentifier item = identifierStack.getItem();
ItemIdentifier item = itemIdentifierStack.getItem();
if (IItemSearch != null && !IItemSearch.itemSearched(item)) {
continue;
}
Expand All @@ -160,14 +162,12 @@ public static void renderItemIdentifierStackListIntoGui(List<ItemIdentifierStack
if (ppi > items * (page + 1)) {
continue;
}
ItemStack itemstack = identifierStack.unsafeMakeNormalStack();

int x = left + xSize * column;
int y = top + ySize * row + 1;

if (itemstack != null) {
itemStackRenderer.setItemstack(itemstack).setPosX(x).setPosY(y);
itemStackRenderer.renderInGui();
}
itemStackRenderer.setItemIdentifierStack(itemIdentifierStack).setPosX(x).setPosY(y);
itemStackRenderer.renderInGui();

column++;
if (column >= columns) {
Expand All @@ -178,7 +178,7 @@ public static void renderItemIdentifierStackListIntoGui(List<ItemIdentifierStack
}

public void renderInGui() {
assert itemstack != null;
assert itemIdentifierStack != null;
assert displayAmount != null;
assert renderBlocks != null;
assert renderItem != null;
Expand Down Expand Up @@ -206,10 +206,11 @@ public void renderInGui() {
GL11.glEnable(GL11.GL_DEPTH_TEST);
}

ItemStack itemStack = itemIdentifierStack.makeNormalStack();
if (!ForgeHooksClient
.renderInventoryItem(renderBlocks, texManager, itemstack, renderInColor, zLevel, posX, posY)) {
.renderInventoryItem(renderBlocks, texManager, itemStack, renderInColor, zLevel, posX, posY)) {
renderItem.zLevel += zLevel;
renderItem.renderItemIntoGUI(fontRenderer, texManager, itemstack, posX, posY, renderEffects);
renderItem.renderItemIntoGUI(fontRenderer, texManager, itemStack, posX, posY, renderEffects);
renderItem.zLevel -= zLevel;
}

Expand All @@ -222,7 +223,7 @@ public void renderInGui() {
GL11.glEnable(GL11.GL_DEPTH_TEST);
}
// 20 should be about the size of a block
GuiGraphics.drawDurabilityBar(itemstack, posX, posY, zLevel + 20.0F);
GuiGraphics.drawDurabilityBar(itemStack, posX, posY, zLevel + 20.0F);

// if we want to render the amount, do that
if (displayAmount != DisplayAmount.NEVER) {
Expand All @@ -232,15 +233,15 @@ public void renderInGui() {
GL11.glEnable(GL11.GL_DEPTH_TEST);
}

FontRenderer specialFontRenderer = itemstack.getItem().getFontRenderer(itemstack);
FontRenderer specialFontRenderer = itemIdentifierStack.getItem().item.getFontRenderer(itemStack);

if (specialFontRenderer != null) {
fontRenderer = specialFontRenderer;
}

GL11.glDisable(GL11.GL_LIGHTING);
String amountString = StringUtils
.getFormatedStackSize(itemstack.stackSize, displayAmount == DisplayAmount.ALWAYS);
.getFormatedStackSize(itemIdentifierStack.getStackSize(), displayAmount == DisplayAmount.ALWAYS);

// 20 should be about the size of a block + 20 for the effect and overlay
GL11.glTranslatef(0.0F, 0.0F, zLevel + 40.0F);
Expand All @@ -262,39 +263,30 @@ public void renderInGui() {
public void renderInWorld() {
assert renderManager != null;
assert renderItem != null;
assert scaleX != 0.0F;
assert scaleY != 0.0F;
assert scaleZ != 0.0F;

if (entityitem == null || !ItemStack.areItemStacksEqual(entityitem.getEntityItem(), itemstack)) {
if (itemstack == null) {
throw new RuntimeException("No EntityItem and no ItemStack, I do not know what to render!");
} else {
if (worldObj == null) {
throw new NullPointerException("World object is null");
}
entityitem = new EntityItem(worldObj, 0.0D, 0.0D, 0.0D, itemstack);
entityitem.getEntityItem().stackSize = 1;
entityitem.hoverStart = 0.0F;
}
ItemIdentifier itemId = itemIdentifierStack.getItem();
EntityItem entityItem = entityCache.get(itemId);
if (entityItem == null) {
entityItem = new EntityItem(worldObj, 0, 0, 0, itemId.makeNormalStack(1));
entityItem.hoverStart = 0;
entityCache.put(itemId, entityItem);
}

boolean changeColor = renderItem.renderWithColor != renderInColor;
if (changeColor) {
renderItem.renderWithColor = renderInColor;
}

Item item = itemstack.getItem();
if (item instanceof ItemBlock) {
Block block = ((ItemBlock) item).field_150939_a;
if (itemId.item instanceof ItemBlock) {
Block block = ((ItemBlock) itemId.item).field_150939_a;
if (block instanceof BlockPane) {
GL11.glScalef(0.5F, 0.5F, 0.5F);
}
} else if (item == LogisticsPipes.logisticsRequestTable) {
} else if (itemId.item == LogisticsPipes.logisticsRequestTable) {
GL11.glScalef(0.5F, 0.5F, 0.5F);
}

renderManager.renderEntityWithPosYaw(entityitem, posX, posY, zLevel, 0.0F, partialTickTime);
renderManager.renderEntityWithPosYaw(entityItem, posX, posY, zLevel, 0.0F, partialTickTime);

if (changeColor) {
renderItem.renderWithColor = !renderInColor;
Expand Down
Loading