Skip to content

Commit

Permalink
Merge remote-tracking branch 'webhead-repo/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/net/cytonic/cytosis/Cytosis.java
#	src/main/java/net/cytonic/cytosis/commands/GamemodeCommand.java
#	src/main/java/net/cytonic/cytosis/data/Database.java
#	src/main/java/net/cytonic/cytosis/events/ServerEventListeners.java
  • Loading branch information
webhead1104 committed May 9, 2024
2 parents 75de446 + 5ac0505 commit 908c69f
Show file tree
Hide file tree
Showing 15 changed files with 501 additions and 133 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
implementation("com.github.Minestom", "Minestom", "6b8a4e4cc9") // minstom itself
implementation("com.google.code.gson:gson:2.10.1") // serializing
implementation("org.slf4j:slf4j-api:2.0.13") // logging
implementation("net.kyori:adventure-text-minimessage:4.16.0")// better components
implementation("net.kyori:adventure-text-minimessage:4.17.0")// better components
implementation("mysql:mysql-connector-java:8.0.33") //mysql connector
compileOnly("org.projectlombok:lombok:1.18.32") // lombok
annotationProcessor("org.projectlombok:lombok:1.18.32") // lombok
Expand Down
128 changes: 62 additions & 66 deletions src/main/java/net/cytonic/cytosis/Cytosis.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.cytonic.cytosis;

import lombok.Getter;
import net.cytonic.cytosis.commands.CommandHandler;
import net.cytonic.cytosis.config.CytosisSettings;
import net.cytonic.cytosis.data.DatabaseManager;
Expand All @@ -8,6 +9,7 @@
import net.cytonic.cytosis.files.FileManager;
import net.cytonic.cytosis.logging.Logger;
import net.cytonic.cytosis.messaging.MessagingManager;
import net.cytonic.cytosis.ranks.RankManager;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandManager;
import net.minestom.server.command.ConsoleSender;
Expand All @@ -20,23 +22,39 @@
import net.minestom.server.instance.block.Block;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.permission.Permission;

import java.util.*;


