diff --git a/pom.xml b/pom.xml
index e3e8fe5..1dd5956 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
me.finn.unlegitlibrary
unlegitlibrary
- 1.4.2-OpenJDK11
+ 1.5.0-OpenJDK11
11
diff --git a/src/main/java/me/finn/unlegitlibrary/command/Command.java b/src/main/java/me/finn/unlegitlibrary/command/Command.java
new file mode 100644
index 0000000..97cf9d8
--- /dev/null
+++ b/src/main/java/me/finn/unlegitlibrary/command/Command.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
+ *
+ * You are unauthorized to remove this copyright.
+ * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
+ * See LICENSE-File if exists
+ */
+
+package me.finn.unlegitlibrary.command;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public abstract class Command {
+
+ public final CommandManager commandManager;
+ public final String name;
+ public final String description;
+ public final String usage;
+ private final List permissions;
+ private final List aliases;
+
+ public final List getPermissions() {
+ return new ArrayList<>(permissions);
+ }
+
+ public final List getAliases() {
+ return new ArrayList<>(aliases);
+ }
+
+ public Command(CommandManager commandManager, String name, String description, String usage, List permissions, List aliases) {
+ this.commandManager = commandManager;
+ this.name = name;
+ this.description = description;
+ this.usage = usage;
+ this.permissions = permissions;
+ this.aliases = aliases;
+ }
+
+ public abstract void execute(CommandExecutor commandExecutor, String label, String[] args);
+}
diff --git a/src/main/java/me/finn/unlegitlibrary/command/CommandExecutor.java b/src/main/java/me/finn/unlegitlibrary/command/CommandExecutor.java
new file mode 100644
index 0000000..0a1211b
--- /dev/null
+++ b/src/main/java/me/finn/unlegitlibrary/command/CommandExecutor.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
+ *
+ * You are unauthorized to remove this copyright.
+ * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
+ * See LICENSE-File if exists
+ */
+
+package me.finn.unlegitlibrary.command;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+
+public abstract class CommandExecutor {
+
+ public final String name;
+ private final List permissions;
+
+ public List getPermissions() {
+ return new ArrayList<>(permissions);
+ }
+
+ public boolean hasPermission(CommandPermission permission) {
+ return permissions.contains(permission);
+ }
+
+ public boolean hasPermissions(List permissions) {
+ return new HashSet<>(this.permissions).containsAll(permissions);
+ }
+
+ public CommandExecutor(String name, CommandPermission... permissions) {
+ this.name = name;
+
+ this.permissions = new ArrayList<>();
+ this.permissions.addAll(Arrays.asList(permissions));
+ }
+
+
+}
diff --git a/src/main/java/me/finn/unlegitlibrary/command/CommandManager.java b/src/main/java/me/finn/unlegitlibrary/command/CommandManager.java
new file mode 100644
index 0000000..867bc94
--- /dev/null
+++ b/src/main/java/me/finn/unlegitlibrary/command/CommandManager.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
+ *
+ * You are unauthorized to remove this copyright.
+ * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
+ * See LICENSE-File if exists
+ */
+
+package me.finn.unlegitlibrary.command;
+
+import me.finn.unlegitlibrary.command.events.*;
+import me.finn.unlegitlibrary.event.EventManager;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class CommandManager {
+
+ private final List commands;
+ private final EventManager eventManager;
+
+ public CommandManager(EventManager eventManager) {
+ this.commands = new ArrayList<>();
+ this.eventManager = eventManager;
+ }
+
+ public final void registerCommand(Command command) {
+ if (this.commands.contains(command)) return;
+ this.commands.add(command);
+ }
+
+ public final void unregisterCommand(Command command) {
+ if (!this.commands.contains(command)) return;
+ this.commands.remove(command);
+ }
+
+ public List getCommands() {
+ return new ArrayList<>(commands);
+ }
+
+ public Command getCommandByName(String name) {
+ for (Command command : commands) if (command.name.equals(name)) return command;
+ return null;
+ }
+
+ public Command getCommandByAliases(List aliases) {
+ for (String alias : aliases) {
+ for (Command command : commands) {
+ List aliasesCommand = new ArrayList<>();
+ for (String registeredAlias : command.getAliases()) aliasesCommand.add(registeredAlias.toLowerCase());
+ if (aliasesCommand.contains(alias.toLowerCase())) return command;
+ }
+ }
+
+ return null;
+ }
+
+ public Command getCommandByAlias(String alias) {
+ for (Command command : commands) {
+ List aliasesCommand = new ArrayList<>();
+ for (String registeredAlias : command.getAliases()) aliasesCommand.add(registeredAlias.toLowerCase());
+ if (aliasesCommand.contains(alias.toLowerCase())) return command;
+ }
+
+ return null;
+ }
+
+ public Command getCommand(String input) {
+ if (getCommandByName(input) != null) return getCommandByName(input);
+ if (getCommandByAlias(input) != null) return getCommandByAlias(input);
+ return null;
+ }
+
+ public void execute(CommandExecutor commandExecutor, String line) {
+ String[] split = line.split(" ");
+ String command = split[0];
+
+ String[] args = Arrays.copyOfRange(split, 1, split.length);
+
+ if (getCommand(command) != null) {
+ Command cmd = getCommand(command);
+ PreCommandExecuteEvent preEvent = new PreCommandExecuteEvent(this, commandExecutor, cmd);
+ eventManager.executeEvent(preEvent);
+
+ if (preEvent.isCancelled()) return;
+
+ if (commandExecutor.hasPermissions(cmd.getPermissions())) {
+ CommandExecuteEvent event = new CommandExecuteEvent(this, commandExecutor, cmd);
+ eventManager.executeEvent(event);
+
+ if (event.isCancelled()) return;
+
+ cmd.execute(commandExecutor, command, args);
+ eventManager.executeEvent(new CommandExecutedEvent(this, commandExecutor, cmd));
+ } else eventManager.executeEvent(new CommandExecutorMissingPermissionEvent(this, commandExecutor, cmd));
+ } else eventManager.executeEvent(new CommandNotFoundEvent(this, commandExecutor, command, args));
+ }
+}
diff --git a/src/main/java/me/finn/unlegitlibrary/command/CommandPermission.java b/src/main/java/me/finn/unlegitlibrary/command/CommandPermission.java
new file mode 100644
index 0000000..a1293a4
--- /dev/null
+++ b/src/main/java/me/finn/unlegitlibrary/command/CommandPermission.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
+ *
+ * You are unauthorized to remove this copyright.
+ * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
+ * See LICENSE-File if exists
+ */
+
+package me.finn.unlegitlibrary.command;
+
+public class CommandPermission {
+
+ public final String name;
+ public final int level;
+
+ public CommandPermission(String name, int level) {
+ this.name = name;
+ this.level = level;
+ }
+}
diff --git a/src/main/java/me/finn/unlegitlibrary/command/events/CommandExecuteEvent.java b/src/main/java/me/finn/unlegitlibrary/command/events/CommandExecuteEvent.java
new file mode 100644
index 0000000..68a8a19
--- /dev/null
+++ b/src/main/java/me/finn/unlegitlibrary/command/events/CommandExecuteEvent.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
+ *
+ * You are unauthorized to remove this copyright.
+ * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
+ * See LICENSE-File if exists
+ */
+
+package me.finn.unlegitlibrary.command.events;
+
+import me.finn.unlegitlibrary.command.Command;
+import me.finn.unlegitlibrary.command.CommandExecutor;
+import me.finn.unlegitlibrary.command.CommandManager;
+import me.finn.unlegitlibrary.event.impl.CancellableEvent;
+import me.finn.unlegitlibrary.event.impl.Event;
+
+public class CommandExecuteEvent extends CancellableEvent {
+ public final CommandManager commandManager;
+ public final CommandExecutor commandExecutor;
+ public final Command command;
+
+ public CommandExecuteEvent(CommandManager commandManager, CommandExecutor commandExecutor, Command command) {
+ this.commandManager = commandManager;
+ this.commandExecutor = commandExecutor;
+ this.command = command;
+ }
+}
diff --git a/src/main/java/me/finn/unlegitlibrary/command/events/CommandExecutedEvent.java b/src/main/java/me/finn/unlegitlibrary/command/events/CommandExecutedEvent.java
new file mode 100644
index 0000000..68aa01d
--- /dev/null
+++ b/src/main/java/me/finn/unlegitlibrary/command/events/CommandExecutedEvent.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
+ *
+ * You are unauthorized to remove this copyright.
+ * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
+ * See LICENSE-File if exists
+ */
+
+package me.finn.unlegitlibrary.command.events;
+
+import me.finn.unlegitlibrary.command.Command;
+import me.finn.unlegitlibrary.command.CommandExecutor;
+import me.finn.unlegitlibrary.command.CommandManager;
+import me.finn.unlegitlibrary.event.impl.Event;
+
+public class CommandExecutedEvent extends Event {
+ public final CommandManager commandManager;
+ public final CommandExecutor commandExecutor;
+ public final Command command;
+
+ public CommandExecutedEvent(CommandManager commandManager, CommandExecutor commandExecutor, Command command) {
+ this.commandManager = commandManager;
+ this.commandExecutor = commandExecutor;
+ this.command = command;
+ }
+}
diff --git a/src/main/java/me/finn/unlegitlibrary/command/events/CommandExecutorMissingPermissionEvent.java b/src/main/java/me/finn/unlegitlibrary/command/events/CommandExecutorMissingPermissionEvent.java
new file mode 100644
index 0000000..db746c0
--- /dev/null
+++ b/src/main/java/me/finn/unlegitlibrary/command/events/CommandExecutorMissingPermissionEvent.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
+ *
+ * You are unauthorized to remove this copyright.
+ * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
+ * See LICENSE-File if exists
+ */
+
+package me.finn.unlegitlibrary.command.events;
+
+import me.finn.unlegitlibrary.command.Command;
+import me.finn.unlegitlibrary.command.CommandExecutor;
+import me.finn.unlegitlibrary.command.CommandManager;
+import me.finn.unlegitlibrary.event.impl.CancellableEvent;
+import me.finn.unlegitlibrary.event.impl.Event;
+
+public class CommandExecutorMissingPermissionEvent extends Event {
+ public final CommandManager commandManager;
+ public final CommandExecutor commandExecutor;
+ public final Command command;
+
+ public CommandExecutorMissingPermissionEvent(CommandManager commandManager, CommandExecutor commandExecutor, Command command) {
+ this.commandManager = commandManager;
+ this.commandExecutor = commandExecutor;
+ this.command = command;
+ }
+}
diff --git a/src/main/java/me/finn/unlegitlibrary/command/events/CommandNotFoundEvent.java b/src/main/java/me/finn/unlegitlibrary/command/events/CommandNotFoundEvent.java
new file mode 100644
index 0000000..a96bd29
--- /dev/null
+++ b/src/main/java/me/finn/unlegitlibrary/command/events/CommandNotFoundEvent.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
+ *
+ * You are unauthorized to remove this copyright.
+ * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
+ * See LICENSE-File if exists
+ */
+
+package me.finn.unlegitlibrary.command.events;
+
+import me.finn.unlegitlibrary.command.Command;
+import me.finn.unlegitlibrary.command.CommandExecutor;
+import me.finn.unlegitlibrary.command.CommandManager;
+import me.finn.unlegitlibrary.event.impl.CancellableEvent;
+import me.finn.unlegitlibrary.event.impl.Event;
+
+public class CommandNotFoundEvent extends Event {
+ public final CommandManager commandManager;
+ public final CommandExecutor commandExecutor;
+ public final String name;
+ public final String[] args;
+
+ public CommandNotFoundEvent(CommandManager commandManager, CommandExecutor commandExecutor, String name, String[] args) {
+ this.commandManager = commandManager;
+ this.commandExecutor = commandExecutor;
+ this.name = name;
+ this.args = args;
+ }
+}
diff --git a/src/main/java/me/finn/unlegitlibrary/command/events/PreCommandExecuteEvent.java b/src/main/java/me/finn/unlegitlibrary/command/events/PreCommandExecuteEvent.java
new file mode 100644
index 0000000..f35908f
--- /dev/null
+++ b/src/main/java/me/finn/unlegitlibrary/command/events/PreCommandExecuteEvent.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
+ *
+ * You are unauthorized to remove this copyright.
+ * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
+ * See LICENSE-File if exists
+ */
+
+package me.finn.unlegitlibrary.command.events;
+
+import me.finn.unlegitlibrary.command.Command;
+import me.finn.unlegitlibrary.command.CommandExecutor;
+import me.finn.unlegitlibrary.command.CommandManager;
+import me.finn.unlegitlibrary.event.impl.CancellableEvent;
+
+public class PreCommandExecuteEvent extends CancellableEvent {
+ public final CommandManager commandManager;
+ public final CommandExecutor commandExecutor;
+ public final Command command;
+
+ public PreCommandExecuteEvent(CommandManager commandManager, CommandExecutor commandExecutor, Command command) {
+ this.commandManager = commandManager;
+ this.commandExecutor = commandExecutor;
+ this.command = command;
+ }
+}
diff --git a/src/main/java/me/finn/unlegitlibrary/file/FileUtils.java b/src/main/java/me/finn/unlegitlibrary/file/FileUtils.java
index 012ed20..c427e46 100644
--- a/src/main/java/me/finn/unlegitlibrary/file/FileUtils.java
+++ b/src/main/java/me/finn/unlegitlibrary/file/FileUtils.java
@@ -14,6 +14,8 @@ import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
+import java.util.ArrayList;
+import java.util.List;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@@ -40,6 +42,7 @@ public class FileUtils extends DefaultMethodsOverrider {
return splitName[splitName.length - 2];
}
+ @Deprecated
public static void copyResourceToFile(String resourceName, File targetFile, Class resourceClass) throws IOException {
InputStream inputStream = resourceClass.getResourceAsStream("/" + resourceName);
OutputStream outputStream = new FileOutputStream(targetFile);
@@ -89,17 +92,17 @@ public class FileUtils extends DefaultMethodsOverrider {
bis.close();
}
- public static boolean isEmpty(Path path) throws IOException {
- if (Files.isDirectory(path)) {
- try (Stream entries = Files.list(path)) {
+ public static boolean isEmpty(File file) throws IOException {
+ if (file.isDirectory()) {
+ try (Stream entries = Files.list(file.toPath())) {
return !entries.findFirst().isPresent();
}
- }
+ } else if (file.isFile()) return file.length() == 0;
return false;
}
- public static String readFile(File file) throws IOException {
+ public static String readFileFull(File file) throws IOException {
Long length = file.length();
byte[] content = new byte[length.intValue()];
@@ -110,42 +113,12 @@ public class FileUtils extends DefaultMethodsOverrider {
return new String(content, StandardCharsets.UTF_8);
}
- public static void copyFile(File sourceFile, File toFolder, boolean replaceExisting) throws IOException {
- // Check if the source file exists and is a regular file
- if (!sourceFile.exists() || !sourceFile.isFile()) return;
-
- // Check if the destination folder exists and is a directory
- if (!toFolder.exists() || !toFolder.isDirectory()) return;
-
- // Get the name of the source file
- String fileName = sourceFile.getName();
- File destinationFile = new File(toFolder, fileName);
-
-
- if (replaceExisting)
- Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
- else Files.copy(sourceFile.toPath(), destinationFile.toPath());
- }
-
- public static void copyFiles(File fromFolder, File toFolder, boolean replaceExisting) throws IOException {
- // Check if the source directory exists and is a directory
- if (!fromFolder.exists() || !fromFolder.isDirectory()) return;
-
- // Check if the destination directory exists and is a directory
- if (!toFolder.exists() || !toFolder.isDirectory()) return;
-
- // List all files in the source directory
- File[] filesToCopy = fromFolder.listFiles();
- if (filesToCopy == null) return;
-
- // Iterate through the files and copy them to the destination directory
- for (File file : filesToCopy) {
- Path source = file.toPath();
- Path destination = new File(toFolder, file.getName()).toPath();
-
- if (replaceExisting) Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
- else Files.copy(source, destination);
- }
+ public static List readFileLines(File file) throws IOException {
+ List lines = new ArrayList<>();
+ BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
+ String str;
+ while ((str = in.readLine()) != null) lines.add(str);
+ return lines;
}
public static void writeFile(File file, String text) throws IOException {
@@ -163,5 +136,4 @@ public class FileUtils extends DefaultMethodsOverrider {
if (!file.isHidden()) return;
Files.setAttribute(Paths.get(file.getPath()), "dos:hidden", false, LinkOption.NOFOLLOW_LINKS);
}
-
}
\ No newline at end of file
diff --git a/src/main/java/tests/Client.java b/src/main/java/tests/Client.java
deleted file mode 100644
index 2adcc6c..0000000
--- a/src/main/java/tests/Client.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
- *
- * You are unauthorized to remove this copyright.
- * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
- * See LICENSE-File if exists
- */
-
-package tests;
-
-import me.finn.unlegitlibrary.event.EventManager;
-import me.finn.unlegitlibrary.network.system.client.NetworkClient;
-import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
-
-import java.lang.reflect.InvocationTargetException;
-
-public class Client {
- public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
- new NetworkClient.ClientBuilder().setPacketHandler(new PacketHandler()).setEventManager(new EventManager()).
- setHost("localhost").setPort(25565).build().connect();
- }
-}
diff --git a/src/main/java/tests/Server.java b/src/main/java/tests/Server.java
deleted file mode 100644
index 7d52e40..0000000
--- a/src/main/java/tests/Server.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
- *
- * You are unauthorized to remove this copyright.
- * You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
- * See LICENSE-File if exists
- */
-
-package tests;
-
-import me.finn.unlegitlibrary.event.EventManager;
-import me.finn.unlegitlibrary.network.system.client.NetworkClient;
-import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
-import me.finn.unlegitlibrary.network.system.server.NetworkServer;
-
-import java.lang.reflect.InvocationTargetException;
-
-public class Server {
- public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
- new NetworkServer.ServerBuilder().setPacketHandler(new PacketHandler()).setEventManager(new EventManager()).
- setPort(25565).build().start();
- }
-}