Added command system
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.finn.unlegitlibrary</groupId>
|
||||
<artifactId>unlegitlibrary</artifactId>
|
||||
<version>1.4.2-OpenJDK11</version>
|
||||
<version>1.5.0-OpenJDK11</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
|
42
src/main/java/me/finn/unlegitlibrary/command/Command.java
Normal file
42
src/main/java/me/finn/unlegitlibrary/command/Command.java
Normal file
@@ -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<CommandPermission> permissions;
|
||||
private final List<String> aliases;
|
||||
|
||||
public final List<CommandPermission> getPermissions() {
|
||||
return new ArrayList<>(permissions);
|
||||
}
|
||||
|
||||
public final List<String> getAliases() {
|
||||
return new ArrayList<>(aliases);
|
||||
}
|
||||
|
||||
public Command(CommandManager commandManager, String name, String description, String usage, List<CommandPermission> permissions, List<String> 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);
|
||||
}
|
@@ -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<CommandPermission> permissions;
|
||||
|
||||
public List<CommandPermission> getPermissions() {
|
||||
return new ArrayList<>(permissions);
|
||||
}
|
||||
|
||||
public boolean hasPermission(CommandPermission permission) {
|
||||
return permissions.contains(permission);
|
||||
}
|
||||
|
||||
public boolean hasPermissions(List<CommandPermission> 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));
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -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<Command> 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<Command> 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<String> aliases) {
|
||||
for (String alias : aliases) {
|
||||
for (Command command : commands) {
|
||||
List<String> 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<String> 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));
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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<Path> entries = Files.list(path)) {
|
||||
public static boolean isEmpty(File file) throws IOException {
|
||||
if (file.isDirectory()) {
|
||||
try (Stream<Path> 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<String> readFileLines(File file) throws IOException {
|
||||
List<String> 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);
|
||||
}
|
||||
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user