Skip to content

Commit 30a983e

Browse files
DrexHDMattiDragon
authored andcommitted
Add player setup functions
1 parent ac48854 commit 30a983e

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

src/main/java/io/github/mattidragon/demobox/DemoBoxCommand.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ public static void register() {
7373
private static RequiredArgumentBuilder<ServerCommandSource, Identifier> buildArgTree(CommandHandler handler) {
7474
return argument("template", IdentifierArgumentType.identifier())
7575
.suggests(STRUCTURE_SUGGESTION_PROVIDER)
76-
.executes(context -> handler.execute(context, IdentifierArgumentType.getIdentifier(context, "template"), new Vec3d(0.5, 2, 0.5), List.of()))
76+
.executes(context -> handler.execute(context, IdentifierArgumentType.getIdentifier(context, "template"), new Vec3d(0.5, 2, 0.5), List.of(), List.of()))
7777
.then(argument("pos", Vec3ArgumentType.vec3())
78-
.executes(context -> handler.execute(context, IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), List.of()))
78+
.executes(context -> handler.execute(context, IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), List.of(), List.of()))
7979
.then(argument("setupFunction", CommandFunctionArgumentType.commandFunction())
8080
.suggests(FunctionCommand.SUGGESTION_PROVIDER)
81-
.executes(context -> handler.execute(context, IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), CommandFunctionArgumentType.getFunctions(context, "setupFunction")))));
81+
.executes(context -> handler.execute(context, IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), CommandFunctionArgumentType.getFunctions(context, "setupFunction"), List.of()))
82+
.then(
83+
argument("playerFunction", CommandFunctionArgumentType.commandFunction())
84+
.suggests(FunctionCommand.SUGGESTION_PROVIDER)
85+
.executes(context -> handler.execute(context, IdentifierArgumentType.getIdentifier(context, "template"), Vec3ArgumentType.getVec3(context, "pos"), CommandFunctionArgumentType.getFunctions(context, "setupFunction"), CommandFunctionArgumentType.getFunctions(context, "playerFunction"))))));
8286
}
8387

8488
// Joinked from GameCommand because brigadier can't deal with childless redirects
@@ -98,7 +102,7 @@ private static int executeLeave(CommandContext<ServerCommandSource> context) thr
98102
return Command.SINGLE_SUCCESS;
99103
}
100104

