Skip to content

Commit ec919fa

Browse files
committed
Use VoxelShape arrays instead of Byte2ObjectMaps for block shape caches
1 parent 302413f commit ec919fa

File tree

5 files changed

+30
-38
lines changed

5 files changed

+30
-38
lines changed

common/src/main/java/juuxel/adorn/block/AbstractTableBlock.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos po
7878
);
7979
}
8080

81-
protected static byte getShapeKey(boolean north, boolean east, boolean south, boolean west, boolean hasCarpet) {
82-
return (byte) ((north ? 1 : 0) << 4 | (east ? 1 : 0) << 3 | (south ? 1 : 0) << 2 | (west ? 1 : 0) << 1 | (hasCarpet ? 1 : 0));
81+
protected static int getShapeKey(boolean north, boolean east, boolean south, boolean west, boolean hasCarpet) {
82+
return (north ? 1 : 0) << 4 | (east ? 1 : 0) << 3 | (south ? 1 : 0) << 2 | (west ? 1 : 0) << 1 | (hasCarpet ? 1 : 0);
8383
}
8484

85-
protected abstract VoxelShape getShapeForKey(byte key);
85+
protected abstract VoxelShape getShapeForKey(int key);
8686

8787
@Override
8888
public boolean canPathfindThrough(BlockState state, NavigationType type) {

common/src/main/java/juuxel/adorn/block/BenchBlock.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package juuxel.adorn.block;
22

3-
import it.unimi.dsi.fastutil.bytes.Byte2ObjectArrayMap;
4-
import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap;
53
import juuxel.adorn.block.variant.BlockVariant;
64
import juuxel.adorn.lib.AdornStats;
75
import juuxel.adorn.util.AdornUtil;
@@ -38,7 +36,7 @@ public final class BenchBlock extends SeatBlock implements Waterloggable, BlockW
3836
private static final String DESCRIPTION_KEY = "block.adorn.bench.description";
3937
private static final VoxelShape X_TOP_SHAPE = createCuboidShape(0.0, 8.0, 1.0, 16.0, 10.0, 15.0);
4038
private static final VoxelShape Z_TOP_SHAPE = createCuboidShape(1.0, 8.0, 0.0, 15.0, 10.0, 16.0);
41-
private static final Byte2ObjectMap<VoxelShape> SHAPES = new Byte2ObjectArrayMap<>();
39+
private static final VoxelShape[] SHAPES = new VoxelShape[8];
4240

4341
static {
4442
var legShapes = Shapes.buildShapeRotationsFromNorth(2, 0, 2, 14, 8, 4);
@@ -61,8 +59,8 @@ public final class BenchBlock extends SeatBlock implements Waterloggable, BlockW
6159
parts.add(positiveLeg);
6260
}
6361

64-
var key = getShapeKey(axis, connectedN, connectedP);
65-
SHAPES.put(key, VoxelShapes.union(topShape, parts.toArray(VoxelShape[]::new)));
62+
int key = getShapeKey(axis, connectedN, connectedP);
63+
SHAPES[key] = VoxelShapes.union(topShape, parts.toArray(VoxelShape[]::new));
6664
}
6765
}
6866
}
@@ -106,11 +104,11 @@ private BlockState updateConnections(BlockView world, BlockPos pos, BlockState s
106104

107105
@Override
108106
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
109-
return SHAPES.get(getShapeKey(state.get(AXIS), state.get(CONNECTED_N), state.get(CONNECTED_P)));
107+
return SHAPES[getShapeKey(state.get(AXIS), state.get(CONNECTED_N), state.get(CONNECTED_P))];
110108
}
111109

