private static final Map<String, ServerLevel> worlds = new HashMap<>();
@SubscribeEvent
public static void onServerStarting(ServerStartingEvent event) {
CommandDispatcher<CommandSourceStack> dispatcher = event.getServer().getCommands().getDispatcher();
dispatcher.register(Commands.literal("createworld").then(Commands.argument("worldName", StringArgumentType.string()).executes(context -> createWorld(context.getSource(), StringArgumentType.getString(context, "worldName")))));
dispatcher.register(Commands.literal("joinworld").then(Commands.argument("worldName", StringArgumentType.string()).executes(context -> joinWorld(context.getSource(), StringArgumentType.getString(context, "worldName")))));
}
private static int createWorld(CommandSourceStack source, String worldName) {
MinecraftServer server = source.getServer();
ServerLevel existingWorld = worlds.get(worldName);
if (existingWorld != null) {
source.sendSuccess(() -> new net.minecraft.network.chat.Component("Мир " + worldName + " уже существует"), false);
return 0;
}
ServerLevel newWorld = server.createLevel(worldName, Level.OVERWORLD, server.getWorldData().getGenerator(), server.getWorldData().isHardcore());
if (newWorld != null) {
worlds.put(worldName, newWorld);
source.sendSuccess(() -> new net.minecraft.network.chat.Component("Мир " + worldName + " создан!"), false);
return 1;
}
source.sendSuccess(() -> new net.minecraft.network.chat.Component("Ошибка при создание мира " + worldName), false);
return 0;
}
private static int joinWorld(CommandSourceStack source, String worldName) {
ServerLevel world = worlds.get(worldName);
if (world != null) {
source.getPlayerOrException().changeDimension(world);
source.sendSuccess(() -> new net.minecraft.network.chat.Component("Вход в мир " + worldName + "!"), false);
return 1;
}
source.sendSuccess(() -> new net.minecraft.network.chat.Component("Мир " + worldName + " не существует"), false);
return 0;
}