Skip to content

Commit

Permalink
Add the spawn command
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored and Gegy committed Dec 9, 2023
1 parent fa70fab commit 56764f5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/main/java/xyz/nucleoid/extras/NucleoidExtrasConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import xyz.nucleoid.extras.command.CommandAliasConfig;
import xyz.nucleoid.extras.error.ErrorReportingConfig;
import xyz.nucleoid.extras.integrations.IntegrationsConfig;
import xyz.nucleoid.extras.lobby.LobbySpawnConfig;
import xyz.nucleoid.extras.util.ExtraCodecs;

import java.io.IOException;
Expand All @@ -31,6 +32,7 @@
public record NucleoidExtrasConfig(
boolean sidebar,
Optional<Identifier> gamePortalOpener,
@Nullable LobbySpawnConfig lobbySpawn,
@Nullable IntegrationsConfig integrations,
@Nullable CommandAliasConfig aliases,
@Nullable ChatFilterConfig chatFilter,
Expand All @@ -47,22 +49,23 @@ public record NucleoidExtrasConfig(
instance.group(
Codec.BOOL.optionalFieldOf("sidebar", false).forGetter(NucleoidExtrasConfig::sidebar),
Identifier.CODEC.optionalFieldOf("game_portal_opener").forGetter(NucleoidExtrasConfig::gamePortalOpener),
LobbySpawnConfig.CODEC.optionalFieldOf("lobby_spawn").forGetter(config -> Optional.ofNullable(config.lobbySpawn())),
IntegrationsConfig.CODEC.optionalFieldOf("integrations").forGetter(config -> Optional.ofNullable(config.integrations())),
CommandAliasConfig.CODEC.optionalFieldOf("aliases").forGetter(config -> Optional.ofNullable(config.aliases())),
ChatFilterConfig.CODEC.optionalFieldOf("chat_filter").forGetter(config -> Optional.ofNullable(config.chatFilter())),
MoreCodecs.url("https").optionalFieldOf("contributor_data_url").forGetter(config -> Optional.ofNullable(config.contributorDataUrl())),
ErrorReportingConfig.CODEC.optionalFieldOf("error_reporting", ErrorReportingConfig.NONE).forGetter(NucleoidExtrasConfig::errorReporting),
Codec.BOOL.optionalFieldOf("dev_server", false).forGetter(NucleoidExtrasConfig::devServer),
ExtraCodecs.URI.optionalFieldOf("http_api").forGetter(config -> Optional.ofNullable(config.httpApi()))
).apply(instance, (sidebar, gamePortalOpener, integrations, aliases, filter, contributorDataUrl, errorReporting, devServer, httpApiUrl) ->
new NucleoidExtrasConfig(sidebar, gamePortalOpener, integrations.orElse(null), aliases.orElse(null), filter.orElse(null), contributorDataUrl.orElse(null), errorReporting, devServer, httpApiUrl.orElse(null))
).apply(instance, (sidebar, gamePortalOpener, lobbySpawn, integrations, aliases, filter, contributorDataUrl, errorReporting, devServer, httpApiUrl) ->
new NucleoidExtrasConfig(sidebar, gamePortalOpener, lobbySpawn.orElse(null), integrations.orElse(null), aliases.orElse(null), filter.orElse(null), contributorDataUrl.orElse(null), errorReporting, devServer, httpApiUrl.orElse(null))
)
);

private static NucleoidExtrasConfig instance;

private NucleoidExtrasConfig() {
this(false, Optional.empty(), null, null, null, null, ErrorReportingConfig.NONE, false, null);
this(false, Optional.empty(), null, null, null, null, null, ErrorReportingConfig.NONE, false, null);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static void register() {
}

private static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
SpawnCommand.register(dispatcher);
StatsCommand.register(dispatcher);
}
}
41 changes: 41 additions & 0 deletions src/main/java/xyz/nucleoid/extras/command/SpawnCommand.java
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 src/main/java/xyz/nucleoid/extras/lobby/LobbySpawnConfig.java
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));
}
}
19 changes: 12 additions & 7 deletions src/main/java/xyz/nucleoid/extras/lobby/NEItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.registry.Registry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
Expand Down Expand Up @@ -813,8 +814,8 @@ public static void register() {
UseEntityCallback.EVENT.register(NEItems::onUseEntity);
}

private static boolean tryOfferStack(ServerPlayNetworkHandler handler, Item item, Consumer<ItemStack> consumer) {
var inventory = handler.getPlayer().getInventory();
private static boolean tryOfferStack(ServerPlayerEntity player, Item item, Consumer<ItemStack> consumer) {
var inventory = player.getInventory();

if (inventory.containsAny(Collections.singleton(item))) {
return false;
Expand All @@ -823,21 +824,25 @@ private static boolean tryOfferStack(ServerPlayNetworkHandler handler, Item item
var stack = new ItemStack(item);
consumer.accept(stack);

handler.getPlayer().getInventory().offer(stack, true);
player.getInventory().offer(stack, true);
return true;
}

private static boolean tryOfferStack(ServerPlayNetworkHandler handler, Item item) {
return tryOfferStack(handler, item, stack -> {});
private static boolean tryOfferStack(ServerPlayerEntity player, Item item) {
return tryOfferStack(player, item, stack -> {});
}

private static void onPlayerJoin(ServerPlayNetworkHandler handler, PacketSender packetSender, MinecraftServer server) {
giveLobbyItems(handler.getPlayer());
}

public static void giveLobbyItems(ServerPlayerEntity player) {
var config = NucleoidExtrasConfig.get();

tryOfferStack(handler, TATER_BOX);
tryOfferStack(player, TATER_BOX);

config.gamePortalOpener().ifPresent(gamePortal -> {
tryOfferStack(handler, GAME_PORTAL_OPENER, stack -> {
tryOfferStack(player, GAME_PORTAL_OPENER, stack -> {
GamePortalOpenerItem.setGamePortalId(stack, gamePortal);
});
});
Expand Down

0 comments on commit 56764f5

Please sign in to comment.