112-
private static byte getShapeKey(Direction.Axis axis, boolean connectedN, boolean connectedP) {
113-
return (byte) ((axis == Direction.Axis.X ? 1 : 0) << 2 | (connectedN ? 1 : 0) << 1 | (connectedP ? 1 : 0));
110+
private static int getShapeKey(Direction.Axis axis, boolean connectedN, boolean connectedP) {
111+
return (axis == Direction.Axis.X ? 1 : 0) << 2 | (connectedN ? 1 : 0) << 1 | (connectedP ? 1 : 0);
114112
}
115113

116114
@Override

common/src/main/java/juuxel/adorn/block/CopperPipeBlock.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package juuxel.adorn.block;
22

3-
import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap;
4-
import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap;
53
import juuxel.adorn.lib.AdornTags;
64
import juuxel.adorn.util.Shapes;
75
import net.minecraft.block.Block;
@@ -35,7 +33,7 @@ public class CopperPipeBlock extends Block implements Waterloggable, BlockWithDe
3533
public static final BooleanProperty WATERLOGGED = Properties.WATERLOGGED;
3634

3735
private static final String DESCRIPTION_KEY = "block.adorn.copper_pipe.description";
38-
private static final Byte2ObjectMap<VoxelShape> SHAPES = new Byte2ObjectOpenHashMap<>();
36+
private static final VoxelShape[] SHAPES = new VoxelShape[64];
3937

4038
static {
4139
var center = createCuboidShape(6.0, 6.0, 6.0, 10.0, 10.0, 10.0);
@@ -92,7 +90,7 @@ public class CopperPipeBlock extends Block implements Waterloggable, BlockWithDe
9290
if (up) shape = VoxelShapes.union(shape, pipes.get(Direction.UP));
9391
if (down) shape = VoxelShapes.union(shape, pipes.get(Direction.DOWN));
9492

95-
SHAPES.put(getShapeKey(north, east, south, west, up, down), shape);
93+
SHAPES[getShapeKey(north, east, south, west, up, down)] = shape;
9694
}
9795
}
9896
}
@@ -112,18 +110,18 @@ public String getDescriptionKey() {
112110

113111
@Override
114112
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
115-
return SHAPES.get(getShapeKey(state.get(NORTH), state.get(EAST), state.get(SOUTH), state.get(WEST), state.get(UP), state.get(DOWN)));
113+
return SHAPES[getShapeKey(state.get(NORTH), state.get(EAST), state.get(SOUTH), state.get(WEST), state.get(UP), state.get(DOWN))];
116114
}
117115

