22
33import com .google .common .cache .Cache ;
44import com .google .common .cache .CacheBuilder ;
5- import it .unimi .dsi .fastutil .ints .Int2ObjectLinkedOpenHashMap ;
6- import it .unimi .dsi .fastutil .ints .Int2ObjectMap ;
75import it .unimi .dsi .fastutil .objects .Object2ObjectLinkedOpenHashMap ;
86import lombok .Getter ;
97import muramasa .antimatter .*;
108import muramasa .antimatter .block .AntimatterItemBlock ;
11- import muramasa .antimatter .blockentity .BlockEntityMachine ;
129import muramasa .antimatter .blockentity .pipe .BlockEntityPipe ;
1310import muramasa .antimatter .client .AntimatterModelManager ;
14- import muramasa .antimatter .client .glu .Util ;
1511import muramasa .antimatter .cover .CoverFactory ;
1612import muramasa .antimatter .cover .CoverReplacements ;
1713import muramasa .antimatter .cover .ICover ;
3430import muramasa .antimatter .blockentity .BlockEntityTickable ;
3531import muramasa .antimatter .tool .AntimatterToolType ;
3632import muramasa .antimatter .util .Utils ;
37- import net .minecraft .ChatFormatting ;
3833import net .minecraft .client .gui .screens .Screen ;
3934import net .minecraft .core .BlockPos ;
4035import net .minecraft .core .Direction ;
4136import net .minecraft .nbt .CompoundTag ;
4237import net .minecraft .network .chat .Component ;
4338import net .minecraft .resources .ResourceLocation ;
44- import net .minecraft .world .Containers ;
4539import net .minecraft .world .InteractionHand ;
4640import net .minecraft .world .InteractionResult ;
4741import net .minecraft .world .entity .LivingEntity ;
4842import net .minecraft .world .entity .player .Player ;
49- import net .minecraft .world .item .Item ;
5043import net .minecraft .world .item .ItemStack ;
5144import net .minecraft .world .item .TooltipFlag ;
5245import 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 }
0 commit comments