@Getter
public class Cytosis {

// manager stuff
private static MinecraftServer MINECRAFT_SERVER;
private static InstanceManager INSTANCE_MANAGER;
private static InstanceContainer DEFAULT_INSTANCE;
private static EventHandler EVENT_HANDLER;
private static ConnectionManager CONNECTION_MANAGER;
private static CommandManager COMMAND_MANAGER;
private static CommandHandler COMMAND_HANDLER;
private static FileManager FILE_MANAGER;
private static DatabaseManager DATABASE_MANAGER;
private static MessagingManager MESSAGE_MANAGER;
private static ConsoleSender CONSOLE_SENDER;
private static int SERVER_PORT;
@Getter
private static MinecraftServer minecraftServer;
@Getter
private static InstanceManager instanceManager;
@Getter
private static InstanceContainer defaultInstance;
@Getter
private static EventHandler eventHandler;
@Getter
private static ConnectionManager connectionManager;
@Getter
private static CommandManager commandManager;
@Getter
private static CommandHandler commandHandler;
@Getter
private static FileManager fileManager;
@Getter
private static DatabaseManager databaseManager;
@Getter
private static MessagingManager messagingManager;
@Getter
private static ConsoleSender consoleSender;
@Getter
private static RankManager rankManager;

private static List<String> FLAGS;

public static void main(String[] args) {
Expand All @@ -45,36 +63,37 @@ public static void main(String[] args) {
long start = System.currentTimeMillis();
// Initialize the server
Logger.info("Starting server.");
MINECRAFT_SERVER = MinecraftServer.init();
minecraftServer = MinecraftServer.init();
MinecraftServer.setBrandName("Cytosis");

Logger.info("Starting instance manager.");
INSTANCE_MANAGER = MinecraftServer.getInstanceManager();
instanceManager = MinecraftServer.getInstanceManager();

Logger.info("Starting connection manager.");
CONNECTION_MANAGER = MinecraftServer.getConnectionManager();
connectionManager = MinecraftServer.getConnectionManager();


Logger.info("Starting manager.");
DATABASE_MANAGER = new DatabaseManager();
databaseManager = new DatabaseManager();

// Commands
Logger.info("Starting command manager.");
COMMAND_MANAGER = MinecraftServer.getCommandManager();
commandManager = MinecraftServer.getCommandManager();

Logger.info("Setting console command sender.");
CONSOLE_SENDER = COMMAND_MANAGER.getConsoleSender();
CONSOLE_SENDER.addPermission(new Permission("*"));
consoleSender = commandManager.getConsoleSender();
consoleSender.addPermission(new Permission("*"));

// instances
Logger.info("Creating instance container");
DEFAULT_INSTANCE = INSTANCE_MANAGER.createInstanceContainer();
defaultInstance = instanceManager.createInstanceContainer();

Logger.info("Creating file manager");
FILE_MANAGER = new FileManager();
fileManager = new FileManager();

// Everything after this point depends on config contents
Logger.info("Initializing file manager");
FILE_MANAGER.init().whenComplete((_, throwable) -> {
fileManager.init().whenComplete((_, throwable) -> {
if (throwable != null) {
Logger.error("An error occurred whilst initializing the file manager!", throwable);
} else {
Expand All @@ -91,29 +110,9 @@ public static void main(String[] args) {
});
}

public static EventHandler getEventHandler() {
return EVENT_HANDLER;
}

public static InstanceContainer getDefaultInstance() {
return DEFAULT_INSTANCE;
}

public static ConnectionManager getConnectionManager() {
return CONNECTION_MANAGER;
}

public static CommandManager getCommandManager() {
return COMMAND_MANAGER;
}

public static DatabaseManager getDatabaseManager() {
return DATABASE_MANAGER;
}

public static Set<Player> getOnlinePlayers() {
Set<Player> players = new HashSet<>();
INSTANCE_MANAGER.getInstances().forEach(instance -> players.addAll(instance.getPlayers()));
instanceManager.getInstances().forEach(instance -> players.addAll(instance.getPlayers()));
return players;
}

Expand All @@ -140,10 +139,6 @@ public static void deopPlayer(Player player) {
player.removePermission("*"); // remove every permission
}

public static ConsoleSender getConsoleSender() {
return CONSOLE_SENDER;
}

public static void mojangAuth() {
Logger.info("Initializing Mojang Authentication");
MojangAuth.init(); //VERY IMPORTANT! (This is online mode!)
Expand All @@ -152,46 +147,47 @@ public static void mojangAuth() {
public static void completeNonEssentialTasks(long start) {
// basic world generator
Logger.info("Generating basic world");
DEFAULT_INSTANCE.setGenerator(unit -> unit.modifier().fillHeight(0, 1, Block.WHITE_STAINED_GLASS));
DEFAULT_INSTANCE.setChunkSupplier(LightingChunk::new);
defaultInstance.setGenerator(unit -> unit.modifier().fillHeight(0, 1, Block.WHITE_STAINED_GLASS));
defaultInstance.setChunkSupplier(LightingChunk::new);

Logger.info("Setting up event handlers");
EVENT_HANDLER = new EventHandler(MinecraftServer.getGlobalEventHandler());
EVENT_HANDLER.init();
eventHandler = new EventHandler(MinecraftServer.getGlobalEventHandler());
eventHandler.init();

Logger.info("Initializing server events");
ServerEventListeners.initServerEvents();

if (CytosisSettings.DATABASE_ENABLED) {
Logger.info("Initializing database");
DATABASE_MANAGER.setupDatabase();
}
Logger.info("Initializing database");
databaseManager.setupDatabase();

MinecraftServer.getSchedulerManager().buildShutdownTask(() -> DATABASE_MANAGER.shutdown());
MinecraftServer.getSchedulerManager().buildShutdownTask(() -> databaseManager.shutdown());

Logger.info("Initializing server commands");
COMMAND_HANDLER = new CommandHandler();
COMMAND_HANDLER.setupConsole();
COMMAND_HANDLER.registerCystosisCommands();
commandHandler = new CommandHandler();
commandHandler.setupConsole();
commandHandler.registerCystosisCommands();

MESSAGE_MANAGER = new MessagingManager();
MESSAGE_MANAGER.initialize().whenComplete((_, throwable) -> {
messagingManager = new MessagingManager();
messagingManager.initialize().whenComplete((_, throwable) -> {
if (throwable != null) {
Logger.error("An error occurred whilst initializing the messaging manager!", throwable);
} else {
Logger.info("Messaging manager initialized!");
}
});

SERVER_PORT = CytosisSettings.SERVER_PORT;

Logger.info("Initializing Rank Manager");
rankManager = new RankManager();
rankManager.init();

// Start the server
Logger.info(STR."Server started on port \{SERVER_PORT}");
MINECRAFT_SERVER.start("0.0.0.0", SERVER_PORT);
Logger.info(STR."Server started on port \{CytosisSettings.SERVER_PORT}");
minecraftServer.start("0.0.0.0", CytosisSettings.SERVER_PORT);

long end = System.currentTimeMillis();
Logger.info(STR."Server started in \{end - start}ms!");

if (FLAGS.contains("--ci-test")) {
if (FLAGS.contains("--ci-test")) {
Logger.info("Stopping server due to '--ci-test' flag.");
MinecraftServer.stopCleanly();
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/cytonic/cytosis/DatabaseManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.cytonic.cytosis;

import lombok.Getter;
import net.cytonic.cytosis.data.Database;

@Getter
public class DatabaseManager {

private Database database;

public void shutdown() {
database.disconnect();
}

public void setupDatabase() {
database = new Database();
database.connect();
database.createTables();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.cytonic.cytosis.Cytosis;
import net.minestom.server.command.CommandManager;
import net.minestom.server.entity.Player;

import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -20,7 +22,11 @@ public CommandHandler() {
public void registerCystosisCommands() {
CommandManager cm = Cytosis.getCommandManager();
cm.register(new GamemodeCommand());
cm.register(new OperatorCommand());
cm.register(new RankCommand());
}

public void recalculateCommands(Player player) {
player.sendPacket(Cytosis.getCommandManager().createDeclareCommandsPacket(player));
}

public void setupConsole() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ public class GamemodeCommand extends Command {

public GamemodeCommand() {
super("gamemode", "gm");
setCondition((sender, commandString) -> sender.hasPermission("cytosis.commands.gamemode"));
setDefaultExecutor((sender, context) -> sender.sendMessage(Component.text("You must specify a gamemode!", NamedTextColor.RED)));
setCondition((sender, _) -> sender.hasPermission("cytosis.commands.gamemode"));
setDefaultExecutor((sender, _) -> sender.sendMessage(Component.text("You must specify a gamemode!", NamedTextColor.RED)));

// using a gamemode as an argument
var gameModeArgument = ArgumentType.Enum("gamemode", GameMode.class).setFormat(ArgumentEnum.Format.LOWER_CASED);
gameModeArgument.setCallback((sender, exception) -> sender.sendMessage(STR."The gamemode \{exception.getInput()} is invalid!"));

var shorthand = ArgumentType.Word("shorthand").from("c", "s", "sv", "a", "0", "1", "2", "3");
shorthand.setSuggestionCallback((sender, context, suggestion) -> {
shorthand.setSuggestionCallback((_, _, suggestion) -> {
suggestion.addEntry(new SuggestionEntry("c", Component.text("Represents the Creative gamemode")));
suggestion.addEntry(new SuggestionEntry("s", Component.text("Represents the Spectator gamemode")));
suggestion.addEntry(new SuggestionEntry("sv", Component.text("Represents the Survival gamemode")));
suggestion.addEntry(new SuggestionEntry("a", Component.text("Represents the Adventure gamemode")));
});
shorthand.setCallback((sender, exception) -> sender.sendMessage(Component.text(STR."The shorthand '\{exception.getInput()}' is invalid!", NamedTextColor.RED)));

addSyntax((sender, context) -> {
if (sender instanceof final Player player) {
final GameMode gameMode = context.get(gameModeArgument);
Expand Down
42 changes: 0 additions & 42 deletions src/main/java/net/cytonic/cytosis/commands/OperatorCommand.java

This file was deleted.

Loading

0 comments on commit 908c69f

Please sign in to comment.