Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ranks #41

Merged
merged 30 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
591a56d
Register custom event
Foxikle May 8, 2024
693d8c1
Reformatting
Foxikle May 8, 2024
2cf604a
Delete operator command as it isn't used anymore
Foxikle May 9, 2024
ae57950
Add events for handling ranks
Foxikle May 9, 2024
78158a6
Create events for rank activities
Foxikle May 9, 2024
bfda3cd
Create Rank enum
Foxikle May 9, 2024
9c98f32
Create command for changing ranks
Foxikle May 9, 2024
0261cdd
Create class for handling interactions with ranks
Foxikle May 9, 2024
0fa6d4c
Formatting
Foxikle May 9, 2024
14a9754
Register new events with the event handler
Foxikle May 9, 2024
b24cab4
Tidying up
Foxikle May 9, 2024
0b503af
Add rank database queries
Foxikle May 9, 2024
f593537
Just a bunch of stuff
Foxikle May 9, 2024
3268582
add method to recalculate command permissions, add rank command
Foxikle May 9, 2024
6d41403
Register custom event
Foxikle May 8, 2024
18e9093
Reformatting
Foxikle May 8, 2024
899420b
Delete operator command as it isn't used anymore
Foxikle May 9, 2024
617efba
Add events for handling ranks
Foxikle May 9, 2024
bbba5df
Create events for rank activities
Foxikle May 9, 2024
93c8275
Create Rank enum
Foxikle May 9, 2024
0a993ab
Create command for changing ranks
Foxikle May 9, 2024
947f412
Create class for handling interactions with ranks
Foxikle May 9, 2024
fabf3cf
Formatting
Foxikle May 9, 2024
9ab0fac
Register new events with the event handler
Foxikle May 9, 2024
cb60115
rebasing
Foxikle May 9, 2024
e9511c9
Add rank database queries
Foxikle May 9, 2024
7152716
Just a bunch of stuff
Foxikle May 9, 2024
c27c30e
add method to recalculate command permissions, add rank command
Foxikle May 9, 2024
addbf27
Merge remote-tracking branch 'origin/feat/ranks' into feat/ranks
Foxikle May 9, 2024
2bf454d
Whoops fix compilation error, use getter
Foxikle May 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 56 additions & 62 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,24 +22,38 @@
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;

Expand All @@ -47,37 +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 @@ -94,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 @@ -143,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 @@ -155,40 +147,42 @@ 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();

Logger.info("Initializing database");
DATABASE_MANAGER.setupDatabase();
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!");
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,15 +13,15 @@ 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")));
Expand Down
42 changes: 0 additions & 42 deletions src/main/java/net/cytonic/cytosis/commands/OperatorCommand.java

This file was deleted.

Loading