Skip to content

Commit

Permalink
Load game configs from dynamic registries
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Aug 14, 2023
1 parent 21dca42 commit 71e88c2
Show file tree
Hide file tree
Showing 27 changed files with 197 additions and 437 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ yarn_mappings=1.20.1+build.1
loader_version=0.14.21

#Fabric api
fabric_version=0.83.0+1.20.1
fabric_version=0.87.0+1.20.1

# Mod Properties
mod_version=0.6
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/xyz/nucleoid/plasmid/Plasmid.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.event.registry.DynamicRegistries;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.Registries;
Expand All @@ -15,14 +16,14 @@
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.nucleoid.plasmid.command.*;
import xyz.nucleoid.plasmid.event.GameEvents;
import xyz.nucleoid.plasmid.game.GameType;
import xyz.nucleoid.plasmid.game.composite.RandomGame;
import xyz.nucleoid.plasmid.game.composite.RandomGameConfig;
import xyz.nucleoid.plasmid.game.config.GameConfig;
import xyz.nucleoid.plasmid.game.config.GameConfigs;
import xyz.nucleoid.plasmid.game.event.GameActivityEvents;
import xyz.nucleoid.plasmid.game.manager.GameSpaceManager;
Expand All @@ -32,8 +33,8 @@
import xyz.nucleoid.plasmid.game.portal.game.ConcurrentGamePortalConfig;
import xyz.nucleoid.plasmid.game.portal.game.LegacyOnDemandPortalConfig;
import xyz.nucleoid.plasmid.game.portal.game.NewGamePortalConfig;
import xyz.nucleoid.plasmid.game.portal.menu.*;
import xyz.nucleoid.plasmid.game.portal.game.SingleGamePortalConfig;
import xyz.nucleoid.plasmid.game.portal.menu.*;
import xyz.nucleoid.plasmid.game.world.generator.GameChunkGenerator;

