Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4292f3c

Browse files
committed
Merge branch 'shape-refactor' into dev-1.18
2 parents 0ce6f1a + 90b17e2 commit 4292f3c

File tree

4 files changed

+79
-72
lines changed

4 files changed

+79
-72
lines changed

common/src/main/java/muramasa/antimatter/Ref.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ public class Ref {
4848
* Global Data
4949
**/
5050
public static final long U = 18144000, U2 = U / 2, U3 = U / 3, U4 = U / 4, U5 = U / 5, U6 = U / 6, U7 = U / 7, U8 = U / 8, U9 = U / 9, U10 = U / 10, U11 = U / 11, U12 = U / 12, U13 = U / 13, U14 = U / 14, U15 = U / 15, U16 = U / 16, U17 = U / 17, U18 = U / 18, U20 = U / 20, U24 = U / 24, U25 = U / 25, U32 = U / 32, U36 = U / 36, U40 = U / 40, U48 = U / 48, U50 = U / 50, U64 = U / 64, U72 = U / 72, U80 = U / 80, U96 = U / 96, U100 = U / 100, U128 = U / 128, U144 = U / 144, U192 = U / 192, U200 = U / 200, U240 = U / 240, U256 = U / 256, U288 = U / 288, U480 = U / 480, U500 = U / 500, U512 = U / 512, U1000 = U / 1000, U9000 = U/9000, U1440 = U / 1440, U81000 = U/81000;
51-
//TODO change this to long and add values up to tier 15(not for gti itself, but for modpacks and compat)
5251
public static final long[] V = new long[]{8, 32, 128, 512, 2048, 8192, 32768, 131_072, 524_288, 2_097_152, 8_388_608, 35_544_432, 134_217_728, 536_870_912, 2_147_483_648L, 8_589_934_592L};
53-
public static final String[] VN = new String[]{"ULV", "LV", "MV", "HV", "EV", "IV", "LuV", "ZPM", "UV", "UHV", "UEV", "UIV", "UMV", "UXV", "MAX", "∞"}; //TODO: Schedule to change? ZPM rename? Tier decisions?
52+
public static final String[] VN = new String[]{"ULV", "LV", "MV", "HV", "EV", "IV", "LuV", "ZPM", "UV", "UHV", "UEV", "UIV", "UMV", "UXV", "MAX", "∞"};
5453
public static final Direction[] DIRS = Direction.values();
5554
/** The first 32 Bits */
5655
public static final int[] B = {1<<0,1<<1,1<<2,1<<3,1<<4,1<<5,1<<6,1<<7,1<<8,1<<9,1<<10,1<<11,1<<12,1<<13,1<<14,1<<15,1<<16,1<<17,1<<18,1<<19,1<<20,1<<21,1<<22,1<<23,1<<24,1<<25,1<<26,1<<27,1<<28,1<<29,1<<30,1<<31};

common/src/main/java/muramasa/antimatter/cover/ICover.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package muramasa.antimatter.cover;
22

3+
import com.google.common.cache.Cache;
4+
import com.google.common.cache.CacheBuilder;
35
import muramasa.antimatter.Ref;
46
import muramasa.antimatter.blockentity.BlockEntityMachine;
57
import muramasa.antimatter.capability.ICoverHandler;
@@ -15,6 +17,7 @@
1517
import muramasa.antimatter.texture.Texture;
1618
import muramasa.antimatter.tool.AntimatterToolType;
1719
import muramasa.antimatter.util.AntimatterPlatformUtils;
20+
import muramasa.antimatter.util.Dir;
1821
import muramasa.antimatter.util.Utils;
1922
import net.minecraft.client.renderer.block.model.BakedQuad;
2023
import net.minecraft.core.Direction;
@@ -32,17 +35,22 @@
3235
import net.minecraft.world.item.ItemStack;
3336
import net.minecraft.world.level.block.state.BlockState;
3437
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
38+
import net.minecraft.world.phys.shapes.Shapes;
39+
import net.minecraft.world.phys.shapes.VoxelShape;
3540
import org.jetbrains.annotations.NotNull;
3641
import org.jetbrains.annotations.Nullable;
3742
import tesseract.api.item.ExtendedItemContainer;
3843

3944
import java.util.List;
4045
import java.util.Map;
4146
import java.util.Objects;
47+
import java.util.concurrent.ExecutionException;
48+
import java.util.concurrent.TimeUnit;
4249
import java.util.function.BiConsumer;
4350

4451
public interface ICover extends ITextureProvider, IDynamicModelProvider, MenuProvider, IGuiHandler {
4552
ResourceLocation PIPE_COVER_MODEL = new ResourceLocation(Ref.ID, "block/cover/cover_pipe");
53+
Cache<Direction, VoxelShape> DEFAULT_SHAPES = CacheBuilder.newBuilder().expireAfterAccess(30, TimeUnit.MINUTES).build();
4654

4755
default void onPlace() {
4856

@@ -222,6 +230,25 @@ default List<BakedQuad> transformQuads(BlockState state, List<BakedQuad> quads)
222230
return quads;
223231
}
224232

233+
default VoxelShape getShape(Direction side) throws ExecutionException {
234+
return DEFAULT_SHAPES.get(side, () -> makeShapes(side));
235+
}
236+
237+
default VoxelShape makeShapes(Direction side) {
238+
return switch (side){
239+
case DOWN -> Shapes.box(0, 0, 0, 1, 0.0625f, 1);
240+
case UP -> Shapes.box(0, 0.9375, 0, 1, 1, 1);
241+
case NORTH -> Shapes.box(0, 0, 0, 1, 1, 0.0625f);
242+
case SOUTH -> Shapes.box(0, 0, 0.9375, 1, 1, 1);
243+
case WEST -> Shapes.box(0, 0, 0, 0.0625f, 1, 1);
244+
case EAST -> Shapes.box(0.9375, 0, 0, 1, 1, 1);
245+
};
246+
}
247+
248+
default ResourceLocation getIdForCache(){
249+
return new ResourceLocation(Ref.ID, "cover");
250+
}
251+
225252
default boolean isEmpty() {
226253
return this == empty;
227254
}

common/src/main/java/muramasa/antimatter/pipe/BlockPipe.java

Lines changed: 47 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
import com.google.common.cache.Cache;
44
import com.google.common.cache.CacheBuilder;
5-
import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
6-
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
75
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
86
import lombok.Getter;
97
import muramasa.antimatter.*;
108
import muramasa.antimatter.block.AntimatterItemBlock;
11-
import muramasa.antimatter.blockentity.BlockEntityMachine;
129
import muramasa.antimatter.blockentity.pipe.BlockEntityPipe;
1310
import muramasa.antimatter.client.AntimatterModelManager;
14-
import muramasa.antimatter.client.glu.Util;
1511
import muramasa.antimatter.cover.CoverFactory;
1612
import muramasa.antimatter.cover.CoverReplacements;
1713
import muramasa.antimatter.cover.ICover;
@@ -34,19 +30,16 @@
3430
import muramasa.antimatter.blockentity.BlockEntityTickable;
3531
import muramasa.antimatter.tool.AntimatterToolType;
3632
import muramasa.antimatter.util.Utils;
37-
import net.minecraft.ChatFormatting;
3833
import net.minecraft.client.gui.screens.Screen;
3934
import net.minecraft.core.BlockPos;
4035
import net.minecraft.core.Direction;
4136
import net.minecraft.nbt.CompoundTag;
4237
import net.minecraft.network.chat.Component;
4338
import net.minecraft.resources.ResourceLocation;
44-
import net.minecraft.world.Containers;
4539
import net.minecraft.world.InteractionHand;
4640
import net.minecraft.world.InteractionResult;
4741
import net.minecraft.world.entity.LivingEntity;
4842
import net.minecraft.world.entity.player.Player;
49-
import net.minecraft.world.item.Item;
5043
import net.minecraft.world.item.ItemStack;
5144
import net.minecraft.world.item.TooltipFlag;
5245
import net.minecraft.world.item.context.BlockPlaceContext;
@@ -94,9 +87,8 @@ public abstract class BlockPipe<T extends PipeType<T>> extends BlockDynamic impl
9487
protected Texture overlay;
9588
protected Texture[] faces;
9689

97-
public static long ticksTotal;
98-
99-
protected static Map<PipeSize, Cache<Integer, VoxelShape>> shapes = new Object2ObjectLinkedOpenHashMap<>();
90+
protected static Map<PipeSize, Cache<Integer, VoxelShape>> pipeShapes = new Object2ObjectLinkedOpenHashMap<>();
91+
protected static Map<PipeSize, Cache<PipeShapeKey, VoxelShape>> shapes = new Object2ObjectLinkedOpenHashMap<>();
10092

10193
public static final BooleanProperty TICKING = BooleanProperty.create("ticking");
10294

@@ -105,9 +97,9 @@ public BlockPipe(String prefix, T type, PipeSize size, int modelId) {
10597
}
10698

10799
public BlockPipe(String prefix, T type, PipeSize size, int modelId, Properties properties) {
108-
super(type.domain, prefix + "_" + size.getId(), size.ordinal() < 6 ? properties.noOcclusion() : properties);
109-
shapes.computeIfAbsent(size, s -> CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build());
110-
100+
super(type.domain, prefix + "_" + size.getId(), size.ordinal() < 6 ? properties.noOcclusion().dynamicShape() : properties);
101+
pipeShapes.computeIfAbsent(size, s -> CacheBuilder.newBuilder().expireAfterAccess(10, TimeUnit.MINUTES).build());
102+
shapes.computeIfAbsent(size, s -> CacheBuilder.newBuilder().expireAfterAccess(3, TimeUnit.MINUTES).maximumSize(1000).build());
111103
this.type = type;
112104
this.size = size;
113105
side = new Texture(type.getMaterial().getSet().getDomain(), type.getMaterial().getSet().getPath() + "/pipe/pipe_side");
@@ -124,21 +116,9 @@ public BlockPipe(String prefix, T type, PipeSize size, int modelId, Properties p
124116
AntimatterAPI.register(BlockPipe.class, this);
125117
registerDefaultState(getStateDefinition().any().setValue(WATERLOGGED, false).setValue(TICKING, false));
126118
this.modelId = modelId;
127-
//long time = System.nanoTime();
128-
//buildShapes();
129-
//time = System.nanoTime() - time;
130-
//ticksTotal += time;
131119
}
132120

133121

134-
private void buildShapes() {
135-
if (size.ordinal() > 5) return;
136-
//recursiveShapeBuild(0, (short) 0);
137-
/*if (!getShapes().containsKey(0)) {
138-
getShapes().get(0, Shapes.create(size.getAABB()));
139-
}*/
140-
}
141-
142122
@Override
143123
public void appendHoverText(ItemStack stack, @Nullable BlockGetter level, List<Component> tooltip, TooltipFlag flag) {
144124
if (stack.getTag() != null && stack.getTag().contains("covers")){
@@ -194,20 +174,12 @@ public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState n
194174
}
195175
}
196176

197-
protected Cache<Integer, VoxelShape> getShapes(){
198-
return shapes.get(size);
177+
public Cache<Integer, VoxelShape> getPipeShapes(){
178+
return pipeShapes.get(size);
199179
}
200180

201-
private void recursiveShapeBuild(int index, short acc) {
202-
if (index > 11) {
203-
/*if (!getShapes().(acc)) {
204-
getShapes().put(acc, makeShapes(acc));
205-
}*/
206-
return;
207-
}
208-
short which = (short) (acc | (1 << index));
209-
recursiveShapeBuild(index + 1, which);
210-
recursiveShapeBuild(index + 1, acc);
181+
protected Cache<PipeShapeKey, VoxelShape> getShapes(){
182+
return shapes.get(size);
211183
}
212184

213185
private VoxelShape makeShapes(short which) {
@@ -225,18 +197,6 @@ private VoxelShape makeShapes(short which) {
225197
shape = Shapes.or(shape, Shapes.box(0, 0.4375 - offset, 0.4375 - offset, 0.4375 - offset, 0.5625 + offset, 0.5625 + offset));
226198
if ((which & (1 << 5)) > 0)
227199
shape = Shapes.or(shape, Shapes.box(0.5625 + offset, 0.4375 - offset, 0.4375 - offset, 1, 0.5625 + offset, 0.5625 + offset));
228-
if ((which & (1 << 6)) > 0)
229-
shape = Shapes.or(shape, Shapes.box(0, 0, 0, 1, 0.0625f, 1));
230-
if ((which & (1 << 7)) > 0)
231-
shape = Shapes.or(shape, Shapes.box(0, 0.9375, 0, 1, 1, 1));
232-
if ((which & (1 << 8)) > 0)
233-
shape = Shapes.or(shape, Shapes.box(0, 0, 0, 1, 1, 0.0625f));
234-
if ((which & (1 << 9)) > 0)
235-
shape = Shapes.or(shape, Shapes.box(0, 0, 0.9375, 1, 1, 1));
236-
if ((which & (1 << 10)) > 0)
237-
shape = Shapes.or(shape, Shapes.box(0, 0, 0, 0.0625f, 1, 1));
238-
if ((which & (1 << 11)) > 0)
239-
shape = Shapes.or(shape, Shapes.box(0.9375, 0, 0, 1, 1, 1));
240200
return shape;
241201
}
242202

@@ -420,10 +380,13 @@ public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, Co
420380
return Shapes.block();
421381
}
422382
}
423-
int config = getShapeConfig(state, world, new BlockPos.MutableBlockPos(pos.getX(), pos.getY(), pos.getZ()), pos);
383+
BlockEntityPipe<?> tile = getTilePipe(world, pos);
384+
if (tile == null) {
385+
return Shapes.block();
386+
}
424387
VoxelShape shape = null;
425388
try {
426-
shape = getShapes().get(config, () -> makeShapes((short) config));
389+
shape = getOrCreateShape(tile);
427390
} catch (ExecutionException e) {
428391
Antimatter.LOGGER.error(e);
429392
}
@@ -440,20 +403,44 @@ protected static BlockEntityPipe<?> getTilePipe(BlockGetter world, BlockPos pos)
440403
return tile instanceof BlockEntityPipe ? (BlockEntityPipe<?>) tile : null;
441404
}
442405

443-
public int getShapeConfig(BlockState state, BlockGetter world, BlockPos.MutableBlockPos mut, BlockPos pos){
406+
public VoxelShape getOrCreateShape(BlockEntityPipe<?> tile) throws ExecutionException {
407+
int config = getShapeConfig(tile);
408+
String[] coverIds = new String[6];
409+
ICover[] covers = new ICover[6];
410+
boolean allEmpty = true;
411+
if (tile.coverHandler.isPresent()){
412+
var coverHandler = tile.coverHandler.get();
413+
for (Direction s : Direction.values()) {
414+
ICover cover = coverHandler.get(s);
415+
covers[s.get3DDataValue()] = cover;
416+
if (cover.isEmpty()) {
417+
coverIds[s.get3DDataValue()] = "";
418+
} else {
419+
coverIds[s.get3DDataValue()] = cover.getIdForCache().toString();
420+
allEmpty = false;
421+
}
422+
}
423+
}
424+
if (allEmpty) return getPipeShapes().get(config, () -> makeShapes((short) config));
425+
PipeShapeKey key = new PipeShapeKey(config, coverIds[0], coverIds[1], coverIds[2], coverIds[3], coverIds[4], coverIds[5]);
426+
return getShapes().get(key, () -> {
427+
VoxelShape core = getPipeShapes().get(config, () -> makeShapes((short) config));
428+
for (ICover cover : covers) {
429+
if (!cover.isEmpty()) {
430+
core = Shapes.or(core, cover.getShape(cover.side()));
431+
}
432+
}
433+
return core;
434+
});
435+
}
436+
437+
public int getShapeConfig(BlockEntityPipe<?> tile) {
444438
int ct = 0;
445-
BlockEntityPipe<?> tile = getTilePipe(world, pos);
446439
if (tile != null) {
447440
for (int s = 0; s < 6; s++) {
448441
if (tile.canConnect(s)) {
449442
ct += 1 << s;
450443
}
451-
if (tile.coverHandler.isPresent()){
452-
var coverHandler = tile.coverHandler.get();
453-
if (!coverHandler.get(Direction.from3DDataValue(s)).isEmpty()){
454-
ct += 1 << (s + 6);
455-
}
456-
}
457444
}
458445
}
459446
return ct;
@@ -464,17 +451,7 @@ public ModelConfig getConfig(BlockState state, BlockGetter world, BlockPos.Mutab
464451
int ct = 0;
465452
BlockEntityPipe<?> tile = getTilePipe(world, pos);
466453
if (tile != null) {
467-
for (int s = 0; s < 6; s++) {
468-
if (tile.canConnect(s)) {
469-
ct += 1 << s;
470-
}
471-
/*if (tile.coverHandler.isPresent()){
472-
var coverHandler = tile.coverHandler.get();
473-
if (!coverHandler.get(Direction.from3DDataValue(s)).isEmpty()){
474-
ct += 1 << (s + 6);
475-
}
476-
}*/
477-
}
454+
ct = getShapeConfig(tile);
478455
}
479456
return config.set(pos, new int[]{getPipeID(ct, 0)});
480457
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package muramasa.antimatter.pipe;
2+
3+
public record PipeShapeKey(int config, String down, String up, String north, String south, String west, String east) {
4+
}

0 commit comments

Comments
 (0)