diff --git a/src/main/java/implementslegend/mod/vaultfaster/interfaces/TileMapperContainer.kt b/src/main/java/implementslegend/mod/vaultfaster/interfaces/TileMapperContainer.kt index 32cd155..920e4db 100644 --- a/src/main/java/implementslegend/mod/vaultfaster/interfaces/TileMapperContainer.kt +++ b/src/main/java/implementslegend/mod/vaultfaster/interfaces/TileMapperContainer.kt @@ -1,11 +1,14 @@ package implementslegend.mod.vaultfaster.interfaces import implementslegend.mod.vaultfaster.TileMapper +import java.util.concurrent.CompletableFuture /* * for flattening purposes * */ interface TileMapperContainer { val tileMapper: TileMapper + val futureTileMapper: CompletableFuture + var hasTileMapperCreationStarted: Boolean fun resetTileMapper() } \ No newline at end of file diff --git a/src/main/java/implementslegend/mod/vaultfaster/mixin/MixinPlacementSettings.java b/src/main/java/implementslegend/mod/vaultfaster/mixin/MixinPlacementSettings.java index dc32935..a5291bb 100644 --- a/src/main/java/implementslegend/mod/vaultfaster/mixin/MixinPlacementSettings.java +++ b/src/main/java/implementslegend/mod/vaultfaster/mixin/MixinPlacementSettings.java @@ -16,8 +16,10 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; -import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; /* @@ -31,40 +33,90 @@ public class MixinPlacementSettings implements ExtendedPlacementSettings { protected List tileProcessors; private TileMapper mapper = new TileMapper(); + CompletableFuture futureTileMapper = new CompletableFuture(); + private AtomicBoolean tileMapperCreationStarted = new AtomicBoolean(); + @NotNull @Override public TileMapper getTileMapper() { - return mapper; + TileMapper privateMapper; + if(!tileMapperCreationStarted.getPlain() && tileMapperCreationStarted.compareAndSet(false,true)){ + privateMapper=new TileMapper(); + tileProcessors.forEach(processor -> { + privateMapper.addProcessor(processor); + }); + futureTileMapper.complete(privateMapper); + }else { + try { + privateMapper=futureTileMapper.get(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } + + return privateMapper; } @Override public void resetTileMapper(){ + if(getHasTileMapperCreationStarted()){ + try { + futureTileMapper.get(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + + futureTileMapper=new CompletableFuture(); + setHasTileMapperCreationStarted(false); + } + /* var privateMapper = new TileMapper(); tileProcessors.forEach(processor -> { privateMapper.addProcessor(processor); }); - mapper=privateMapper; + mapper=privateMapper;*/ } @Inject(method = "addProcessor(Liskallia/vault/core/world/processor/Processor;)Liskallia/vault/core/world/template/PlacementSettings;",at = @At("HEAD"),remap = false) private void registerProcessor(Processor processor, CallbackInfoReturnable cir){ - if(processor instanceof TileProcessor) mapper.addProcessor((TileProcessor) processor); + if(processor instanceof TileProcessor) resetTileMapper();//mapper.addProcessor((TileProcessor) processor); } @Inject(method = "copy",locals = LocalCapture.CAPTURE_FAILHARD,at = @At(value = "FIELD", opcode = Opcodes.GETFIELD, target = "Liskallia/vault/core/world/template/PlacementSettings;flags:I"/*,target = "Liskallia/vault/core/world/template/PlacementSettings;(Liskallia/vault/core/world/processor/ProcessorContext;)V"*/),remap = false) private void copyMapper(CallbackInfoReturnable cir, PlacementSettings nw){ - + if (getHasTileMapperCreationStarted()) { + ((TileMapperContainer) nw).getFutureTileMapper().complete(getTileMapper()); + ((TileMapperContainer) nw).setHasTileMapperCreationStarted(true); + } + /* var tileMapper = ((TileMapperContainer) nw).getTileMapper(); tileProcessors.forEach(processor -> { tileMapper.addProcessor(processor); - }); + });*/ } @Override public void addProcessorAtBegining(@NotNull TileProcessor tileProcessor) { + resetTileMapper(); tileProcessors.add(0,tileProcessor); - mapper.addProcessor( tileProcessor, true); + //mapper.addProcessor( tileProcessor, true);* + } + + @NotNull + @Override + public CompletableFuture getFutureTileMapper() { + return futureTileMapper; + } + + @Override + public boolean getHasTileMapperCreationStarted() { + return tileMapperCreationStarted.getPlain() || tileMapperCreationStarted.get(); + } + + @Override + public void setHasTileMapperCreationStarted(boolean b) { + tileMapperCreationStarted.set(b); } }