This commit is contained in:
Paldiu 2021-01-26 21:47:08 -06:00
parent b77ec36cee
commit aea26ef2f3
16 changed files with 317 additions and 44 deletions

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,6 +1,8 @@
package io.github.paldiu.simplexcore;
import io.github.paldiu.simplexcore.registry.Addon;
import io.github.paldiu.simplexcore.command.defaults.Command_info;
import io.github.paldiu.simplexcore.plugin.Addon;
import io.github.paldiu.simplexcore.utils.Constants;
public final class SimplexCore extends Addon<SimplexCore> {
protected SimplexCore(SimplexCore plugin) {
@ -9,7 +11,8 @@ public final class SimplexCore extends Addon<SimplexCore> {
@Override
public void start() {
Constants.getRegistry().register(this);
Constants.getCommandLoader().classpath(Command_info.class).load();
}
@Override

View File

@ -0,0 +1,41 @@
package io.github.paldiu.simplexcore.command;
import io.github.paldiu.simplexcore.utils.Constants;
import io.github.paldiu.simplexcore.utils.Utilities;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public abstract class CommandBase implements CommandExecutor, TabCompleter {
public boolean checkSender(CommandSender sender) {
return sender instanceof Player;
}
public Player getPlayer(String name) {
return Constants.getServer().getPlayer(name);
}
public Player getPlayer(UUID uuid) {
return Constants.getServer().getPlayer(uuid);
}
public void playerMsg(Player player, String... message) {
StringBuilder builder = new StringBuilder();
Utilities.primitiveFE(message, builder::append);
player.sendMessage(builder.toString());
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return new ArrayList<>();
}
}

View File

@ -1,5 +1,7 @@
package io.github.paldiu.simplexcore.command;
import org.bukkit.ChatColor;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@ -14,4 +16,6 @@ public @interface CommandInfo {
String aliases() default "";
String permission() default "simplex.core";
String permissionMessage() default "You do not have permission to use this command.";
}

View File

@ -1,22 +1,25 @@
package io.github.paldiu.simplexcore.command;
import io.github.paldiu.simplexcore.Constants;
import io.github.paldiu.simplexcore.utils.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.jetbrains.annotations.Nullable;
import org.reflections.Reflections;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.Arrays;
import java.util.Map;
import java.util.MissingResourceException;
public class CommandLoader {
private final Reflections reflections;
private Reflections reflections;
public CommandLoader(Class<?> clazz) {
public CommandLoader classpath(Class<?> clazz) {
if (!clazz.isAnnotationPresent(CommandInfo.class)) {
throw new MissingResourceException("Cannot register this class as the main resource location!", clazz.getSimpleName(), "@CommandInfo");
}
@ -26,6 +29,7 @@ public class CommandLoader {
}
reflections = new Reflections(clazz);
return this;
}
public void load() {
@ -33,21 +37,30 @@ public class CommandLoader {
CommandInfo info = annotated.getDeclaredAnnotation(CommandInfo.class);
if (info == null) return;
if (!CommandExecutor.class.isAssignableFrom(annotated)) return;
if (!CommandBase.class.isAssignableFrom(annotated)) return;
PluginCommand objectToRegister = Registry.create(Constants.getPlugin(), info.name());
objectToRegister.setDescription(info.description());
objectToRegister.setUsage(info.usage());
PluginCommand objectToRegister = Registry.create(Constants.getPlugin(), info.name().toLowerCase());
objectToRegister.setAliases(Arrays.asList(info.aliases().split(",")));
objectToRegister.setPermission(info.permission());
objectToRegister.setDescription(info.description());
objectToRegister.setExecutor(getFromSetName(info.name()));
objectToRegister.setLabel(info.name().toLowerCase());
objectToRegister.setPermission(info.permission());
objectToRegister.setPermissionMessage(info.permissionMessage());
objectToRegister.setTabCompleter(getTabFromName(info.name()));
objectToRegister.setUsage(info.usage());
Registry.registerCommand(objectToRegister);
});
}
public CommandExecutor getFromSetName(String name) {
for (Class<? extends CommandExecutor> obj : reflections.getSubTypesOf(CommandExecutor.class)) {
if (name.equalsIgnoreCase(obj.getSimpleName())) {
if (!obj.isAnnotationPresent(CommandInfo.class)) {
throw new RuntimeException("Missing annotation CommandInfo!");
}
CommandInfo info = obj.getDeclaredAnnotation(CommandInfo.class);
if (name.equalsIgnoreCase(info.name())) {
try {
Constructor<? extends CommandExecutor> constr = obj.getDeclaredConstructor();
return constr.newInstance();
@ -59,7 +72,26 @@ public class CommandLoader {
throw new RuntimeException("Unable to get a command executor! Terminating!");
}
@SuppressWarnings("unchecked")
@Nullable
public TabCompleter getTabFromName(String name) {
for (Class<? extends TabCompleter> obj : reflections.getSubTypesOf(TabCompleter.class)) {
if (!obj.isAnnotationPresent(CommandInfo.class)) {
throw new RuntimeException("Missing annotation CommandInfo!");
}
CommandInfo info = obj.getDeclaredAnnotation(CommandInfo.class);
if (name.equalsIgnoreCase(info.name())) {
try {
Constructor<? extends TabCompleter> constr = obj.getDeclaredConstructor();
return constr.newInstance();
} catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
return null;
}
private static class Registry {
private static final Constructor<PluginCommand> constructor;
private static final Field cmdMapField;
@ -107,14 +139,23 @@ public class CommandLoader {
CommandMap map = (CommandMap) cmdMapField.get(Bukkit.getPluginManager());
Map<String, Command> knownCommands = map.getKnownCommands();
if (knownCommands.containsKey(command.getName())) {
knownCommands.replace(command.getName(), command);
if (knownCommands.containsKey(command.getName().toLowerCase())) {
knownCommands.replace(command.getName().toLowerCase(), command);
}
map.register(command.getName(), command);
map.register(command.getName().toLowerCase(), command);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
private static final CommandLoader instance = new CommandLoader();
protected CommandLoader() {
}
public static CommandLoader getInstance() {
return instance;
}
}

View File

@ -0,0 +1,16 @@
package io.github.paldiu.simplexcore.command.defaults;
import io.github.paldiu.simplexcore.command.CommandBase;
import io.github.paldiu.simplexcore.command.CommandInfo;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@CommandInfo(name = "Info", description = "Gets info on this API / Library.", usage = "/<command>", permission = "simplex.core.info")
public class Command_info extends CommandBase {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
sender.sendMessage("This is an API!");
return true;
}
}

View File

@ -0,0 +1,27 @@
package io.github.paldiu.simplexcore.math;
import org.bukkit.Location;
public class Cuboid {
private final int x, y, z;
public Cuboid() {
this(3, 3, 3);
}
public Cuboid(int xSize, int ySize, int zSize) {
this.x = xSize;
this.y = ySize;
this.z = zSize;
}
public Cuboid(Size size) {
this(size.getX(), size.getY(), size.getZ());
}
public void generate(Location location) {
int t1 = location.getBlockX();
int t2 = location.getBlockY();
int t3 = location.getBlockZ();
}
}

View File

@ -0,0 +1,30 @@
package io.github.paldiu.simplexcore.math;
public enum Size {
SMALL(1, 1, 1),
MEDIUM(3, 3, 3),
LARGE(5, 5, 5),
EXTRA_LARGE(10, 10, 10),
HUGE(20, 20, 20),
ENORMOUS(50, 50, 50);
int x, y, z;
Size(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
}

View File

@ -1,4 +1,4 @@
package io.github.paldiu.simplexcore.registry;
package io.github.paldiu.simplexcore.plugin;
import org.bukkit.plugin.java.JavaPlugin;

View File

@ -0,0 +1,21 @@
package io.github.paldiu.simplexcore.plugin;
import io.github.paldiu.simplexcore.utils.Constants;
import io.github.paldiu.simplexcore.utils.Utilities;
public class AddonManager {
public AddonManager() { }
public void disable(Addon<?> addon) {
Constants.getManager().disablePlugin(addon);
}
public void enable(Addon<?> addon) {
Constants.getManager().enablePlugin(addon);
}
public void reload(Addon<?> addon) {
disable(addon);
enable(addon);
}
}

View File

@ -0,0 +1,24 @@
package io.github.paldiu.simplexcore.plugin;
import java.util.HashSet;
import java.util.Set;
public class AddonRegistry {
private final Set<Addon<?>> components = new HashSet<>();
private static final AddonRegistry instance = new AddonRegistry();
protected AddonRegistry() {
}
public static AddonRegistry getInstance() {
return instance;
}
public <T extends Addon<T>> void register(T addon) {
getComponents().add(addon);
}
public Set<Addon<?>> getComponents() {
return components;
}
}

View File

@ -1,19 +0,0 @@
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;
}
}

View File

@ -0,0 +1,17 @@
package io.github.paldiu.simplexcore.utils;
public class Bean<T> {
protected T bean;
public Bean(T bean) {
this.bean = bean;
}
public void setBean(T bean) {
this.bean = bean;
}
public T getBean() {
return bean;
}
}

View File

@ -1,6 +1,8 @@
package io.github.paldiu.simplexcore;
package io.github.paldiu.simplexcore.utils;
import io.github.paldiu.simplexcore.registry.AddonRegistry;
import io.github.paldiu.simplexcore.SimplexCore;
import io.github.paldiu.simplexcore.command.CommandLoader;
import io.github.paldiu.simplexcore.plugin.AddonRegistry;
import org.bukkit.Server;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
@ -14,7 +16,6 @@ public final class Constants {
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;
@ -37,6 +38,10 @@ public final class Constants {
}
public static AddonRegistry getRegistry() {
return registry;
return AddonRegistry.getInstance();
}
public static CommandLoader getCommandLoader() {
return CommandLoader.getInstance();
}
}

View File

@ -0,0 +1,25 @@
package io.github.paldiu.simplexcore.utils;
public class Trio<A, B, C> {
private final A primary;
private final B secondary;
private final C tertiary;
public Trio(A primary, B secondary, C tertiary) {
this.primary = primary;
this.secondary = secondary;
this.tertiary = tertiary;
}
public A getPrimary() {
return primary;
}
public B getSecondary() {
return secondary;
}
public C getTertiary() {
return tertiary;
}
}

View File

@ -0,0 +1,32 @@
package io.github.paldiu.simplexcore.utils;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Utilities {
private static <T> Stream<T> feStr(T[] array) {
return Arrays.stream(array);
}
private static <T> Set<T> feCol(Stream<T> stream) {
return stream.collect(Collectors.toSet());
}
public static <T> void primitiveFE(T[] array, Consumer<? super T> action) {
feCol(feStr(array)).forEach(action);
}
public static <T> void smartFE(Collection<T> collection, Consumer<? super T> action) {
collection.forEach(action);
}
public static <K, V> void mapFE(Map<K, V> map, BiConsumer<K, V> actions) {
map.forEach(actions);
}
}