diff --git a/src/client/java/io/github/mattidragon/demobox/client/DemoBoxClient.java b/src/client/java/io/github/mattidragon/demobox/client/DemoBoxClient.java deleted file mode 100644 index 1030516..0000000 --- a/src/client/java/io/github/mattidragon/demobox/client/DemoBoxClient.java +++ /dev/null @@ -1,10 +0,0 @@ -package io.github.mattidragon.demobox.client; - -import net.fabricmc.api.ClientModInitializer; - -public class DemoBoxClient implements ClientModInitializer { - @Override - public void onInitializeClient() { - - } -} diff --git a/src/main/java/io/github/mattidragon/demobox/DemoBoxCommand.java b/src/main/java/io/github/mattidragon/demobox/DemoBoxCommand.java index 63bd160..23d48dd 100644 --- a/src/main/java/io/github/mattidragon/demobox/DemoBoxCommand.java +++ b/src/main/java/io/github/mattidragon/demobox/DemoBoxCommand.java @@ -1,8 +1,10 @@ package io.github.mattidragon.demobox; import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.IntegerSuggestion; import com.mojang.brigadier.suggestion.SuggestionProvider; import me.lucko.fabric.api.permissions.v0.Permissions; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; @@ -48,12 +50,18 @@ public static void register() { .requires(source -> Permissions.check(source, "demobox.open", 2)) .then(argument("template", IdentifierArgumentType.identifier()) .suggests(STRUCTURE_SUGGESTION_PROVIDER) - .executes(context -> execute(context.getSource(), IdentifierArgumentType.getIdentifier(context, "template"), new Vec3d(0.5, 2, 0.5), List.of())) + .executes(context -> execute(context.getSource(), IdentifierArgumentType.getIdentifier(context, "template"), new Vec3d(0.5, 2, 0.5), List.of(), List.of(), context)) .then(argument("pos", Vec3ArgumentType.vec3()) - .executes(context -> execute(context.getSource(), IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), List.of())) + .executes(context -> execute(context.getSource(), IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), List.of(), List.of(), context)) .then(argument("setupFunction", CommandFunctionArgumentType.commandFunction()) .suggests(FunctionCommand.SUGGESTION_PROVIDER) - .executes(context -> execute(context.getSource(), IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), CommandFunctionArgumentType.getFunctions(context, "setupFunction")))))))); + .executes(context -> execute(context.getSource(), IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), CommandFunctionArgumentType.getFunctions(context, "setupFunction"), List.of(), context)) + .then(argument("onJoinFunction", CommandFunctionArgumentType.commandFunction()) + .suggests(FunctionCommand.SUGGESTION_PROVIDER) + .executes(context -> execute(context.getSource(), IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), CommandFunctionArgumentType.getFunctions(context, "setupFunction"), CommandFunctionArgumentType.getFunctions(context, "onJoinFunction"), context)) + .then(argument("permissionLevel", IntegerArgumentType.integer(0, 4)) + .executes(context -> execute(context.getSource(), IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), CommandFunctionArgumentType.getFunctions(context, "setupFunction"), CommandFunctionArgumentType.getFunctions(context, "onJoinFunction"), IntegerArgumentType.getInteger(context, "permissionLevel")))))))))); + }); } @@ -74,10 +82,15 @@ private static int leaveGame(CommandContext context) throws return Command.SINGLE_SUCCESS; } - private static int execute(ServerCommandSource source, Identifier structure, Vec3d pos, Collection> functions) throws CommandSyntaxException { + private static int execute(ServerCommandSource source, Identifier structure, Vec3d pos, Collection> functions, Collection> onJoinFunctions, CommandContext context) throws CommandSyntaxException { + var player = context.getSource().getPlayerOrThrow(); + var server = player.getServer(); + return execute(source, structure, pos, functions, onJoinFunctions, server.getPermissionLevel(player.getGameProfile())); + } + private static int execute(ServerCommandSource source, Identifier structure, Vec3d pos, Collection> functions, Collection> onJoinFunctions, int permissionLevel) throws CommandSyntaxException { var player = source.getPlayerOrThrow(); - DemoBoxGame.open(new DemoBoxGame.Settings(structure, pos, functions.stream().map(CommandFunction::id).toList())) + DemoBoxGame.open(new DemoBoxGame.Settings(structure, pos, functions.stream().map(CommandFunction::id).toList(), onJoinFunctions.stream().map(CommandFunction::id).toList(), permissionLevel)) .thenAcceptAsync(gameSpace -> { var space = GameSpaceManager.get().byPlayer(player); if (space != null) space.getPlayers().kick(player); diff --git a/src/main/java/io/github/mattidragon/demobox/DemoBoxGame.java b/src/main/java/io/github/mattidragon/demobox/DemoBoxGame.java index 9389740..bf65321 100644 --- a/src/main/java/io/github/mattidragon/demobox/DemoBoxGame.java +++ b/src/main/java/io/github/mattidragon/demobox/DemoBoxGame.java @@ -92,6 +92,13 @@ private void onPlayerJoin(ServerPlayerEntity player) { player.sendMessage(Text.translatable("demobox.info.2").formatted(Formatting.WHITE)); player.sendMessage(Text.translatable("demobox.info.3").formatted(Formatting.WHITE)); player.sendMessage(Text.translatable("demobox.info.4").formatted(Formatting.WHITE)); + + var server = player.getServer(); + var manager = server.getCommandFunctionManager(); + for (var id : settings.onJoinFunctions) { + manager.getFunction(id).ifPresentOrElse(function -> manager.execute(function, new ServerCommandSource(server, player.getPos(), player.getRotationClient(), world, settings.permissionLevel(), player.getNameForScoreboard(), player.getDisplayName(), server, player).withSilent()), + () -> DemoBox.LOGGER.warn("Missing function: {}", id)); + } } @NotNull @@ -116,11 +123,13 @@ private static RuntimeWorldConfig createWorldConfig(DynamicRegistryManager regis return worldConfig; } - public record Settings(Identifier structureId, Vec3d playerPos, List functions) { + public record Settings(Identifier structureId, Vec3d playerPos, List functions, List onJoinFunctions, int permissionLevel) { public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( Identifier.CODEC.fieldOf("structureId").forGetter(Settings::structureId), Vec3d.CODEC.fieldOf("playerPos").forGetter(Settings::playerPos), - Identifier.CODEC.listOf().fieldOf("functions").forGetter(Settings::functions) + Identifier.CODEC.listOf().fieldOf("functions").forGetter(Settings::functions), + Identifier.CODEC.listOf().fieldOf("onJoinFunctions").forGetter(Settings::onJoinFunctions), + Codec.INT.fieldOf("permissionLevel").forGetter(Settings::permissionLevel) ).apply(instance, Settings::new)); } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 8197f3a..9183717 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -15,9 +15,6 @@ "icon": "assets/demobox/icon.png", "environment": "*", "entrypoints": { - "client": [ - "io.github.mattidragon.demobox.client.DemoBoxClient" - ], "main": [ "io.github.mattidragon.demobox.DemoBox" ]