Skip to content

Commit

Permalink
Make GameOpenContext store RegistryEntry instead of direct GameConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Nov 21, 2024
1 parent 5e6c20c commit 6159702
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/main/java/xyz/nucleoid/plasmid/api/game/GameOpenContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nucleoid.plasmid.api.game;

import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import xyz.nucleoid.fantasy.RuntimeWorldConfig;
Expand All @@ -19,7 +20,13 @@
* @see GameOpenProcedure
* @see GameType.Open
*/
public record GameOpenContext<C>(MinecraftServer server, GameConfig<C> game) {
public record GameOpenContext<C>(MinecraftServer server, RegistryEntry<GameConfig<C>> gameConfig) {

@Deprecated(forRemoval = true)
public GameOpenContext(MinecraftServer server, GameConfig<C> game) {
this(server, RegistryEntry.of(game));
}

/**
* Creates a {@link GameOpenProcedure} that opens a game given the {@code setup} function.
* <p>
Expand Down Expand Up @@ -60,6 +67,10 @@ public GameOpenProcedure openWithWorld(RuntimeWorldConfig worldConfig, BiConsume
* @return the configuration that this game was opened with
*/
public C config() {
return this.game.config();
return this.gameConfig.value().config();
}

public GameConfig<C> game() {
return this.gameConfig.value();
}
}
13 changes: 10 additions & 3 deletions src/main/java/xyz/nucleoid/plasmid/api/game/config/GameConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ public record GameConfig<C>(
});
public static final Codec<RegistryEntry<GameConfig<?>>> CODEC = RegistryElementCodec.of(GameConfigs.REGISTRY_KEY, DIRECT_CODEC);

public GameOpenProcedure openProcedure(MinecraftServer server) {
var context = new GameOpenContext<C>(server, this);
return this.type.open(context);
public static GameOpenProcedure openProcedure(MinecraftServer server, RegistryEntry<GameConfig<?>> config) {
//noinspection unchecked,rawtypes
var context = new GameOpenContext<>(server, config);
//noinspection unchecked
return config.value().type().open(context);
}

/**
Expand Down Expand Up @@ -141,6 +143,11 @@ private static <C> MapCodec<GameConfig<C>> createTypedCodec(GameType<C> type) {
)));
}

@Deprecated(forRemoval = true)
public GameOpenProcedure openProcedure(MinecraftServer server) {
return openProcedure(server, RegistryEntry.of(this));
}

private record Metadata(
Optional<Text> name,
Optional<Text> shortName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import xyz.nucleoid.plasmid.api.game.GameOpenContext;
import xyz.nucleoid.plasmid.api.game.GameOpenException;
import xyz.nucleoid.plasmid.api.game.GameOpenProcedure;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;

public final class RandomGame {
public static GameOpenProcedure open(GameOpenContext<RandomGameConfig> context) {
Expand All @@ -15,6 +16,6 @@ public static GameOpenProcedure open(GameOpenContext<RandomGameConfig> context)
throw new GameOpenException(Text.translatable("text.plasmid.random.empty_composite_game_config"));
}

return GameOpenProcedure.withOverride(game.value().openProcedure(context.server()), game);
return GameOpenProcedure.withOverride(GameConfig.openProcedure(context.server(), game), game);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public CompletableFuture<GameSpace> open(RegistryEntry<GameConfig<?>> config) {
return CompletableFuture.failedFuture(new RuntimeException("Not initialized yet!"));
}
return CompletableFuture.supplyAsync(
() -> config.value().openProcedure(this.server),
() -> GameConfig.openProcedure(this.server, config),
Util.getMainWorkerExecutor()
).thenApplyAsync(
procedure -> this.addGameSpace(procedure.configOverride() != null ? procedure.configOverride() : config, config, procedure),
Expand Down

0 comments on commit 6159702

Please sign in to comment.