This commit is contained in:
Paul Reilly
2023-04-13 20:24:40 -05:00
commit 09bede768b
39 changed files with 1377 additions and 0 deletions

42
Commons/.gitignore vendored Normal file
View File

@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

22
Commons/build.gradle Normal file
View File

@ -0,0 +1,22 @@
group = 'me.totalfreedom'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
api 'io.projectreactor:reactor-core:3.5.4'
api 'org.reflections:reflections:0.10.2'
api 'org.slf4j:slf4j-api:1.7.36'
api 'co.aikar:acf-paper:0.5.1-SNAPSHOT'
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
ext.weight = 1
test {
useJUnitPlatform()
}

View File

@ -0,0 +1,27 @@
package me.totalfreedom.admin;
import me.totalfreedom.permission.Group;
import me.totalfreedom.permission.Node;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
public interface AdminManager
{
Map<UUID, Administrator> getAdminMap();
Administrator getAdmin(UUID uuid);
Administrator getAdmin(String name);
Set<Administrator> getAdminsWithPermissions(Node... nodes);
void addAdmin(Administrator admin);
void removeAdmin(Administrator admin);
CompletableFuture<Void> saveAdmin(Administrator admin, Consumer<Administrator> callback);
}

View File

@ -0,0 +1,22 @@
package me.totalfreedom.admin;
import me.totalfreedom.permission.Group;
import me.totalfreedom.permission.PermissionHolder;
import net.kyori.adventure.text.Component;
import org.bukkit.permissions.PermissionAttachment;
import java.util.List;
import java.util.UUID;
public interface Administrator extends Group
{
boolean isActive();
void setActive(boolean active);
void setWeight(int weight);
Component getLoginMessage();
void setLoginMessage(Component loginMessage);
}

View File

@ -0,0 +1,94 @@
package me.totalfreedom.api;
import net.kyori.adventure.text.Component;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.block.Action;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@FunctionalInterface
public interface Context<T>
{
T get();
default @Nullable Player asPlayer() {
if (get() instanceof Player player) {
return player;
} else {
return null;
}
}
default @Nullable CommandSender asCommandSender() {
if (get() instanceof CommandSender commandSender) {
return commandSender;
} else {
return null;
}
}
default @NotNull String asLiteral() {
return get().toString();
}
default @Nullable World asWorld() {
if (get() instanceof World world) {
return world;
} else {
return null;
}
}
default @Nullable Location asLocation() {
if (get() instanceof Location location) {
return location;
} else {
return null;
}
}
default @Nullable LivingEntity asLivingEntity() {
if (get() instanceof LivingEntity livingEntity) {
return livingEntity;
} else {
return null;
}
}
default @Nullable Component asComponent() {
if (get() instanceof Component component) {
return component;
} else {
return null;
}
}
default @Nullable Projectile asProjectile() {
if (get() instanceof Projectile projectile) {
return projectile;
} else {
return null;
}
}
default @Nullable Action asAction() {
if (get() instanceof Action action) {
return action;
} else {
return null;
}
}
default <U> @Nullable U asCustom(Class<U> clazz) {
if (clazz.isInstance(get())) {
return clazz.cast(get());
} else {
return null;
}
}
}

View File

@ -0,0 +1,10 @@
package me.totalfreedom.api;
import java.util.function.Consumer;
public interface Interruptable
{
boolean canInterrupt();
void interrupt(Consumer<Throwable> callback);
}

View File

@ -0,0 +1,52 @@
package me.totalfreedom.base;
import me.totalfreedom.event.EventBus;
import me.totalfreedom.module.Module;
import me.totalfreedom.utils.Identity;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
public class CommonsBase implements Module<CommonsBase>
{
private final EventBus eventBus = new EventBus(this);
@Override
public void enable()
{
Bukkit.getServicesManager().register(EventBus.class,
eventBus,
JavaPlugin.getPlugin(CommonsJavaPlugin.class),
ServicePriority.High);
}
@Override
public void disable()
{
}
@Override
public Identity getIdentity()
{
return null;
}
@Override
public Class<CommonsBase> getRuntimeClass()
{
return CommonsBase.class;
}
@Override
public CommonsBase getRuntimeInstance()
{
return this;
}
public RegisteredServiceProvider<EventBus> getEventBus() {
return Bukkit.getServicesManager().getRegistration(EventBus.class);
}
}

