Update content

This commit is contained in:
Paul Reilly 2023-05-12 21:19:35 -05:00
parent c41b8dc084
commit 98d7ffafe3
30 changed files with 300 additions and 307 deletions

12
.gitignore vendored
View File

@ -42,4 +42,14 @@ bin/
.DS_Store .DS_Store
.idea .idea
/gradle /gradle
.idea/codeStyles/codeStyleConfig.xml
.idea/codeStyles/Project.xml
.idea/dbnavigator.xml
.idea/gradle.xml
.idea/inspectionProfiles/Project_Default.xml
.idea/misc.xml
.idea/sonarlint/issuestore/index.pb
.idea/sonarlint/securityhotspotstore/index.pb
.idea/uiDesigner.xml
.idea/vcs.xml

View File

@ -1,27 +0,0 @@
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

@ -1,22 +0,0 @@
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

@ -16,78 +16,106 @@ public interface Context<T>
{ {
T get(); T get();
default @Nullable Player asPlayer() { default @Nullable Player asPlayer()
if (get() instanceof Player player) { {
if (get() instanceof Player player)
{
return player; return player;
} else { } else
{
return null; return null;
} }
} }
default @Nullable CommandSender asCommandSender() { default @Nullable CommandSender asCommandSender()
if (get() instanceof CommandSender commandSender) { {
if (get() instanceof CommandSender commandSender)
{
return commandSender; return commandSender;
} else { } else
{
return null; return null;
} }
} }
default @NotNull String asLiteral() { default @NotNull String asLiteral()
{
return get().toString(); return get().toString();
} }
default @Nullable World asWorld() { default @Nullable World asWorld()
if (get() instanceof World world) { {
if (get() instanceof World world)
{
return world; return world;
} else { } else
{
return null; return null;
} }
} }
default @Nullable Location asLocation() { default @Nullable Location asLocation()
if (get() instanceof Location location) { {
if (get() instanceof Location location)
{
return location; return location;
} else { } else
{
return null; return null;
} }
} }
default @Nullable LivingEntity asLivingEntity() { default @Nullable LivingEntity asLivingEntity()
if (get() instanceof LivingEntity livingEntity) { {
if (get() instanceof LivingEntity livingEntity)
{
return livingEntity; return livingEntity;
} else { } else
{
return null; return null;
} }
} }
default @Nullable Component asComponent() { default @Nullable Component asComponent()
if (get() instanceof Component component) { {
if (get() instanceof Component component)
{
return component; return component;
} else { } else
{
return null; return null;
} }
} }
default @Nullable Projectile asProjectile() { default @Nullable Projectile asProjectile()
if (get() instanceof Projectile projectile) { {
if (get() instanceof Projectile projectile)
{
return projectile; return projectile;
} else { } else
{
return null; return null;
} }
} }
default @Nullable Action asAction() { default @Nullable Action asAction()
if (get() instanceof Action action) { {
if (get() instanceof Action action)
{
return action; return action;
} else { } else
{
return null; return null;
} }
} }
default <U> @Nullable U asCustom(Class<U> clazz) { default <U> @Nullable U asCustom(Class<U> clazz)
if (clazz.isInstance(get())) { {
if (clazz.isInstance(get()))
{
return clazz.cast(get()); return clazz.cast(get());
} else { } else
{
return null; return null;
} }
} }

View File

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

View File

@ -0,0 +1,15 @@
package me.totalfreedom.api;
public interface Serializable<T>
{
/**
* Serialize an object to a string.
* Ideally, this should serialize to an SQL query for easy data transfer.
*
* @param object The object to serialize
* @return The serialized object
*/
String serialize(T object);
T deserialize(Context<?>... contexts);
}

View File

@ -1,52 +1,37 @@
package me.totalfreedom.base; package me.totalfreedom.base;
import me.totalfreedom.event.EventBus; import me.totalfreedom.event.EventBus;
import me.totalfreedom.module.Module;
import me.totalfreedom.utils.Identity;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class CommonsBase implements Module<CommonsBase> public class CommonsBase extends JavaPlugin
{ {
private final EventBus eventBus = new EventBus(this); private final EventBus eventBus = new EventBus(this);
public static CommonsBase getInstance()
{
return JavaPlugin.getPlugin(CommonsBase.class);
}
@Override @Override
public void enable() public void onEnable()
{ {
Bukkit.getServicesManager().register(EventBus.class, Bukkit.getServicesManager().register(EventBus.class,
eventBus, eventBus,
JavaPlugin.getPlugin(CommonsJavaPlugin.class), this,
ServicePriority.High); ServicePriority.High);
} }
@Override @Override
public void disable() public void onDisable()
{ {
} }
@Override public RegisteredServiceProvider<EventBus> getEventBus()
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); return Bukkit.getServicesManager().getRegistration(EventBus.class);
} }
} }