101-
private static int executeSign(CommandContext<ServerCommandSource> context, Identifier structure, Vec3d pos, Collection<CommandFunction<ServerCommandSource>> functions) throws CommandSyntaxException {
105+
private static int executeSign(CommandContext<ServerCommandSource> context, Identifier structure, Vec3d pos, Collection<CommandFunction<ServerCommandSource>> functions, Collection<CommandFunction<ServerCommandSource>> playerFunctions) throws CommandSyntaxException {
102106
var signPos = BlockPosArgumentType.getLoadedBlockPos(context, "signPos");
103107
var source = context.getSource();
104108
var block = source.getWorld().getBlockState(signPos).getBlock();
@@ -163,11 +167,11 @@ private static String getCommandEnd(CommandContext<ServerCommandSource> context)
163167
return context.getInput().substring(mainNode.getRange().getStart());
164168
}
165169

166-
private static int executeOpen(CommandContext<ServerCommandSource> context, Identifier structure, Vec3d pos, Collection<CommandFunction<ServerCommandSource>> functions) throws CommandSyntaxException {
170+
private static int executeOpen(CommandContext<ServerCommandSource> context, Identifier structure, Vec3d pos, Collection<CommandFunction<ServerCommandSource>> functions, Collection<CommandFunction<ServerCommandSource>> playerFunctions) throws CommandSyntaxException {
167171
var source = context.getSource();
168172
var player = source.getPlayerOrThrow();
169173

170-
DemoBoxGame.open(new DemoBoxGame.Settings(structure, pos, functions.stream().map(CommandFunction::id).toList()))
174+
DemoBoxGame.open(new DemoBoxGame.Settings(structure, pos, functions.stream().map(CommandFunction::id).toList(), playerFunctions.stream().map(CommandFunction::id).toList()))
171175
.thenAcceptAsync(gameSpace -> {
172176
var space = GameSpaceManager.get().byPlayer(player);
173177
if (space != null) space.getPlayers().kick(player);
@@ -182,6 +186,6 @@ private static int executeOpen(CommandContext<ServerCommandSource> context, Iden
182186

183187
@FunctionalInterface
184188
private interface CommandHandler {
185-
int execute(CommandContext<ServerCommandSource> context, Identifier structure, Vec3d pos, Collection<CommandFunction<ServerCommandSource>> functions) throws CommandSyntaxException;
189+
int execute(CommandContext<ServerCommandSource> context, Identifier structure, Vec3d pos, Collection<CommandFunction<ServerCommandSource>> functions, Collection<CommandFunction<ServerCommandSource>> playerFunctions) throws CommandSyntaxException;
186190
}
187191
}

src/main/java/io/github/mattidragon/demobox/DemoBoxGame.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mojang.serialization.MapCodec;
44
import com.mojang.serialization.codecs.RecordCodecBuilder;
55
import net.minecraft.block.Blocks;
6+
import net.minecraft.entity.Entity;
67
import net.minecraft.registry.DynamicRegistryManager;
78
import net.minecraft.registry.entry.RegistryEntry;
89
import net.minecraft.registry.entry.RegistryEntryList;
@@ -84,17 +85,7 @@ private void setup() {
8485
var pos = new BlockPos(size.getX() / -2, 1, size.getZ() / -2);
8586
template.place(world, pos, pos, new StructurePlacementData(), world.random, 0);
8687
});
87-
var server = world.getServer();
88-
var manager = server.getCommandFunctionManager();
89-
for (var id : settings.functions) {
90-
manager.getFunction(id).ifPresentOrElse(
91-
function -> manager.execute(
92-
function,
93-
new ServerCommandSource(server, Vec3d.ZERO, Vec2f.ZERO, world, 2, "DemoBox Setup", Text.literal("DemoBox Setup"), server, null).withSilent()
94-
),
95-
() -> DemoBox.LOGGER.warn("Missing function: {}", id)
96-
);
97-
}
88+
executeFunctions(settings.functions, null);
9889
}
9990

10091
private void onPlayerLeave(ServerPlayerEntity player) {
@@ -108,6 +99,7 @@ private void onPlayerJoin(ServerPlayerEntity player) {
10899
player.sendMessage(Text.translatable("demobox.info.2").formatted(Formatting.WHITE));
109100
player.sendMessage(Text.translatable("demobox.info.3").formatted(Formatting.WHITE));
110101
player.sendMessage(Text.translatable("demobox.info.4").formatted(Formatting.WHITE));
102+
executeFunctions(settings.playerFunctions, player);
111103
}
112104

113105
private Text onJoinMessage(ServerPlayerEntity player, @Nullable Text currentText, Text defaultText) {
@@ -126,6 +118,20 @@ private JoinAcceptorResult onPlayerAccepted(JoinAcceptor joinAcceptor) {
126118
return joinAcceptor.teleport(world, settings.playerPos);
127119
}
128120

121+
private void executeFunctions(List<Identifier> functions, Entity entity) {
122+
var server = world.getServer();
123+
var manager = server.getCommandFunctionManager();
124+
for (var id : functions) {
125+
manager.getFunction(id).ifPresentOrElse(
126+
function -> manager.execute(
127+
function,
128+
new ServerCommandSource(server, Vec3d.ZERO, Vec2f.ZERO, world, 2, "DemoBox Setup", Text.literal("DemoBox Setup"), server, entity).withSilent()
129+
),
130+
() -> DemoBox.LOGGER.warn("Missing function: {}", id)
131+
);
132+
}
133+
}
134+
129135
@NotNull
130136
private static RuntimeWorldConfig createWorldConfig(DynamicRegistryManager registryManager) {
131137
var worldConfig = new RuntimeWorldConfig();
@@ -143,11 +149,12 @@ private static RuntimeWorldConfig createWorldConfig(DynamicRegistryManager regis
143149
return worldConfig;
144150
}
145151

146-
public record Settings(Identifier structureId, Vec3d playerPos, List<Identifier> functions) {
152+
public record Settings(Identifier structureId, Vec3d playerPos, List<Identifier> functions, List<Identifier> playerFunctions) {
147153
public static final MapCodec<Settings> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
148154
Identifier.CODEC.fieldOf("structureId").forGetter(Settings::structureId),
149155
Vec3d.CODEC.fieldOf("playerPos").forGetter(Settings::playerPos),
150-
Identifier.CODEC.listOf().fieldOf("functions").forGetter(Settings::functions)
156+
Identifier.CODEC.listOf().fieldOf("functions").forGetter(Settings::functions),
157+
Identifier.CODEC.listOf().fieldOf("playerFunctions").forGetter(Settings::playerFunctions)
151158
).apply(instance, Settings::new));
152159
}
153160
}

0 commit comments

Comments
 (0)