View File

@ -0,0 +1,24 @@
package me.totalfreedom.base;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
public class CommonsJavaPlugin extends JavaPlugin
{
private final File moduleFolder = new File(getDataFolder(), "modules");
@Override
public void onEnable()
{
}
@Override
public void onDisable()
{
Registration.getInstance()
.getModuleRegistry()
.unloadModules(moduleFolder);
}
}

View File

@ -0,0 +1,55 @@
package me.totalfreedom.base;
import me.totalfreedom.data.*;
public class Registration
{
private static final Registration INSTANCE = new Registration();
private final ModuleRegistry moduleRegistry;
private final CommandRegistry commandRegistry;
private final EventRegistry eventRegistry;
private final UserRegistry userRegistry;
private final ServiceRegistry serviceRegistry;
private Registration() {
this.commandRegistry = new CommandRegistry();
this.eventRegistry = new EventRegistry();
this.userRegistry = new UserRegistry();
this.serviceRegistry = new ServiceRegistry();
this.moduleRegistry = new ModuleRegistry();
}
public static Registration getInstance()
{
return INSTANCE;
}
public ModuleRegistry getModuleRegistry()
{
return moduleRegistry;
}
public CommandRegistry getCommandRegistry()
{
return commandRegistry;
}
public EventRegistry getEventRegistry()
{
return eventRegistry;
}
public UserRegistry getUserRegistry()
{
return userRegistry;
}
public ServiceRegistry getServiceRegistry()
{
return serviceRegistry;
}
public CommonsBase getCommonsBase() {
return getModuleRegistry().getModule(CommonsBase.class);
}
}

View File

@ -0,0 +1,40 @@
package me.totalfreedom.config;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.List;
public interface Configuration
{
YamlConfiguration asYaml();
void save() throws IOException;
void load() throws IOException;
String getFileName();
File getConfigurationFile();
String getString(String path);
Boolean getBoolean(String path);
<T> List<T> getList(String path);
List<String> getStringList(String path);
Integer getInt(String path);
Long getLong(String path);
Double getDouble(String path);
<T> void set(String path, T value);
<T> T get(String path, Class<T> type);
<T> T getOrDefault(String path, Class<T> type, T fallback);
}

View File

@ -0,0 +1,60 @@
package me.totalfreedom.data;
import co.aikar.commands.*;
import me.totalfreedom.base.CommonsJavaPlugin;
import org.bukkit.plugin.java.JavaPlugin;
public class CommandRegistry
{
private final PaperCommandManager manager;
private final PaperCommandContexts contexts;
private final PaperCommandCompletions completions;
private final CommandReplacements replacements;
private final CommandConditions<BukkitCommandIssuer,
BukkitCommandExecutionContext,
BukkitConditionContext> conditions;
public CommandRegistry()
{
this.manager = new PaperCommandManager(JavaPlugin.getPlugin(CommonsJavaPlugin.class));
this.contexts = new PaperCommandContexts(manager);
this.completions = new PaperCommandCompletions(manager);
this.replacements = manager.getCommandReplacements();
this.conditions = manager.getCommandConditions();
}
public PaperCommandManager getManager()
{
return manager;
}
public PaperCommandContexts getContexts()
{
return contexts;
}
public PaperCommandCompletions getCompletions()
{
return completions;
}
public CommandReplacements getReplacements()
{
return replacements;
}
public CommandConditions<BukkitCommandIssuer,
BukkitCommandExecutionContext,
BukkitConditionContext> getConditions()
{
return conditions;
}
public void register(BaseCommand cmd) {
manager.registerCommand(cmd);
}
public void unregister(BaseCommand cmd) {
manager.unregisterCommand(cmd);
}
}

View File

@ -0,0 +1,11 @@
package me.totalfreedom.data;
import me.totalfreedom.config.Configuration;
import java.util.HashSet;
import java.util.Set;
public class ConfigRegistry
{
Set<Configuration> configurationSet = new HashSet<>();
}

View File

@ -0,0 +1,5 @@
package me.totalfreedom.data;
public class EventRegistry
{
}