View File

@ -1,24 +0,0 @@
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

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

View File

@ -1,8 +1,7 @@
package me.totalfreedom.data; package me.totalfreedom.data;
import co.aikar.commands.*; import co.aikar.commands.*;
import me.totalfreedom.base.CommonsJavaPlugin; import me.totalfreedom.base.CommonsBase;
import org.bukkit.plugin.java.JavaPlugin;
public class CommandRegistry public class CommandRegistry
{ {
@ -16,7 +15,7 @@ public class CommandRegistry
public CommandRegistry() public CommandRegistry()
{ {
this.manager = new PaperCommandManager(JavaPlugin.getPlugin(CommonsJavaPlugin.class)); this.manager = new PaperCommandManager(CommonsBase.getInstance());
this.contexts = new PaperCommandContexts(manager); this.contexts = new PaperCommandContexts(manager);
this.completions = new PaperCommandCompletions(manager); this.completions = new PaperCommandCompletions(manager);
this.replacements = manager.getCommandReplacements(); this.replacements = manager.getCommandReplacements();
@ -50,11 +49,13 @@ public class CommandRegistry
return conditions; return conditions;
} }
public void register(BaseCommand cmd) { public void register(BaseCommand cmd)
{
manager.registerCommand(cmd); manager.registerCommand(cmd);
} }
public void unregister(BaseCommand cmd) { public void unregister(BaseCommand cmd)
{
manager.unregisterCommand(cmd); manager.unregisterCommand(cmd);
} }
} }

View File

@ -1,5 +1,39 @@
package me.totalfreedom.data; package me.totalfreedom.data;
import me.totalfreedom.event.FEvent;
import me.totalfreedom.provider.EventProvider;
import java.util.ArrayList;
import java.util.List;
public class EventRegistry public class EventRegistry
{ {
private final List<FEvent> events;
public EventRegistry()
{
this.events = new ArrayList<>();
}
public void register(final FEvent event)
{
this.events.add(event);
}
public void unregister(final FEvent event)
{
this.events.remove(event);
}
public <T extends FEvent> EventProvider<T> getEvent(final Class<T> clazz)
{
for (final FEvent event : this.events)
{
if (clazz.isInstance(event))
{
return () -> (T) event;
}
}
return null;
}
} }

View File

