-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/xyz/nucleoid/extras/command/SpawnCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package xyz.nucleoid.extras.command; | ||
|
||
import com.mojang.brigadier.Command; | ||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import xyz.nucleoid.extras.NucleoidExtrasConfig; | ||
import xyz.nucleoid.extras.lobby.NEItems; | ||
|
||
import static net.minecraft.server.command.CommandManager.literal; | ||
|
||
public class SpawnCommand { | ||
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { | ||
var config = NucleoidExtrasConfig.get(); | ||
|
||
if (config.lobbySpawn() != null) { | ||
dispatcher.register(literal("spawn").executes(SpawnCommand::execute)); | ||
|
||
// https://github.com/Mojang/brigadier/issues/46 | ||
dispatcher.register(literal("lobby").executes(SpawnCommand::execute)); | ||
dispatcher.register(literal("hub").executes(SpawnCommand::execute)); | ||
} | ||
} | ||
|
||
private static int execute(CommandContext<ServerCommandSource> context) throws CommandSyntaxException { | ||
var source = context.getSource(); | ||
|
||
var player = source.getPlayerOrThrow(); | ||
var server = source.getServer(); | ||
|
||
var config = NucleoidExtrasConfig.get().lobbySpawn(); | ||
|
||
config.teleport(player, server.getOverworld()); | ||
config.changeGameMode(player, server.getDefaultGameMode()); | ||
|
||
NEItems.giveLobbyItems(player); | ||
|
||
return Command.SINGLE_SUCCESS; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/xyz/nucleoid/extras/lobby/LobbySpawnConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package xyz.nucleoid.extras.lobby; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
|
||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.math.Vec3d; | ||
import net.minecraft.world.GameMode; | ||
|
||
public record LobbySpawnConfig( | ||
Vec3d pos, | ||
float yaw, | ||
float pitch, | ||
Optional<GameMode> gameMode | ||
) { | ||
public static final Codec<LobbySpawnConfig> CODEC = RecordCodecBuilder.create(instance -> | ||
instance.group( | ||
Vec3d.CODEC.fieldOf("pos").forGetter(LobbySpawnConfig::pos), | ||
Codec.FLOAT.optionalFieldOf("yaw", 0f).forGetter(LobbySpawnConfig::yaw), | ||
Codec.FLOAT.optionalFieldOf("pitch", 0f).forGetter(LobbySpawnConfig::pitch), | ||
GameMode.CODEC.optionalFieldOf("game_mode").forGetter(LobbySpawnConfig::gameMode) | ||
).apply(instance, LobbySpawnConfig::new) | ||
); | ||
|
||
public void teleport(ServerPlayerEntity player, ServerWorld world) { | ||
player.teleport(world, this.pos.getX(), this.pos.getY(), this.pos.getZ(), Collections.emptySet(), this.yaw, this.pitch); | ||
} | ||
|
||
public void changeGameMode(ServerPlayerEntity player, GameMode fallback) { | ||
player.changeGameMode(this.gameMode.orElse(fallback)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters