diff --git a/src/main/java/com/brandon3055/draconicevolution/client/render/tile/RenderTilePlacedItem.java b/src/main/java/com/brandon3055/draconicevolution/client/render/tile/RenderTilePlacedItem.java index 2594cddb..0ba81835 100644 --- a/src/main/java/com/brandon3055/draconicevolution/client/render/tile/RenderTilePlacedItem.java +++ b/src/main/java/com/brandon3055/draconicevolution/client/render/tile/RenderTilePlacedItem.java @@ -31,17 +31,6 @@ public class RenderTilePlacedItem extends TileEntitySpecialRenderer { private final EntityItem cachedEntity = new EntityItem(null, 0, 0, 0, new ItemStack(Items.apple)); private final PlacedItemDisplayListCache displayListCache = new PlacedItemDisplayListCache(); - /** - * Pivot point on the mounting face and the two in-plane axes used to lay out the item grid, indexed by block - * metadata (the side the display is attached to). - */ - private static final float[][] FACE_PIVOT = { { 0.5F, 1F, 0.5F }, { 0.5F, 0F, 0.5F }, { 0.5F, 0.5F, 1F }, - { 0.5F, 0.5F, 0F }, { 1F, 0.5F, 0.5F }, { 0F, 0.5F, 0.5F } }; - private static final float[][] FACE_AXIS_U = { { 1F, 0F, 0F }, { 1F, 0F, 0F }, { 1F, 0F, 0F }, { 1F, 0F, 0F }, - { 0F, 0F, 1F }, { 0F, 0F, 1F } }; - private static final float[][] FACE_AXIS_V = { { 0F, 0F, 1F }, { 0F, 0F, 1F }, { 0F, 1F, 0F }, { 0F, 1F, 0F }, - { 0F, 1F, 0F }, { 0F, 1F, 0F } }; - @Override public void renderTileEntityAt(TileEntity te, double x, double y, double z, float timeSinceLastTick) { if (!(te instanceof TilePlacedItem tile)) return; @@ -75,12 +64,11 @@ public class RenderTilePlacedItem extends TileEntitySpecialRenderer { */ private void renderGrid(TilePlacedItem tile, int meta) { int count = tile.getDisplayCount(); - int gridSize = (int) Math.ceil(Math.sqrt(count)); - int rows = (count + gridSize - 1) / gridSize; + int gridSize = TilePlacedItem.getGridSize(count); float cell = 1F / gridSize; - float[] pivot = FACE_PIVOT[meta]; - float[] axisU = FACE_AXIS_U[meta]; - float[] axisV = FACE_AXIS_V[meta]; + float[] pivot = TilePlacedItem.FACE_PIVOT[meta]; + float[] axisU = TilePlacedItem.FACE_AXIS_U[meta]; + float[] axisV = TilePlacedItem.FACE_AXIS_V[meta]; for (int i = 0; i < count; i++) { ItemStack stack = tile.getStack(i); @@ -88,11 +76,9 @@ public class RenderTilePlacedItem extends TileEntitySpecialRenderer { GL11.glPushMatrix(); if (gridSize > 1) { - int row = i / gridSize; - int col = i % gridSize; - int rowLength = Math.min(gridSize, count - row * gridSize); - float u = (col - (rowLength - 1) / 2F) * cell; - float v = ((rows - 1) / 2F - row) * cell; + float[] offset = TilePlacedItem.getCellOffset(i, count); + float u = offset[0]; + float v = offset[1]; GL11.glTranslatef( pivot[0] + axisU[0] * u + axisV[0] * v, diff --git a/src/main/java/com/brandon3055/draconicevolution/common/blocks/PlacedItem.java b/src/main/java/com/brandon3055/draconicevolution/common/blocks/PlacedItem.java index d01badd7..a542070e 100644 --- a/src/main/java/com/brandon3055/draconicevolution/common/blocks/PlacedItem.java +++ b/src/main/java/com/brandon3055/draconicevolution/common/blocks/PlacedItem.java @@ -178,8 +178,8 @@ public class PlacedItem extends BlockDE { } @Override - public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, - float p_149727_7_, float p_149727_8_, float p_149727_9_) { + public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, + float hitY, float hitZ) { TilePlacedItem tile = (world.getTileEntity(x, y, z) != null && world.getTileEntity(x, y, z) instanceof TilePlacedItem) ? (TilePlacedItem) world.getTileEntity(x, y, z) @@ -191,9 +191,10 @@ public class PlacedItem extends BlockDE { if (player.isSneaking()) { tile.rotation += 5.625F; } else { - // Pop the most recently placed item; the block is removed once the last one is taken. + // Remove the item the player clicked on; the block is removed once the last one is taken. if (!world.isRemote) { - ItemStack stack = tile.removeLastItem(); + int index = tile.getStackIndexAt(world.getBlockMetadata(x, y, z), hitX, hitY, hitZ); + ItemStack stack = tile.removeItem(index); if (stack != null) { dropStack(world, x, y, z, stack); } @@ -237,9 +238,13 @@ public class PlacedItem extends BlockDE { @Override public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) { TileEntity te = world.getTileEntity(x, y, z); - if (te != null && te instanceof TilePlacedItem && ((TilePlacedItem) te).getStack() != null) { - TilePlacedItem tile = (TilePlacedItem) te; - return tile.getStack(); + if (te instanceof TilePlacedItem tile && target != null && target.hitVec != null) { + int index = tile.getStackIndexAt( + world.getBlockMetadata(x, y, z), + (float) (target.hitVec.xCoord - x), + (float) (target.hitVec.yCoord - y), + (float) (target.hitVec.zCoord - z)); + return tile.getStack(index); } return null; } diff --git a/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TilePlacedItem.java b/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TilePlacedItem.java index fe3e2aff..40b811cd 100644 --- a/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TilePlacedItem.java +++ b/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TilePlacedItem.java @@ -23,6 +23,18 @@ public class TilePlacedItem extends TileEntity { public static final int ABSOLUTE_MAX_STACKS = 16; + /** + * Pivot point on the mounting face and the two in-plane axes used to lay out the item grid, indexed by block + * metadata (the side the display is attached to). Shared by the renderer and the hit test so that the stack a + * player clicks on is the one they see. + */ + public static final float[][] FACE_PIVOT = { { 0.5F, 1F, 0.5F }, { 0.5F, 0F, 0.5F }, { 0.5F, 0.5F, 1F }, + { 0.5F, 0.5F, 0F }, { 1F, 0.5F, 0.5F }, { 0F, 0.5F, 0.5F } }; + public static final float[][] FACE_AXIS_U = { { 1F, 0F, 0F }, { 1F, 0F, 0F }, { 1F, 0F, 0F }, { 1F, 0F, 0F }, + { 0F, 0F, 1F }, { 0F, 0F, 1F } }; + public static final float[][] FACE_AXIS_V = { { 0F, 0F, 1F }, { 0F, 0F, 1F }, { 0F, 1F, 0F }, { 0F, 1F, 0F }, + { 0F, 1F, 0F }, { 0F, 1F, 0F } }; + private final List stacks = new ArrayList<>(ABSOLUTE_MAX_STACKS); public float rotation = 0F; private boolean hasUpdated = false; @@ -93,6 +105,86 @@ public class TilePlacedItem extends TileEntity { return stack; } + /** + * Removes and returns the item at {@code index}, or null if the index is out of range. + */ + public ItemStack removeItem(int index) { + if (index < 0 || index >= stacks.size()) { + return null; + } + ItemStack stack = stacks.remove(index); + markDirtyAndSync(); + return stack; + } + + /** + * @return the number of columns (and maximum rows) in the grid used to display {@code count} items. + */ + public static int getGridSize(int count) { + return (int) Math.ceil(Math.sqrt(count)); + } + + /** + * Computes where the centre of grid cell {@code index} sits relative to the face pivot, in block units along + * {@link #FACE_AXIS_U} and {@link #FACE_AXIS_V}. Rows are centred so a partially filled last row does not hang to + * one side. + * + * @return {u, v} + */ + public static float[] getCellOffset(int index, int count) { + int gridSize = getGridSize(count); + if (gridSize <= 1) { + return new float[] { 0F, 0F }; + } + int rows = (count + gridSize - 1) / gridSize; + float cell = 1F / gridSize; + int row = index / gridSize; + int col = index % gridSize; + int rowLength = Math.min(gridSize, count - row * gridSize); + return new float[] { (col - (rowLength - 1) / 2F) * cell, ((rows - 1) / 2F - row) * cell }; + } + + /** + * Finds the stack whose grid cell is closest to a point on this block. + * + * @param meta the block metadata (mounting side) + * @param hitX x of the hit, relative to the block (0..1) + * @param hitY y of the hit, relative to the block (0..1) + * @param hitZ z of the hit, relative to the block (0..1) + * @return the index of the closest stack, or -1 if the display is empty + */ + public int getStackIndexAt(int meta, float hitX, float hitY, float hitZ) { + int count = stacks.size(); + if (count == 0) { + return -1; + } + if (meta < 0 || meta > 5) { + meta = 1; + } + float[] pivot = FACE_PIVOT[meta]; + float[] axisU = FACE_AXIS_U[meta]; + float[] axisV = FACE_AXIS_V[meta]; + float dx = hitX - pivot[0]; + float dy = hitY - pivot[1]; + float dz = hitZ - pivot[2]; + float u = dx * axisU[0] + dy * axisU[1] + dz * axisU[2]; + float v = dx * axisV[0] + dy * axisV[1] + dz * axisV[2]; + + int closest = 0; + float closestDistSq = Float.MAX_VALUE; + for (int i = 0; i < count; i++) { + float[] offset = getCellOffset(i, count); + float du = u - offset[0]; + float dv = v - offset[1]; + float distSq = du * du + dv * dv; + if (distSq < closestDistSq) { + closestDistSq = distSq; + closest = i; + } + } + return closest; + } + public static int getStackLimit() { return Math.min(ConfigHandler.placedItemStackLimit, ABSOLUTE_MAX_STACKS); }