View File

@ -0,0 +1,89 @@
package me.totalfreedom.data;
import me.totalfreedom.module.Module;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.InvalidPathException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ModuleRegistry
{
private final Set<Module<?>> moduleSet;
public ModuleRegistry()
{
this.moduleSet = new HashSet<>();
}
public Set<? extends Module<?>> getModuleSet()
{
return moduleSet;
}
public void addModule(Module<?> module)
{
moduleSet.add(module);
}
public void removeModule(Module<?> module)
{
moduleSet.remove(module);
}
@SuppressWarnings("unchecked")
public <T> T getModule(Class<T> clazz)
{
for (Module<?> module : moduleSet)
{
if (module.getRuntimeClass().equals(clazz))
{
// We know that because the runtime class matches,
// we can safely infer the type.
return (T) module.getRuntimeInstance();
}
}
return null;
}
public void enableModules()
{
for (Module<?> module : moduleSet)
{
module.enable();
}
}
public void disableModules()
{
for (Module<?> module : moduleSet)
{
module.disable();
}
}
public boolean isLoaded(Class<Module<?>> module)
{
return moduleSet.stream()
.anyMatch(m ->
m.getRuntimeClass().equals(module));
}
public void unloadModules(File dataFolder)
{
if (dataFolder.mkdirs()) return;
for (Module<?> module : moduleSet)
{
module.disable();
moduleSet.remove(module);
}
}
}

View File

@ -0,0 +1,5 @@
package me.totalfreedom.data;
public class ServiceRegistry
{
}

View File

@ -0,0 +1,5 @@
package me.totalfreedom.data;
public class UserRegistry
{
}

View File