118-
private static byte getShapeKey(boolean north, boolean east, boolean south, boolean west, boolean up, boolean down) {
116+
private static int getShapeKey(boolean north, boolean east, boolean south, boolean west, boolean up, boolean down) {
119117
int northB = north ? 1 : 0;
120118
int eastB = east ? 1 : 0;
121119
int southB = south ? 1 : 0;
122120
int westB = west ? 1 : 0;
123121
int upB = up ? 1 : 0;
124122
int downB = down ? 1 : 0;
125123

126-
return (byte) (northB << 5 | eastB << 4 | southB << 3 | westB << 2 | upB << 1 | downB);
124+
return northB << 5 | eastB << 4 | southB << 3 | westB << 2 | upB << 1 | downB;
127125
}
128126

129127
@Override

common/src/main/java/juuxel/adorn/block/SofaBlock.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package juuxel.adorn.block;
22

3-
import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap;
4-
import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap;
53
import juuxel.adorn.block.property.FrontConnection;
64
import juuxel.adorn.block.variant.BlockVariant;
75
import juuxel.adorn.lib.AdornStats;
@@ -52,8 +50,8 @@ public class SofaBlock extends SeatBlock implements Waterloggable, SneakClickHan
5250
public static final EnumProperty<FrontConnection> FRONT_CONNECTION = EnumProperty.of("front", FrontConnection.class);
5351
public static final BooleanProperty WATERLOGGED = Properties.WATERLOGGED;
5452

55-
private static final Byte2ObjectMap<VoxelShape> OUTLINE_SHAPE_MAP = buildShapeMap(false);
56-
private static final Byte2ObjectMap<VoxelShape> COLLISION_SHAPE_MAP = buildShapeMap(true);
53+
private static final VoxelShape[] OUTLINE_SHAPES = buildShapes(false);
54+
private static final VoxelShape[] COLLISION_SHAPES = buildShapes(true);
5755
private static final String DESCRIPTION_KEY = "block.adorn.sofa.description";
5856

5957
public SofaBlock(BlockVariant variant) {
@@ -168,26 +166,26 @@ private BlockState updateConnections(BlockState state, WorldAccess world, BlockP
168166

169167
@Override
170168
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
171-
return OUTLINE_SHAPE_MAP.get(
169+
return OUTLINE_SHAPES[
172170
getShapeKey(
173171
state.get(FACING),
174172
state.get(CONNECTED_LEFT),
175173
state.get(CONNECTED_RIGHT),
176174
state.get(FRONT_CONNECTION)
177175
)
178-
);
176+
];
179177
}
180178

181179
@Override
182180
public VoxelShape getCollisionShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
183-
return COLLISION_SHAPE_MAP.get(
181+
return COLLISION_SHAPES[
184182
getShapeKey(
185183
state.get(FACING),
186184
state.get(CONNECTED_LEFT),
187185
state.get(CONNECTED_RIGHT),
188186
state.get(FRONT_CONNECTION)
189187
)
190-
);
188+
];
191189
}
192190

193191
@Override
@@ -210,7 +208,7 @@ public double getSittingOffset(World world, BlockState state, BlockPos pos) {
210208
return 0.4375; // 7/16
211209
}
212210

213-
private static Byte2ObjectMap<VoxelShape> buildShapeMap(boolean thin) {
211+
private static VoxelShape[] buildShapes(boolean thin) {
214212
var bottom = createCuboidShape(0.0, 2.0, 0.0, 16.0, 7.0, 16.0);
215213
var leftArms = Shapes.buildShapeRotations(5, 7, 13, 16, 13, 16);
216214
var rightArms = Shapes.buildShapeRotations(5, 7, 0, 16, 13, 3);
@@ -220,7 +218,7 @@ private static Byte2ObjectMap<VoxelShape> buildShapeMap(boolean thin) {
220218
var leftCorners = Shapes.buildShapeRotations(5, 7, 11, 16, 16, 16);
221219
var rightCorners = Shapes.buildShapeRotations(5, 7, 0, 16, 16, 5);
222220
var booleans = new boolean[] { true, false };
223-
Byte2ObjectMap<VoxelShape> result = new Byte2ObjectOpenHashMap<>();
221+
var result = new VoxelShape[48];
224222
for (var facing : FACING.getValues()) {
225223
for (var left : booleans) {
226224
for (var right : booleans) {
@@ -240,18 +238,18 @@ private static Byte2ObjectMap<VoxelShape> buildShapeMap(boolean thin) {
240238
case RIGHT -> parts.add(rightCorners.get(facing));
241239
}
242240

243-
var key = getShapeKey(facing, left, right, front);
241+
int key = getShapeKey(facing, left, right, front);
244242
var shape = VoxelShapes.union(bottom, parts.toArray(VoxelShape[]::new));
245-
result.put(key, shape);
243+
result[key] = shape;
246244
}
247245
}
248246
}
249247
}
250248
return result;
251249
}
252250

253-
private static byte getShapeKey(Direction facing, boolean left, boolean right, FrontConnection front) {
254-
return (byte) (facing.getHorizontal() << 5 | (left ? 1 : 0) << 3 | (right ? 1 : 0) << 2 | front.ordinal());
251+
private static int getShapeKey(Direction facing, boolean left, boolean right, FrontConnection front) {
252+
return front.ordinal() << 4 | (left ? 1 : 0) << 3 | (right ? 1 : 0) << 2 | facing.getHorizontal();
255253
}
256254

257255
public static @Nullable Direction getSleepingDirection(BlockView world, BlockPos pos) {

common/src/main/java/juuxel/adorn/block/TableBlock.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package juuxel.adorn.block;
22

3-
import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap;
4-
import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap;
53
import juuxel.adorn.block.variant.BlockVariant;
64
import net.minecraft.block.BlockState;
75
import net.minecraft.util.Identifier;
@@ -15,7 +13,7 @@
1513

1614
public final class TableBlock extends AbstractTableBlock implements BlockWithDescription {
1715
private static final String DESCRIPTION_KEY = "block.adorn.table.description";
18-
private static final Byte2ObjectMap<VoxelShape> SHAPES = new Byte2ObjectOpenHashMap<>();
16+
private static final VoxelShape[] SHAPES = new VoxelShape[32];
1917

2018
static {
2119
var topShape = createCuboidShape(0.0, 14.0, 0.0, 16.0, 16.0, 16.0);
@@ -32,7 +30,7 @@ public final class TableBlock extends AbstractTableBlock implements BlockWithDes
3230
for (var hasCarpet : booleans) {
3331
var key = getShapeKey(north, east, south, west, hasCarpet);
3432
var shape = makeShape(north, east, south, west, hasCarpet, topShape, legX0Z0, legX1Z0, legX0Z1, legX1Z1);
35-
SHAPES.put(key, shape);
33+
SHAPES[key] = shape;
3634
}
3735
}
3836
}
@@ -69,8 +67,8 @@ protected boolean canConnectTo(BlockState state, Direction sideOfSelf) {
6967
}
7068

7169
@Override
72-
protected VoxelShape getShapeForKey(byte key) {
73-
return SHAPES.get(key);
70+
protected VoxelShape getShapeForKey(int key) {
71+
return SHAPES[key];
7472
}
7573

7674
private static VoxelShape makeShape(

0 commit comments

Comments
 (0)