mirror of
https://github.com/SimplexDevelopment/SimplexCore.git
synced 2025-07-01 14:46:43 +00:00
Initial commit
This commit is contained in:
42
src/main/java/io/github/paldiu/simplexcore/Constants.java
Normal file
42
src/main/java/io/github/paldiu/simplexcore/Constants.java
Normal file
@ -0,0 +1,42 @@
|
||||
package io.github.paldiu.simplexcore;
|
||||
|
||||
import io.github.paldiu.simplexcore.registry.AddonRegistry;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public final class Constants {
|
||||
private static final SimplexCore plugin = JavaPlugin.getPlugin(SimplexCore.class);
|
||||
private static final Server server = plugin.getServer();
|
||||
private static final Logger logger = plugin.getLogger();
|
||||
private static final PluginManager manager = server.getPluginManager();
|
||||
private static final BukkitScheduler scheduler = server.getScheduler();
|
||||
private static final AddonRegistry registry = new AddonRegistry();
|
||||
|
||||
public static SimplexCore getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public static Server getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public static Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public static PluginManager getManager() {
|
||||
return manager;
|
||||
}
|
||||
|
||||
public static BukkitScheduler getScheduler() {
|
||||
return scheduler;
|
||||
}
|
||||
|
||||
public static AddonRegistry getRegistry() {
|
||||
return registry;
|
||||
}
|
||||
}
|
19
src/main/java/io/github/paldiu/simplexcore/SimplexCore.java
Normal file
19
src/main/java/io/github/paldiu/simplexcore/SimplexCore.java
Normal file
@ -0,0 +1,19 @@
|
||||
package io.github.paldiu.simplexcore;
|
||||
|
||||
import io.github.paldiu.simplexcore.registry.Addon;
|
||||
|
||||
public final class SimplexCore extends Addon<SimplexCore> {
|
||||
protected SimplexCore(SimplexCore plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package io.github.paldiu.simplexcore.command;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface CommandInfo {
|
||||
String name();
|
||||
|
||||
String description();
|
||||
|
||||
String usage();
|
||||
|
||||
String aliases() default "";
|
||||
|
||||
String permission() default "simplex.core";
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
package io.github.paldiu.simplexcore.command;
|
||||
|
||||
import io.github.paldiu.simplexcore.Constants;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.*;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.SimplePluginManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
|
||||
public class CommandLoader {
|
||||
private final Reflections reflections;
|
||||
|
||||
public CommandLoader(Class<?> clazz) {
|
||||
if (!clazz.isAnnotationPresent(CommandInfo.class)) {
|
||||
throw new MissingResourceException("Cannot register this class as the main resource location!", clazz.getSimpleName(), "@CommandInfo");
|
||||
}
|
||||
|
||||
if (!clazz.isAssignableFrom(CommandExecutor.class)) {
|
||||
throw new RuntimeException("Unable to assign an executor!");
|
||||
}
|
||||
|
||||
reflections = new Reflections(clazz);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
reflections.getTypesAnnotatedWith(CommandInfo.class).forEach(annotated -> {
|
||||
CommandInfo info = annotated.getDeclaredAnnotation(CommandInfo.class);
|
||||
|
||||
if (info == null) return;
|
||||
if (!CommandExecutor.class.isAssignableFrom(annotated)) return;
|
||||
|
||||
PluginCommand objectToRegister = Registry.create(Constants.getPlugin(), info.name());
|
||||
objectToRegister.setDescription(info.description());
|
||||
objectToRegister.setUsage(info.usage());
|
||||
objectToRegister.setAliases(Arrays.asList(info.aliases().split(",")));
|
||||
objectToRegister.setPermission(info.permission());
|
||||
objectToRegister.setExecutor(getFromSetName(info.name()));
|
||||
Registry.registerCommand(objectToRegister);
|
||||
});
|
||||
}
|
||||
|
||||
public CommandExecutor getFromSetName(String name) {
|
||||
for (Class<? extends CommandExecutor> obj : reflections.getSubTypesOf(CommandExecutor.class)) {
|
||||
if (name.equalsIgnoreCase(obj.getSimpleName())) {
|
||||
try {
|
||||
Constructor<? extends CommandExecutor> constr = obj.getDeclaredConstructor();
|
||||
return constr.newInstance();
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("Unable to get a command executor! Terminating!");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static class Registry {
|
||||
private static final Constructor<PluginCommand> constructor;
|
||||
private static final Field cmdMapField;
|
||||
private static final Field knownCmdsField;
|
||||
|
||||
static {
|
||||
Constructor<PluginCommand> temp;
|
||||
try {
|
||||
temp = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
|
||||
temp.setAccessible(true);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
constructor = temp;
|
||||
|
||||
Field cmf;
|
||||
try {
|
||||
cmf = SimplePluginManager.class.getDeclaredField("commandMap");
|
||||
cmf.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
cmdMapField = cmf;
|
||||
|
||||
Field kcf;
|
||||
try {
|
||||
kcf = SimpleCommandMap.class.getDeclaredField("knownCommands");
|
||||
kcf.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
knownCmdsField = kcf;
|
||||
}
|
||||
|
||||
public static PluginCommand create(@NotNull Plugin plugin, @NotNull String name) {
|
||||
try {
|
||||
return constructor.newInstance(name, plugin);
|
||||
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void registerCommand(PluginCommand command) {
|
||||
try {
|
||||
CommandMap map = (CommandMap) cmdMapField.get(Bukkit.getPluginManager());
|
||||
Map<String, Command> knownCommands = map.getKnownCommands();
|
||||
|
||||
if (knownCommands.containsKey(command.getName())) {
|
||||
knownCommands.replace(command.getName(), command);
|
||||
}
|
||||
|
||||
map.register(command.getName(), command);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package io.github.paldiu.simplexcore.registry;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public abstract class Addon<T extends Addon<T>> extends JavaPlugin {
|
||||
private final T plugin;
|
||||
|
||||
protected Addon(T plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets your plugin as an addon.
|
||||
*
|
||||
* @return The addon.
|
||||
*/
|
||||
public T getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin startup logic.
|
||||
*/
|
||||
public abstract void start();
|
||||
|
||||
/**
|
||||
* Plugin shutdown logic.
|
||||
*/
|
||||
public abstract void stop();
|
||||
|
||||
/**
|
||||
* Plugin initialization logic.
|
||||
*/
|
||||
public void init() {
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package io.github.paldiu.simplexcore.registry;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class AddonRegistry {
|
||||
private final Set<Addon<?>> components = new HashSet<>();
|
||||
|
||||
public AddonRegistry() {
|
||||
}
|
||||
|
||||
public void register(Addon<?> addon) {
|
||||
getComponents().add(addon);
|
||||
}
|
||||
|
||||
public Set<Addon<?>> getComponents() {
|
||||
return components;
|
||||
}
|
||||
}
|
7
src/main/resources/plugin.yml
Normal file
7
src/main/resources/plugin.yml
Normal file
@ -0,0 +1,7 @@
|
||||
name: SimplexCore
|
||||
version: ${project.version}
|
||||
main: pw.coomer.simplexcore.SimplexCore
|
||||
api-version: 1.16
|
||||
prefix: Simplex
|
||||
authors: [ Paldiu ]
|
||||
description: Core for a group of plugins designed to enhance gameplay to the max
|
Reference in New Issue
Block a user