@ -1,89 +1,40 @@
package me.totalfreedom.data; package me.totalfreedom.data;
import me.totalfreedom.module.Module; import me.totalfreedom.provider.ModuleProvider;
import org.yaml.snakeyaml.Yaml; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.util.ArrayList;
import java.io.IOException; import java.util.List;
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 public class ModuleRegistry
{ {
private final Set<Module<?>> moduleSet; private final List<JavaPlugin> plugins;
public ModuleRegistry() public ModuleRegistry()
{ {
this.moduleSet = new HashSet<>(); this.plugins = new ArrayList<>();
} }
public Set<? extends Module<?>> getModuleSet() public void addModule(final JavaPlugin plugin)
{ {
return moduleSet; if (this.plugins.contains(plugin))
} {
return;
public void addModule(Module<?> module) }
{ this.plugins.add(plugin);
moduleSet.add(module);
}
public void removeModule(Module<?> module)
{
moduleSet.remove(module);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getModule(Class<T> clazz) public <T extends JavaPlugin> ModuleProvider<T> getModule(Class<T> clazz)
{ {
for (Module<?> module : moduleSet) for (JavaPlugin plugin : plugins)
{ {
if (module.getRuntimeClass().equals(clazz)) if (clazz.isInstance(plugin))
{ {
// We know that because the runtime class matches, return () -> (T) plugin;
// we can safely infer the type.
return (T) module.getRuntimeInstance();
} }
} }
return null;
}
public void enableModules() return () -> null;
{
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

@ -1,5 +1,64 @@
package me.totalfreedom.data; package me.totalfreedom.data;
import me.totalfreedom.service.Service;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;
import java.util.ArrayList;
import java.util.List;
public class ServiceRegistry public class ServiceRegistry
{ {
private final List<Service> services;
public ServiceRegistry()
{
this.services = new ArrayList<>();
}
public void startAll()
{
for (Service service : this.services)
{
service.start();
}
}
public void stopAll()
{
for (Service service : this.services)
{
service.stop();
}
}
@SuppressWarnings("unchecked")
// Suppressing is fine here; we know that the service is of type T extends Service,
// and calling getClass() on this object would effectively be Class<T>, though we may lose
// the identity of the code signature in the process.
// In this case, that is fine.
public <T extends Service> void register(Plugin plugin, final T service)
{
this.services.add(service);
if (!service.getClass().isInstance(service))
{
throw new UnknownError("""
A critical issue has been encountered:
The service %s is not an instance of itself.
This is a critical issue and should be reported immediately.
""".formatted(service.getClass().getName()));
}
Bukkit.getServicesManager().register(
(Class<T>) service.getClass(),
service,
plugin,
ServicePriority.Normal);
}
public <T extends Service> RegisteredServiceProvider<T> getService(Class<T> clazz)
{
return Bukkit.getServicesManager().getRegistration(clazz);
}
} }

View File

@ -1,14 +1,9 @@
package me.totalfreedom.event; package me.totalfreedom.event;
import me.totalfreedom.base.CommonsBase; 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.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Executable; import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -18,11 +13,13 @@ public class EventBus
private final Map<Listener, Set<FEvent>> listenerEventMap = new HashMap<>(); private final Map<Listener, Set<FEvent>> listenerEventMap = new HashMap<>();
private final CommonsBase plugin; private final CommonsBase plugin;
public EventBus(CommonsBase plugin) { public EventBus(CommonsBase plugin)
{
this.plugin = plugin; this.plugin = plugin;
} }
void registerListener(Listener listener) { void registerListener(Listener listener)
{
Set<FEvent> eventSet = Arrays.stream(listener.getClass().getDeclaredMethods()) Set<FEvent> eventSet = Arrays.stream(listener.getClass().getDeclaredMethods())
.filter(m -> m.isAnnotationPresent(Handler.class)) .filter(m -> m.isAnnotationPresent(Handler.class))
.map(Executable::getParameters) .map(Executable::getParameters)
@ -33,7 +30,8 @@ public class EventBus
try try
{ {
return (FEvent) p[0].getType().getDeclaredConstructor().newInstance(); return (FEvent) p[0].getType().getDeclaredConstructor().newInstance();
} catch (Exception exception) { } catch (Exception exception)
{
exception.printStackTrace(); exception.printStackTrace();
return null; return null;
} }
@ -44,19 +42,33 @@ public class EventBus
listenerEventMap.put(listener, eventSet); listenerEventMap.put(listener, eventSet);
} }
void unregisterListener(Listener listener) { void unregisterListener(Listener listener)
{
listenerEventMap.remove(listener); listenerEventMap.remove(listener);
} }
public void startListening() { public void startListening()
{
listenerSet().forEach(this::registerListener); listenerSet().forEach(this::registerListener);
} }
public void stopListening() { public void stopListening()
{
listenerSet().forEach(this::unregisterListener); listenerSet().forEach(this::unregisterListener);
} }
public Set<Listener> listenerSet() { public Set<Listener> listenerSet()
{
return listenerSet; return listenerSet;
} }
public Map<Listener, Set<FEvent>> listenerEventMap()
{
return listenerEventMap;
}
public CommonsBase getCommonsBase()
{
return plugin;
}
} }

View File

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

View File

@ -1,28 +0,0 @@
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,9 @@
package me.totalfreedom.provider;
import me.totalfreedom.event.FEvent;
@FunctionalInterface
public interface EventProvider<T extends FEvent>
{
T getEvent();
}

View File

@ -0,0 +1,9 @@
package me.totalfreedom.provider;
import org.bukkit.plugin.java.JavaPlugin;
@FunctionalInterface
public interface ModuleProvider<T extends JavaPlugin>
{
T getModule();
}

View File

@ -0,0 +1,8 @@
package me.totalfreedom.provider;
import me.totalfreedom.service.Service;
public interface ServiceProvider<T extends Service>
{
T getService();
}

View File

@ -1,4 +1,4 @@
package me.totalfreedom.permission; package me.totalfreedom.security;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;

View File

@ -1,7 +1,6 @@
package me.totalfreedom.permission; package me.totalfreedom.security;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
@ -12,7 +11,7 @@ public interface Node
boolean getValue(); boolean getValue();
Permission spigot(); Permission bukkit();
NodeType getType(); NodeType getType();
@ -29,6 +28,4 @@ public interface Node
boolean isWildcard(); boolean isWildcard();
boolean isNegated(); boolean isNegated();
NodeBuilder builder();
} }

View File

@ -1,4 +1,4 @@
package me.totalfreedom.permission; package me.totalfreedom.security;
public interface NodeBuilder public interface NodeBuilder
{ {

View File

@ -1,4 +1,4 @@
package me.totalfreedom.permission; package me.totalfreedom.security;
public enum NodeType public enum NodeType
{ {

View File

@ -1,16 +1,13 @@
package me.totalfreedom.permission; package me.totalfreedom.security;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible; import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.Permission;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
public interface PermissionHolder extends Permissible public interface PermissionHolder extends Permissible
{ {
PermissionHolder fromPlayer(Player player);
UUID getUniqueId(); UUID getUniqueId();
Set<Node> permissions(); Set<Node> permissions();

View File

@ -1,12 +0,0 @@
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,16 @@
package me.totalfreedom.service;
public abstract class Service
{
private final String name;
protected Service(String name)
{
this.name = name;
}
public abstract void start();
public abstract void stop();
}

View File

@ -1,6 +1,6 @@
package me.totalfreedom.service; package me.totalfreedom.service;
public interface FreedomService public interface Task extends Runnable
{ {
void start(); void start();

View File

@ -8,16 +8,10 @@ public interface SQL
{ {
CompletableFuture<Connection> getConnection(String url); CompletableFuture<Connection> getConnection(String url);
CompletableFuture<ResultSet> executeQuery(String query);
CompletableFuture<ResultSet> executeQuery(String query, Object... args); CompletableFuture<ResultSet> executeQuery(String query, Object... args);
CompletableFuture<Boolean> executeUpdate(String query);
CompletableFuture<Boolean> executeUpdate(String query, Object... args); CompletableFuture<Boolean> executeUpdate(String query, Object... args);
CompletableFuture<Void> execute(String query);
CompletableFuture<Void> execute(String query, Object... args); CompletableFuture<Void> execute(String query, Object... args);
CompletableFuture<Boolean> createTable(String table, String... columns); CompletableFuture<Boolean> createTable(String table, String... columns);

View File

@ -7,7 +7,8 @@ public interface SQLProperties
{ {
Properties getProperties(File propertiesFile); Properties getProperties(File propertiesFile);
default Properties getDefaultProperties() { default Properties getDefaultProperties()
{
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("driver", "sqlite"); properties.setProperty("driver", "sqlite");
properties.setProperty("host", "localhost"); properties.setProperty("host", "localhost");
@ -30,7 +31,8 @@ public interface SQLProperties
String getPassword(); String getPassword();
default String toURLPlain() { default String toURLPlain()
{
return String.format("jdbc:%s://%s:%s/%s", return String.format("jdbc:%s://%s:%s/%s",
this.getDriver(), this.getDriver(),
this.getHost(), this.getHost(),
@ -38,7 +40,8 @@ public interface SQLProperties
this.getDatabase()); this.getDatabase());
} }
default String toURLWithLogin() { default String toURLWithLogin()
{
return String.format("jdbc:%s://%s:%s/%s?user=%s&password=%s", return String.format("jdbc:%s://%s:%s/%s?user=%s&password=%s",
this.getDriver(), this.getDriver(),
this.getHost(), this.getHost(),

View File

@ -1,10 +1,8 @@
package me.totalfreedom.user; package me.totalfreedom.user;
import me.totalfreedom.permission.PermissionHolder; import me.totalfreedom.security.PermissionHolder;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.UUID;
public interface User extends PermissionHolder public interface User extends PermissionHolder
{ {
Component getDisplayName(); Component getDisplayName();