public final class Plasmid implements ModInitializer {
Expand All @@ -43,8 +44,9 @@ public final class Plasmid implements ModInitializer {

@Override
public void onInitialize() {
Registry.register(Registries.CHUNK_GENERATOR, new Identifier(ID, "game"), GameChunkGenerator.CODEC);
DynamicRegistries.register(GameConfigs.REGISTRY_KEY, GameConfig.DIRECT_CODEC);

Registry.register(Registries.CHUNK_GENERATOR, new Identifier(ID, "game"), GameChunkGenerator.CODEC);

GamePortalConfig.register(new Identifier(ID, "single_game"), SingleGamePortalConfig.CODEC);
GamePortalConfig.register(new Identifier(ID, "new_game"), NewGamePortalConfig.CODEC);
Expand All @@ -64,8 +66,7 @@ public void onInitialize() {
}

private void loadData(DynamicRegistryManager registryManager, ResourceManager manager) {
GameConfigs.reload(registryManager, manager);
GamePortalManager.INSTANCE.reload(manager);
GamePortalManager.INSTANCE.reload(registryManager, manager);
}

private void registerCallbacks() {
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
Expand Down Expand Up @@ -121,7 +122,7 @@ private static int openGame(CommandContext<ServerCommandSource> context) throws
protected static int openGame(CommandContext<ServerCommandSource> context, boolean test) throws CommandSyntaxException {
try {
var game = GameConfigArgument.get(context, "game_config");
return openGame(context, game.getSecond(), test);
return openGame(context, game, test);
} catch (CommandSyntaxException e) {
throw e;
} catch (Exception e) {
Expand All @@ -138,13 +139,13 @@ private static int openAnonymousGame(CommandContext<ServerCommandSource> context
protected static int openAnonymousGame(CommandContext<ServerCommandSource> context, boolean test) throws CommandSyntaxException {
try {
var configNbt = NbtCompoundArgumentType.getNbtCompound(context, "game_config_nbt");
var result = GameConfig.CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, context.getSource().getRegistryManager()), configNbt);
var result = GameConfig.DIRECT_CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, context.getSource().getRegistryManager()), configNbt);
if (result.error().isPresent()) {
throw MALFORMED_CONFIG.create(result.error().get());
}

var game = result.result().get();
return openGame(context, game, test);
return openGame(context, RegistryEntry.of(game), test);
} catch (CommandSyntaxException e) {
throw e;
} catch (Exception e) {
Expand All @@ -154,7 +155,7 @@ protected static int openAnonymousGame(CommandContext<ServerCommandSource> conte
}
}

private static int openGame(CommandContext<ServerCommandSource> context, GameConfig<?> config, boolean test) {
private static int openGame(CommandContext<ServerCommandSource> context, RegistryEntry<GameConfig<?>> config, boolean test) {
var source = context.getSource();
var server = source.getServer();

Expand Down Expand Up @@ -414,19 +415,21 @@ private static int stopGameConfirmed(CommandContext<ServerCommandSource> context
}

private static int listGames(CommandContext<ServerCommandSource> context) {
var registry = context.getSource().getRegistryManager().get(GameConfigs.REGISTRY_KEY);
var source = context.getSource();
source.sendFeedback(() -> GameTexts.Command.gameList().formatted(Formatting.BOLD), false);

for (var id : GameConfigs.getKeys()) {
registry.streamEntries().forEach(game -> {
var id = game.registryKey().getValue();
source.sendFeedback(() -> {
String command = "/game open " + id;

var link = GameConfig.name(GameConfigs.get(id)).copy()
var link = GameConfig.name(game).copy()
.setStyle(GameTexts.commandLinkStyle(command));

return GameTexts.Command.listEntry(link);
}, false);
}
});

return Command.SINGLE_SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.mojang.datafixers.util.Pair;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.IdentifierArgumentType;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
Expand All @@ -15,6 +18,7 @@
import xyz.nucleoid.plasmid.game.config.GameConfigs;

import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;

public final class GameConfigArgument {
Expand All @@ -25,24 +29,21 @@ public final class GameConfigArgument {
public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argument(String name) {
return CommandManager.argument(name, IdentifierArgumentType.identifier())
.suggests((ctx, builder) -> {
Iterable<Identifier> candidates = GameConfigs.getKeys().stream()::iterator;
var registry = ctx.getSource().getRegistryManager().get(GameConfigs.REGISTRY_KEY);
var remaining = builder.getRemaining().toLowerCase(Locale.ROOT);

CommandSource.forEachMatching(candidates, remaining, Function.identity(), id -> {
builder.suggest(id.toString(), GameConfig.name(GameConfigs.get(id)));
CommandSource.forEachMatching(registry.getKeys(), remaining, RegistryKey::getValue, key -> {
registry.getEntry(key).ifPresent(entry -> {
builder.suggest(key.getValue().toString(), GameConfig.name(entry));
});
});
return builder.buildFuture();
});
}

public static Pair<Identifier, GameConfig<?>> get(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
var identifier = IdentifierArgumentType.getIdentifier(context, name);

var config = GameConfigs.get(identifier);
if (config == null) {
throw GAME_NOT_FOUND.create(identifier);
}

return new Pair<>(identifier, config);
public static RegistryEntry.Reference<GameConfig<?>> get(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
var key = RegistryKey.of(GameConfigs.REGISTRY_KEY, IdentifierArgumentType.getIdentifier(context, name));
var registry = context.getSource().getRegistryManager().get(GameConfigs.REGISTRY_KEY);
return registry.getEntry(key).orElseThrow(() -> GAME_NOT_FOUND.create(key.getValue()));
}
}
4 changes: 2 additions & 2 deletions src/main/java/xyz/nucleoid/plasmid/command/ui/GameJoinUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ private void changePage(int change) {

private GuiElementBuilder createIconFor(GameSpace gameSpace) {
var sourceConfig = gameSpace.getMetadata().sourceConfig();
var element = GuiElementBuilder.from(sourceConfig.icon().copy()).hideFlags()
var element = GuiElementBuilder.from(sourceConfig.value().icon().copy()).hideFlags()
.setName(GameConfig.name(sourceConfig).copy());

for (var line : sourceConfig.description()) {
for (var line : sourceConfig.value().description()) {
var text = line.copy();

if (line.getStyle().getColor() == null) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/xyz/nucleoid/plasmid/event/GameEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.game.GameActivity;
Expand Down Expand Up @@ -75,7 +76,7 @@ public interface GameSpaceOpened {
* @param game The game and its configuration
* @param gameSpace The {@link GameSpace} the game is running in.
*/
void onGameSpaceOpened(GameConfig<?> game, GameSpace gameSpace);
void onGameSpaceOpened(RegistryEntry<GameConfig<?>> game, GameSpace gameSpace);
}

public interface CreateActivity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nucleoid.plasmid.game;

import net.minecraft.registry.entry.RegistryEntry;
import xyz.nucleoid.plasmid.game.config.GameConfig;

/**
Expand All @@ -8,23 +9,23 @@
* @see GameOpenContext
*/
public interface GameOpenProcedure {
static GameOpenProcedure withOverride(GameOpenProcedure procedure, GameConfig<?> game) {
static GameOpenProcedure withOverride(GameOpenProcedure procedure, RegistryEntry<GameConfig<?>> game) {
return new GameOpenProcedure() {
@Override
public void apply(GameSpace context) {
procedure.apply(context);
}

@Override
public GameConfig<?> configOverride() {
public RegistryEntry<GameConfig<?>> configOverride() {
return game;
}
};
}

void apply(GameSpace gameSpace);

default GameConfig<?> configOverride() {
default RegistryEntry<GameConfig<?>> configOverride() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nucleoid.plasmid.game;

import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.game.config.GameConfig;

Expand All @@ -12,7 +13,7 @@
public record GameSpaceMetadata(
UUID id,
Identifier userId,
GameConfig<?> sourceConfig
RegistryEntry<GameConfig<?>> sourceConfig
) {
/**
* @return the globally unique ID for this {@link GameSpace}
Expand All @@ -36,8 +37,7 @@ public Identifier userId() {
/**
* @return the {@link GameConfig} that was responsible for creating this {@link GameSpace}
*/
@Override
public GameConfig<?> sourceConfig() {
public RegistryEntry<GameConfig<?>> sourceConfig() {
return this.sourceConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static GameWaitingLobby addTo(GameActivity activity, PlayerConfig playerC
activity.listen(GamePlayerEvents.REMOVE, lobby::onRemovePlayer);


lobby.setSidebarLines(sourceConfig.description());
lobby.setSidebarLines(sourceConfig.value().description());
var title = GameConfig.shortName(sourceConfig).copy();

if (title.getStyle().getColor() == null) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package xyz.nucleoid.plasmid.game.composite;

import net.minecraft.text.Text;
import net.minecraft.util.math.random.Random;
import xyz.nucleoid.plasmid.game.GameOpenContext;
import xyz.nucleoid.plasmid.game.GameOpenException;
import xyz.nucleoid.plasmid.game.GameOpenProcedure;

import java.util.Random;

public final class RandomGame {
public static GameOpenProcedure open(GameOpenContext<RandomGameConfig> context) {
var config = context.config();

var game = config.selectGame(new Random());
var game = config.selectGame(Random.createLocal());
if (game == null) {
throw new GameOpenException(Text.translatable("text.plasmid.random.empty_composite_game_config"));
}

return GameOpenProcedure.withOverride(game.openProcedure(context.server()), game);
return GameOpenProcedure.withOverride(game.value().openProcedure(context.server()), game);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.registry.RegistryCodecs;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.util.math.random.Random;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.game.config.GameConfig;
import xyz.nucleoid.plasmid.game.config.GameConfigs;

import java.util.Random;

public record RandomGameConfig(GameListConfig games) {
public static final Codec<RandomGameConfig> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
GameListConfig.CODEC.fieldOf("games").forGetter(config -> config.games)
).apply(instance, RandomGameConfig::new);
});
public record RandomGameConfig(RegistryEntryList<GameConfig<?>> games) {
public static final Codec<RandomGameConfig> CODEC = RecordCodecBuilder.create(i -> i.group(
RegistryCodecs.entryList(GameConfigs.REGISTRY_KEY).fieldOf("games").forGetter(config -> config.games)
).apply(i, RandomGameConfig::new));

@Nullable
public GameConfig<?> selectGame(Random random) {
return this.games.selectGame(random);
public RegistryEntry<GameConfig<?>> selectGame(Random random) {
return this.games.getRandom(random).orElse(null);
}

public boolean isEmpty() {
return this.games.isEmpty();
return this.games.size() == 0;
}
}
Loading

0 comments on commit 71e88c2

Please sign in to comment.