initial commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.alttd.altitudequests;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class AQuests extends JavaPlugin {
|
||||
|
||||
public static AQuests instance;
|
||||
|
||||
public static AQuests getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Plugin startup logic
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.alttd.altitudequests.commands;
|
||||
|
||||
import com.alttd.altitudequests.AQuests;
|
||||
import com.alttd.altitudequests.commands.subcommands.CommandHelp;
|
||||
import com.alttd.altitudequests.commands.subcommands.CommandReload;
|
||||
import com.alttd.altitudequests.config.Config;
|
||||
import com.alttd.altitudequests.util.Logger;
|
||||
import org.bukkit.command.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandManager implements CommandExecutor, TabExecutor {
|
||||
private final List<SubCommand> subCommands;
|
||||
|
||||
public CommandManager() {
|
||||
AQuests aQuests = AQuests.getInstance();
|
||||
|
||||
PluginCommand command = aQuests.getCommand("aquest");
|
||||
if (command == null) {
|
||||
subCommands = null;
|
||||
Logger.severe("Unable to find AltitudeQuests command.");
|
||||
return;
|
||||
}
|
||||
command.setExecutor(this);
|
||||
command.setTabCompleter(this);
|
||||
|
||||
subCommands = Arrays.asList(
|
||||
new CommandHelp(this),
|
||||
new CommandReload());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
|
||||
SubCommand subCommand;
|
||||
if (args.length == 0)
|
||||
subCommand = getSubCommand("help");
|
||||
else
|
||||
subCommand = getSubCommand(args[0]);
|
||||
|
||||
if (!commandSender.hasPermission(subCommand.getPermission())) {
|
||||
commandSender.sendMiniMessage(Config.NO_PERMISSION, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
return subCommand.onCommand(commandSender, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
|
||||
List<String> res = new ArrayList<>();
|
||||
|
||||
if (args.length <= 1) {
|
||||
res.addAll(subCommands.stream()
|
||||
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
||||
.map(SubCommand::getName)
|
||||
.filter(name -> args.length == 0 || name.startsWith(args[0]))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
} else {
|
||||
SubCommand subCommand = getSubCommand(args[0]);
|
||||
if (subCommand != null && commandSender.hasPermission(subCommand.getPermission()))
|
||||
res.addAll(subCommand.getTabComplete(commandSender, args));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<SubCommand> getSubCommands() {
|
||||
return subCommands;
|
||||
}
|
||||
|
||||
private SubCommand getSubCommand(String cmdName) {
|
||||
return subCommands.stream()
|
||||
.filter(subCommand -> subCommand.getName().equals(cmdName))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.alttd.altitudequests.commands;
|
||||
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class SubCommand {
|
||||
|
||||
private final MiniMessage miniMessage;
|
||||
|
||||
public SubCommand() {
|
||||
miniMessage = MiniMessage.miniMessage();
|
||||
}
|
||||
|
||||
public abstract boolean onCommand(CommandSender commandSender, String[] args);
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public String getPermission() {
|
||||
return "aquests." + getName();
|
||||
}
|
||||
|
||||
public abstract List<String> getTabComplete(CommandSender commandSender, String[] args);
|
||||
|
||||
public abstract String getHelpMessage();
|
||||
|
||||
protected MiniMessage getMiniMessage() {
|
||||
return miniMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.alttd.altitudequests.commands.subcommands;
|
||||
|
||||
import com.alttd.altitudequests.commands.CommandManager;
|
||||
import com.alttd.altitudequests.commands.SubCommand;
|
||||
import com.alttd.altitudequests.config.Config;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandHelp extends SubCommand {
|
||||
|
||||
private final CommandManager commandManager;
|
||||
|
||||
public CommandHelp(CommandManager commandManager) {
|
||||
super();
|
||||
this.commandManager = commandManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER.replaceAll("<commands>", commandManager
|
||||
.getSubCommands().stream()
|
||||
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
||||
.map(SubCommand::getHelpMessage)
|
||||
.collect(Collectors.joining("\n"))), null);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "help";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Config.HELP_MESSAGE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.alttd.altitudequests.commands.subcommands;
|
||||
|
||||
import com.alttd.altitudequests.commands.SubCommand;
|
||||
import com.alttd.altitudequests.config.Config;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandReload extends SubCommand {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
Config.reload();
|
||||
commandSender.sendMiniMessage("<green>Reloaded AltitudeQuests config.</green>", null);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "reload";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Config.RELOAD_HELP_MESSAGE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.alttd.altitudequests.config;
|
||||
|
||||
import com.alttd.altitudequests.AQuests;
|
||||
import com.alttd.altitudequests.util.Logger;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings({"unused", "SameParameterValue"})
|
||||
abstract class AbstractConfig {
|
||||
File file;
|
||||
YamlConfiguration yaml;
|
||||
|
||||
AbstractConfig(String filename) {
|
||||
init(new File(AQuests.getInstance().getDataFolder(), filename), filename);
|
||||
}
|
||||
|
||||
AbstractConfig(File file, String filename) {
|
||||
init(new File(file.getPath() + File.separator + filename), filename);
|
||||
}
|
||||
|
||||
private void init(File file, String filename) {
|
||||
this.file = file;
|
||||
this.yaml = new YamlConfiguration();
|
||||
try {
|
||||
yaml.load(file);
|
||||
} catch (IOException ignore) {
|
||||
} catch (InvalidConfigurationException ex) {
|
||||
Logger.severe(String.format("Could not load %s, please correct your syntax errors", filename));
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
yaml.options().copyDefaults(true);
|
||||
}
|
||||
|
||||
void readConfig(Class<?> clazz, Object instance) {
|
||||
for (Method method : clazz.getDeclaredMethods()) {
|
||||
if (Modifier.isPrivate(method.getModifiers())) {
|
||||
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.invoke(instance);
|
||||
} catch (InvocationTargetException ex) {
|
||||
throw new RuntimeException(ex.getCause());
|
||||
} catch (Exception ex) {
|
||||
Logger.severe("Error invoking %.", method.toString());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
save();
|
||||
}
|
||||
|
||||
private void save() {
|
||||
try {
|
||||
yaml.save(file);
|
||||
} catch (IOException ex) {
|
||||
Logger.severe("Could not save %.", file.toString());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void set(String path, Object val) {
|
||||
yaml.addDefault(path, val);
|
||||
yaml.set(path, val);
|
||||
save();
|
||||
}
|
||||
|
||||
String getString(String path, String def) {
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getString(path, yaml.getString(path));
|
||||
}
|
||||
|
||||
boolean getBoolean(String path, boolean def) {
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getBoolean(path, yaml.getBoolean(path));
|
||||
}
|
||||
|
||||
int getInt(String path, int def) {
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getInt(path, yaml.getInt(path));
|
||||
}
|
||||
|
||||
double getDouble(String path, double def) {
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getDouble(path, yaml.getDouble(path));
|
||||
}
|
||||
|
||||
<T> List<?> getList(String path, T def) {
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getList(path, yaml.getList(path));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
<T> Map<String, T> getMap(final @NonNull String path, final @Nullable Map<String, T> def) {
|
||||
final ImmutableMap.Builder<String, T> builder = ImmutableMap.builder();
|
||||
if (def != null && yaml.getConfigurationSection(path) == null) {
|
||||
yaml.addDefault(path, def.isEmpty() ? new HashMap<>() : def);
|
||||
return def;
|
||||
}
|
||||
final ConfigurationSection section = yaml.getConfigurationSection(path);
|
||||
if (section != null) {
|
||||
for (String key : section.getKeys(false)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
final T val = (T) section.get(key);
|
||||
if (val != null) {
|
||||
builder.put(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
ConfigurationSection getConfigurationSection(String path) {
|
||||
return yaml.getConfigurationSection(path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.alttd.altitudequests.config;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public final class Config extends AbstractConfig {
|
||||
|
||||
static Config config;
|
||||
static int version;
|
||||
public Config() {
|
||||
super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "AltitudeQuests"), "config.yml");
|
||||
}
|
||||
|
||||
public static void reload() {
|
||||
config = new Config();
|
||||
|
||||
version = config.getInt("config-version", 1);
|
||||
config.set("config-version", 1);
|
||||
|
||||
config.readConfig(Config.class, null);
|
||||
}
|
||||
|
||||
public static String HELP_MESSAGE_WRAPPER = "<gold>AltitudeQuests help:\n<commands></gold>";
|
||||
public static String HELP_MESSAGE = "<green>Show this menu: <gold>/aquest help</gold></green>";
|
||||
public static String RELOAD_HELP_MESSAGE = "<green>Reload configs: <gold>/aquest reload</gold></green>";
|
||||
|
||||
private static void loadHelp() {
|
||||
HELP_MESSAGE_WRAPPER = config.getString("help.help-wrapper", HELP_MESSAGE_WRAPPER);
|
||||
HELP_MESSAGE = config.getString("help.help", HELP_MESSAGE);
|
||||
RELOAD_HELP_MESSAGE = config.getString("help.reload", RELOAD_HELP_MESSAGE);
|
||||
}
|
||||
|
||||
public static String NO_PERMISSION = "<red>You do not have permission to do that.</red>";
|
||||
public static String NO_CONSOLE = "<red>You cannot use this command from console.</red>";
|
||||
|
||||
private static void loadGeneric() {
|
||||
NO_PERMISSION = config.getString("generic.no-permission", NO_PERMISSION);
|
||||
NO_CONSOLE = config.getString("generic.no-console", NO_CONSOLE);
|
||||
}
|
||||
|
||||
private static void loadMessages() {
|
||||
}
|
||||
|
||||
private static void loadGUIText() {
|
||||
}
|
||||
|
||||
public static boolean DEBUG = false;
|
||||
|
||||
private static void loadSettings() {
|
||||
DEBUG = config.getBoolean("settings.debug", DEBUG);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.alttd.altitudequests.util;
|
||||
|
||||
import com.alttd.altitudequests.AQuests;
|
||||
|
||||
public class Logger {
|
||||
|
||||
static private final java.util.logging.Logger logger;
|
||||
|
||||
static {
|
||||
logger = AQuests.getInstance().getLogger();
|
||||
}
|
||||
|
||||
public static void info(String info, String... variables)
|
||||
{
|
||||
for (String variable : variables) {
|
||||
info = info.replaceFirst("%", variable);
|
||||
}
|
||||
logger.info(info);
|
||||
}
|
||||
|
||||
public static void warning(String warning, String... variables)
|
||||
{
|
||||
for (String variable : variables) {
|
||||
warning = warning.replaceFirst("%", variable);
|
||||
}
|
||||
logger.warning(warning);
|
||||
}
|
||||
|
||||
public static void severe(String severe, String... variables)
|
||||
{
|
||||
for (String variable : variables) {
|
||||
severe = severe.replaceFirst("%", variable);
|
||||
}
|
||||
logger.severe(severe);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
name: AltitudeQuests
|
||||
version: '${version}'
|
||||
main: com.alttd.altitudequests.AltitudeQuests
|
||||
api-version: 1.18
|
||||
authors: [ Teriuihi ]
|
||||
description: A plugin to run quests on a server
|
||||
commands:
|
||||
aquest:
|
||||
permission: aquest.use
|
||||
Reference in New Issue
Block a user