Skip to content

Add onPlayerJoin functions and permission level fields #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 1.20.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

23 changes: 18 additions & 5 deletions src/main/java/io/github/mattidragon/demobox/DemoBoxCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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"))))))))));

});
}

Expand All @@ -74,10 +82,15 @@ private static int leaveGame(CommandContext<ServerCommandSource> context) throws
return Command.SINGLE_SUCCESS;
}

private static int execute(ServerCommandSource source, Identifier structure, Vec3d pos, Collection<CommandFunction<ServerCommandSource>> functions) throws CommandSyntaxException {
private static int execute(ServerCommandSource source, Identifier structure, Vec3d pos, Collection<CommandFunction<ServerCommandSource>> functions, Collection<CommandFunction<ServerCommandSource>> onJoinFunctions, CommandContext<ServerCommandSource> 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<CommandFunction<ServerCommandSource>> functions, Collection<CommandFunction<ServerCommandSource>> 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);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/io/github/mattidragon/demobox/DemoBoxGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -116,11 +123,13 @@ private static RuntimeWorldConfig createWorldConfig(DynamicRegistryManager regis
return worldConfig;
}

public record Settings(Identifier structureId, Vec3d playerPos, List<Identifier> functions) {
public record Settings(Identifier structureId, Vec3d playerPos, List<Identifier> functions, List<Identifier> onJoinFunctions, int permissionLevel) {
public static final Codec<Settings> 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));
}
}
3 changes: 0 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
"icon": "assets/demobox/icon.png",
"environment": "*",
"entrypoints": {
"client": [
"io.github.mattidragon.demobox.client.DemoBoxClient"
],
"main": [
"io.github.mattidragon.demobox.DemoBox"
]
Expand Down