@ -0,0 +1,62 @@
package me.totalfreedom.event;
import me.totalfreedom.base.CommonsBase;
import me.totalfreedom.base.CommonsJavaPlugin;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
public class EventBus
{
private final Set<Listener> listenerSet = new HashSet<>();
private final Map<Listener, Set<FEvent>> listenerEventMap = new HashMap<>();
private final CommonsBase plugin;
public EventBus(CommonsBase plugin) {
this.plugin = plugin;
}
void registerListener(Listener listener) {
Set<FEvent> eventSet = Arrays.stream(listener.getClass().getDeclaredMethods())
.filter(m -> m.isAnnotationPresent(Handler.class))
.map(Executable::getParameters)
.filter(p -> p.length == 1)
.filter(p -> FEvent.class.isAssignableFrom(p[0].getType()))
.map(p ->
{
try
{
return (FEvent) p[0].getType().getDeclaredConstructor().newInstance();
} catch (Exception exception) {
exception.printStackTrace();
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
listenerEventMap.put(listener, eventSet);
}
void unregisterListener(Listener listener) {
listenerEventMap.remove(listener);
}
public void startListening() {
listenerSet().forEach(this::registerListener);
}
public void stopListening() {
listenerSet().forEach(this::unregisterListener);
}
public Set<Listener> listenerSet() {
return listenerSet;
}
}

View File

@ -0,0 +1,11 @@
package me.totalfreedom.event;
import me.totalfreedom.api.Context;
import org.bukkit.event.Cancellable;
public interface FEvent
{
void call(Context<?>... contexts);
void cancel();
}

View File

@ -0,0 +1,10 @@
package me.totalfreedom.event;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Handler
{
// This is a marker annotation
}

View File

@ -0,0 +1,28 @@
package me.totalfreedom.module;
import me.totalfreedom.utils.Identity;
import java.util.HashSet;
import java.util.Set;
public interface Module<T extends Module<?>>
{
Identity getIdentity();
Class<T> getRuntimeClass();
T getRuntimeInstance();
default void enable()
{
}
default void disable()
{
}
default Set<String> dependencies()
{
return new HashSet<>();
}
}

View File

@ -0,0 +1,18 @@
package me.totalfreedom.permission;
import net.kyori.adventure.text.Component;
public interface Group extends PermissionHolder
{
Component getName();
Component getPrefix();
Component getAbbreviation();
int getWeight();
boolean isDefault();
boolean isHidden();
}

View File

@ -0,0 +1,34 @@
package me.totalfreedom.permission;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import javax.annotation.concurrent.Immutable;
@Immutable
public interface Node
{
String getKey();
boolean getValue();
Permission spigot();
NodeType getType();
boolean compare(Node node);
long getExpiry();
boolean isExpired();
boolean isPermanent();
boolean isTemporary();
boolean isWildcard();
boolean isNegated();
NodeBuilder builder();
}

View File

@ -0,0 +1,18 @@
package me.totalfreedom.permission;
public interface NodeBuilder
{
NodeBuilder key(String key);
NodeBuilder value(boolean value);
NodeBuilder expiry(long expiry);
NodeBuilder type(NodeType type);
NodeBuilder wildcard(boolean wildcard);
NodeBuilder negated(boolean negated);
Node build();
}

View File

@ -0,0 +1,10 @@
package me.totalfreedom.permission;
public enum NodeType
{
INHERITANCE,
PREFIX,
SUFFIX,
PERMISSION,
WEIGHT
}

View File

@ -0,0 +1,21 @@
package me.totalfreedom.permission;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.Permission;
import java.util.Set;
import java.util.UUID;
public interface PermissionHolder extends Permissible
{
PermissionHolder fromPlayer(Player player);
UUID getUniqueId();
Set<Node> permissions();
boolean addPermission(Node node);
boolean removePermission(Node node);
}

View File

@ -0,0 +1,12 @@
package me.totalfreedom.security;
public interface Verification
{
public boolean verify(String input);
public String getVerificationMessage();
public String getVerificationFailedMessage();
public String generateVerificationCode();
}

View File

@ -0,0 +1,24 @@
package me.totalfreedom.service;
public interface FreedomService
{
void start();
void stop();
boolean isRunning();
String getName();
boolean isRepeating();
void setRepeating(long interval);
boolean isDelayed();
void setDelayed(long delay);
long getInterval();
long getDelay();
}

View File

@ -0,0 +1,24 @@
package me.totalfreedom.sql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.concurrent.CompletableFuture;
public interface SQL
{
CompletableFuture<Connection> getConnection(String url);
CompletableFuture<ResultSet> executeQuery(String query);
CompletableFuture<ResultSet> executeQuery(String query, Object... args);
CompletableFuture<Boolean> executeUpdate(String query);
CompletableFuture<Boolean> executeUpdate(String query, Object... args);
CompletableFuture<Void> execute(String query);
CompletableFuture<Void> execute(String query, Object... args);
CompletableFuture<Boolean> createTable(String table, String... columns);
}

View File

@ -0,0 +1,50 @@
package me.totalfreedom.sql;
import java.io.File;
import java.util.Properties;
public interface SQLProperties
{
Properties getProperties(File propertiesFile);
default Properties getDefaultProperties() {
Properties properties = new Properties();
properties.setProperty("driver", "sqlite");
properties.setProperty("host", "localhost");
properties.setProperty("port", "3306");
properties.setProperty("database", "database.db");
properties.setProperty("username", "root");
properties.setProperty("password", "password");
return properties;
}
String getDriver();
String getHost();
String getPort();
String getDatabase();
String getUsername();
String getPassword();
default String toURLPlain() {
return String.format("jdbc:%s://%s:%s/%s",
this.getDriver(),
this.getHost(),
this.getPort(),
this.getDatabase());
}
default String toURLWithLogin() {
return String.format("jdbc:%s://%s:%s/%s?user=%s&password=%s",
this.getDriver(),
this.getHost(),
this.getPort(),
this.getDatabase(),
this.getUsername(),
this.getPassword());
}
}

View File

@ -0,0 +1,13 @@
package me.totalfreedom.user;
import me.totalfreedom.permission.PermissionHolder;
import net.kyori.adventure.text.Component;
import java.util.UUID;
public interface User extends PermissionHolder
{
Component getDisplayName();
boolean isOnline();
}

View File

@ -0,0 +1,26 @@
package me.totalfreedom.utils;
import java.util.UUID;
public class Identity
{
private final String key;
private final UUID id;
public Identity(String key)
{
this.key = key;
this.id = UUID.nameUUIDFromBytes(key.getBytes());
}
public String getKey()
{
return key;
}
public UUID getId()
{
return id;
}
}