diff --git a/common/src/main/java/net/infumia/frame/Frame.java b/common/src/main/java/net/infumia/frame/Frame.java index b735eaf..45048f8 100644 --- a/common/src/main/java/net/infumia/frame/Frame.java +++ b/common/src/main/java/net/infumia/frame/Frame.java @@ -67,6 +67,8 @@ static Frame create( void register(); + void register(@NotNull Consumer instanceConfigurer); + void unregister(); @NotNull diff --git a/common/src/main/java/net/infumia/frame/pipeline/context/PipelineContextManager.java b/common/src/main/java/net/infumia/frame/pipeline/context/PipelineContextManager.java index 5816c8c..5c55707 100644 --- a/common/src/main/java/net/infumia/frame/pipeline/context/PipelineContextManager.java +++ b/common/src/main/java/net/infumia/frame/pipeline/context/PipelineContextManager.java @@ -1,8 +1,10 @@ package net.infumia.frame.pipeline.context; import java.util.Collection; +import java.util.function.Consumer; import net.infumia.frame.Frame; import net.infumia.frame.pipeline.PipelineContext; +import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder; import net.infumia.frame.view.View; import org.jetbrains.annotations.NotNull; @@ -18,6 +20,9 @@ interface ViewCreated extends PipelineContextManager { interface ViewRegistered extends PipelineContextManager { @NotNull Collection registeredViews(); + + @NotNull + Consumer instanceConfigurer(); } interface ListenerRegistered extends PipelineContextManager {} diff --git a/common/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorManager.java b/common/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorManager.java index 21cae82..5448623 100644 --- a/common/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorManager.java +++ b/common/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorManager.java @@ -2,9 +2,11 @@ import java.util.Collection; import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; import net.infumia.frame.pipeline.context.PipelineContextManager; import net.infumia.frame.service.ConsumerService; import net.infumia.frame.service.Implementation; +import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder; import net.infumia.frame.view.View; import org.jetbrains.annotations.NotNull; @@ -16,7 +18,8 @@ CompletableFuture> executeViewCreated( @NotNull CompletableFuture> executeViewRegistered( - @NotNull Collection registeredViews + @NotNull Collection registeredViews, + @NotNull Consumer instanceConfigurer ); @NotNull diff --git a/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageFactory.java b/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageFactory.java index 206ed10..a718dd5 100644 --- a/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageFactory.java +++ b/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageFactory.java @@ -12,9 +12,6 @@ static TypedKeyStorageFactory create() { @NotNull TypedKeyStorage create(@NotNull Map, Object> base); - @NotNull - TypedKeyStorageImmutableBuilder createImmutableBuilder(); - @NotNull TypedKeyStorageImmutableBuilder createImmutableBuilder(@NotNull Map, Object> base); } diff --git a/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageFactoryImpl.java b/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageFactoryImpl.java index 121c24e..2e3a87f 100644 --- a/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageFactoryImpl.java +++ b/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageFactoryImpl.java @@ -1,6 +1,5 @@ package net.infumia.frame.typedkey; -import java.util.HashMap; import java.util.Map; import org.jetbrains.annotations.NotNull; @@ -14,12 +13,6 @@ public TypedKeyStorage create(@NotNull final Map, Object> base) { return new TypedKeyStorageImpl(base); } - @NotNull - @Override - public TypedKeyStorageImmutableBuilder createImmutableBuilder() { - return this.createImmutableBuilder(new HashMap<>()); - } - @NotNull @Override public TypedKeyStorageImmutableBuilder createImmutableBuilder( diff --git a/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageImmutable.java b/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageImmutable.java index 628bd2c..b58a760 100644 --- a/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageImmutable.java +++ b/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageImmutable.java @@ -38,4 +38,8 @@ public interface TypedKeyStorageImmutable { @NotNull @UnmodifiableView Collection, Object>> entries(); + + @NotNull + @UnmodifiableView + Map, Object> map(); } diff --git a/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageImmutableImpl.java b/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageImmutableImpl.java index b239569..dc9a875 100644 --- a/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageImmutableImpl.java +++ b/common/src/main/java/net/infumia/frame/typedkey/TypedKeyStorageImmutableImpl.java @@ -6,6 +6,7 @@ import net.infumia.frame.util.Preconditions; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.UnmodifiableView; class TypedKeyStorageImmutableImpl implements TypedKeyStorageImmutable { @@ -87,6 +88,13 @@ public Collection, Object>> entries() { return Collections.unmodifiableCollection(this.map.entrySet()); } + @NotNull + @Override + @UnmodifiableView + public Map, Object> map() { + return Collections.unmodifiableMap(this.map); + } + @Override public String toString() { return "TypedKeyStorageImmutableImpl{" + "map=" + this.map + '}'; diff --git a/core/src/main/java/net/infumia/frame/FrameImpl.java b/core/src/main/java/net/infumia/frame/FrameImpl.java index 08e1688..44e05bd 100644 --- a/core/src/main/java/net/infumia/frame/FrameImpl.java +++ b/core/src/main/java/net/infumia/frame/FrameImpl.java @@ -125,12 +125,19 @@ public void inventoryCreator(@NotNull final InventoryCreator inventoryCreator) { @Override public void register() { + this.register(builder -> {}); + } + + @Override + public void register( + @NotNull final Consumer instanceConfigurer + ) { Preconditions.state( !this.registered.get(), "This inventory manager is already registered! #register() method cannot be called twice!" ); this.registered.set(true); - this.executeViewCreation(this.unregisteredViews) + this.executeViewCreation(this.unregisteredViews, instanceConfigurer) .thenCompose(views -> { this.registeredViews.clear(); this.registeredViews.putAll( @@ -210,7 +217,7 @@ public Frame with(@NotNull final Class viewClass) { return CompletableFuture.completedFuture(null); } final TypedKeyStorageImmutableBuilder builder = - this.storageFactory.createImmutableBuilder(); + this.storageFactory.createImmutableBuilder(new HashMap<>()); initialDataConfigurer.accept(builder); return CompletableFutureExtensions.logError( ((ViewEventHandler) view).simulateOpen(players, builder.build()), @@ -278,10 +285,11 @@ private void intoUnregisteredViews(@NotNull final Class viewClass) { @NotNull private CompletableFuture> executeViewCreation( - @NotNull final Collection> views + @NotNull final Collection> views, + @NotNull final Consumer instanceConfigurer ) { - return this.pipelines.executeViewCreated(views).thenCompose( - this.pipelines::executeViewRegistered + return this.pipelines.executeViewCreated(views).thenCompose(instances -> + this.pipelines.executeViewRegistered(instances, instanceConfigurer) ); } diff --git a/core/src/main/java/net/infumia/frame/pipeline/context/PipelineContextManagers.java b/core/src/main/java/net/infumia/frame/pipeline/context/PipelineContextManagers.java index 5438c77..0985381 100644 --- a/core/src/main/java/net/infumia/frame/pipeline/context/PipelineContextManagers.java +++ b/core/src/main/java/net/infumia/frame/pipeline/context/PipelineContextManagers.java @@ -1,7 +1,9 @@ package net.infumia.frame.pipeline.context; import java.util.Collection; +import java.util.function.Consumer; import net.infumia.frame.Frame; +import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder; import net.infumia.frame.view.View; import org.jetbrains.annotations.NotNull; @@ -36,13 +38,16 @@ final class ViewRegistered implements PipelineContextManager.ViewRegistered { private final Frame manager; private final Collection registeredViews; + private final Consumer storageConfigurer; public ViewRegistered( @NotNull final Frame manager, - @NotNull final Collection registeredViews + @NotNull final Collection registeredViews, + @NotNull final Consumer storageConfigurer ) { this.manager = manager; this.registeredViews = registeredViews; + this.storageConfigurer = storageConfigurer; } @NotNull @@ -56,6 +61,12 @@ public Frame frame() { public Collection registeredViews() { return this.registeredViews; } + + @NotNull + @Override + public Consumer instanceConfigurer() { + return this.storageConfigurer; + } } final class ListenerRegistered implements PipelineContextManager.ListenerRegistered { diff --git a/core/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorManagerImpl.java b/core/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorManagerImpl.java index 6d9fcf9..f859e01 100644 --- a/core/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorManagerImpl.java +++ b/core/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorManagerImpl.java @@ -3,12 +3,14 @@ import java.util.Collection; import java.util.Collections; import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; import net.infumia.frame.Frame; import net.infumia.frame.pipeline.context.PipelineContextManager; import net.infumia.frame.pipeline.context.PipelineContextManagers; import net.infumia.frame.pipeline.holder.PipelineHolderManager; import net.infumia.frame.service.ConsumerService; import net.infumia.frame.service.Implementation; +import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder; import net.infumia.frame.view.View; import org.jetbrains.annotations.NotNull; @@ -38,13 +40,15 @@ public CompletableFuture> executeViewCreated( @NotNull @Override public CompletableFuture> executeViewRegistered( - @NotNull final Collection registeredViews + @NotNull final Collection registeredViews, + @NotNull final Consumer instanceConfigurer ) { return this.pipelines.viewRegistered() .completeWith( new PipelineContextManagers.ViewRegistered( this.manager, - Collections.unmodifiableCollection(registeredViews) + Collections.unmodifiableCollection(registeredViews), + instanceConfigurer ) ); } diff --git a/core/src/main/java/net/infumia/frame/pipeline/service/manager/ServiceViewRegistered.java b/core/src/main/java/net/infumia/frame/pipeline/service/manager/ServiceViewRegistered.java index 604428a..6ae8a08 100644 --- a/core/src/main/java/net/infumia/frame/pipeline/service/manager/ServiceViewRegistered.java +++ b/core/src/main/java/net/infumia/frame/pipeline/service/manager/ServiceViewRegistered.java @@ -2,8 +2,8 @@ import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import net.infumia.frame.Frame; import net.infumia.frame.config.ViewConfigBuilderRich; @@ -12,6 +12,8 @@ import net.infumia.frame.pipeline.PipelineService; import net.infumia.frame.pipeline.context.PipelineContextManager; import net.infumia.frame.state.StateRegistry; +import net.infumia.frame.typedkey.TypedKeyStorage; +import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder; import net.infumia.frame.view.View; import net.infumia.frame.view.ViewEventHandler; import net.infumia.frame.view.ViewImpl; @@ -43,13 +45,16 @@ public CompletableFuture> handle( .registeredViews() .stream() .map(instance -> { + final TypedKeyStorageImmutableBuilder builder = frame + .storageFactory() + .createImmutableBuilder(new HashMap<>()); + ctx.instanceConfigurer().accept(builder); + final TypedKeyStorage instances = frame + .storageFactory() + .create(builder.build().map()); final View view = new ViewImpl( new ContextInitImpl( - new ContextImpl( - frame, - frame.storageFactory().create(new ConcurrentHashMap<>()), - new StateRegistry(frame.logger()) - ), + new ContextImpl(frame, instances, new StateRegistry(frame.logger())), ViewConfigBuilderRich.create() ), instance