Use Commodore for rich command completion registering

feat/data-edit-commands
William 3 years ago
parent d1c95030f0
commit 2f700b2d93

@ -7,6 +7,10 @@ dependencies {
}
shadowJar {
dependencies {
exclude(dependency('com.mojang:brigadier'))
}
relocate 'org.apache', 'net.william278.husksync.libraries'
relocate 'dev.dejvokep', 'net.william278.husksync.libraries'
relocate 'de.themoep', 'net.william278.husksync.libraries'
@ -16,6 +20,7 @@ shadowJar {
relocate 'com.google', 'net.william278.husksync.libraries'
relocate 'redis.clients', 'net.william278.husksync.libraries'
relocate 'org.json', 'net.william278.husksync.libraries.json'
relocate 'me.lucko.commodore', 'net.william278.husksync.libraries.commodore'
relocate 'net.byteflux.libby', 'net.william278.husksync.libraries.libby'
relocate 'org.bstats', 'net.william278.husksync.libraries.bstats'

@ -31,11 +31,12 @@ allprojects {
maven { url 'https://repo.minebench.de/' }
maven { url 'https://repo.alessiodp.com/releases/' }
maven { url 'https://jitpack.io' }
maven { url 'https://libraries.minecraft.net/' }
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}
test {

@ -3,11 +3,12 @@ dependencies {
implementation 'org.bstats:bstats-bukkit:3.0.0'
implementation 'net.william278:mpdbdataconverter:1.0.1'
implementation 'net.william278:hsldataconverter:1.0'
implementation 'me.lucko:commodore:2.2'
compileOnly 'redis.clients:jedis:4.2.3'
compileOnly 'commons-io:commons-io:2.11.0'
compileOnly 'de.themoep:minedown:1.7.1-SNAPSHOT'
compileOnly 'dev.dejvokep:boosted-yaml:1.2'
compileOnly 'dev.dejvokep:boosted-yaml:1.3'
compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
compileOnly 'com.zaxxer:HikariCP:5.0.1'
@ -15,6 +16,10 @@ dependencies {
}
shadowJar {
dependencies {
exclude(dependency('com.mojang:brigadier'))
}
relocate 'org.apache', 'net.william278.husksync.libraries'
relocate 'de.themoep', 'net.william278.husksync.libraries'
relocate 'org.jetbrains', 'net.william278.husksync.libraries'
@ -23,6 +28,7 @@ shadowJar {
relocate 'com.google', 'net.william278.husksync.libraries'
relocate 'redis.clients', 'net.william278.husksync.libraries'
relocate 'org.json', 'net.william278.husksync.libraries.json'
relocate 'me.lucko.commodore', 'net.william278.husksync.libraries.commodore'
relocate 'net.byteflux.libby', 'net.william278.husksync.libraries.libby'
relocate 'org.bstats', 'net.william278.husksync.libraries.bstats'

@ -268,6 +268,12 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
return logger;
}
@NotNull
@Override
public ResourceReader getResourceReader() {
return resourceReader;
}
@Override
public @NotNull Version getPluginVersion() {
return Version.pluginVersion(getDescription().getVersion());

@ -0,0 +1,32 @@
package net.william278.husksync.command;
import me.lucko.commodore.CommodoreProvider;
import me.lucko.commodore.file.CommodoreFileReader;
import net.william278.husksync.BukkitHuskSync;
import org.bukkit.command.PluginCommand;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
/**
* Used for registering Brigadier hooks on platforms that support commodore for rich command syntax
*/
public class BrigadierUtil {
protected static void registerCommodore(@NotNull BukkitHuskSync plugin, @NotNull PluginCommand pluginCommand,
@NotNull CommandBase command) {
// Register command descriptions via commodore (brigadier wrapper)
try (InputStream pluginFile = plugin.getResourceReader()
.getResource("commodore/" + command.command + ".commodore")) {
CommodoreProvider.getCommodore(plugin).register(pluginCommand,
CommodoreFileReader.INSTANCE.parse(pluginFile),
player -> player.hasPermission(command.permission));
} catch (IOException e) {
plugin.getLoggingAdapter().log(Level.SEVERE,
"Failed to load " + command.command + ".commodore command definitions", e);
}
}
}

@ -1,6 +1,7 @@
package net.william278.husksync.command;
import net.william278.husksync.HuskSync;
import me.lucko.commodore.CommodoreProvider;
import net.william278.husksync.BukkitHuskSync;
import net.william278.husksync.player.BukkitPlayer;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
@ -18,14 +19,14 @@ public class BukkitCommand implements CommandExecutor, TabExecutor {
/**
* The {@link CommandBase} that will be executed
*/
private final CommandBase command;
protected final CommandBase command;
/**
* The implementing plugin
*/
private final HuskSync plugin;
private final BukkitHuskSync plugin;
public BukkitCommand(@NotNull CommandBase command, @NotNull HuskSync implementor) {
public BukkitCommand(@NotNull CommandBase command, @NotNull BukkitHuskSync implementor) {
this.command = command;
this.plugin = implementor;
}
@ -40,6 +41,9 @@ public class BukkitCommand implements CommandExecutor, TabExecutor {
pluginCommand.setTabCompleter(this);
pluginCommand.setPermission(command.permission);
pluginCommand.setDescription(command.getDescription());
if (CommodoreProvider.isSupported()) {
BrigadierUtil.registerCommodore(plugin, pluginCommand, command);
}
}
@Override

@ -3,7 +3,6 @@ package net.william278.husksync.util;
import net.william278.husksync.BukkitHuskSync;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.InputStream;
import java.util.Objects;
@ -20,9 +19,4 @@ public class BukkitResourceReader implements ResourceReader {
return Objects.requireNonNull(plugin.getResource(fileName));
}
@Override
public @NotNull File getDataFolder() {
return plugin.getDataFolder();
}
}

@ -0,0 +1,3 @@
inventory {
name brigadier:string single_word;
}

@ -0,0 +1,5 @@
husksync {
update;
about;
reload;
}

@ -0,0 +1,3 @@
enderchest {
name brigadier:string single_word;
}

@ -0,0 +1,29 @@
userdata {
view {
name brigadier:string single_word {
version brigadier:string single_word;
test;
}
}
list {
name brigadier:string single_word;
test;
}
delete {
name brigadier:string single_word {
version brigadier:string single_word;
test;
}
}
restore {
name brigadier:string single_word {
version brigadier:string single_word;
test;
}
}
pin {
name brigadier:string single_word {
version brigadier:string single_word;
}
}
}

@ -11,7 +11,7 @@ softdepend:
libraries:
- 'mysql:mysql-connector-java:8.0.29'
- 'org.xerial.snappy:snappy-java:1.1.8.4'
- 'dev.dejvokep:boosted-yaml:1.2'
- 'dev.dejvokep:boosted-yaml:1.3'
commands:
husksync:
usage: '/husksync <update/info/reload/migrate>'

@ -9,7 +9,7 @@ dependencies {
exclude module: 'slf4j-api'
}
compileOnly 'dev.dejvokep:boosted-yaml:1.2'
compileOnly 'dev.dejvokep:boosted-yaml:1.3'
compileOnly 'org.xerial.snappy:snappy-java:1.1.8.4'
compileOnly 'org.jetbrains:annotations:23.0.0'
compileOnly 'com.github.plan-player-analytics:Plan:5.4.1690'

@ -10,6 +10,7 @@ import net.william278.husksync.migrator.Migrator;
import net.william278.husksync.player.OnlineUser;
import net.william278.husksync.redis.RedisManager;
import net.william278.husksync.util.Logger;
import net.william278.husksync.util.ResourceReader;
import net.william278.husksync.util.Version;
import org.jetbrains.annotations.NotNull;
@ -114,6 +115,14 @@ public interface HuskSync {
@NotNull
Logger getLoggingAdapter();
/**
* Returns the plugin resource file reader
*
* @return the {@link ResourceReader}
*/
@NotNull
ResourceReader getResourceReader();
/**
* Returns the plugin version
*

@ -9,6 +9,7 @@ import net.william278.husksync.util.UpdateChecker;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
@ -126,9 +127,12 @@ public class HuskSyncCommand extends CommandBase implements TabCompletable, Cons
@Override
public List<String> onTabComplete(@NotNull String[] args) {
return Arrays.stream(COMMAND_ARGUMENTS)
.filter(argument -> argument.startsWith(args.length >= 1 ? args[0] : ""))
.sorted().collect(Collectors.toList());
if (args.length <= 1) {
return Arrays.stream(COMMAND_ARGUMENTS)
.filter(argument -> argument.startsWith(args.length >= 1 ? args[0] : ""))
.sorted().collect(Collectors.toList());
}
return Collections.emptyList();
}
private void displayPluginInformation(@NotNull OnlineUser player) {

@ -225,7 +225,7 @@ public class UserDataCommand extends CommandBase implements TabCompletable {
switch (args.length) {
case 0, 1 -> {
return Arrays.stream(COMMAND_ARGUMENTS)
.filter(argument -> argument.startsWith(args.length >= 1 ? args[0] : ""))
.filter(argument -> argument.startsWith(args.length == 1 ? args[0] : ""))
.sorted().collect(Collectors.toList());
}
case 2 -> {

@ -14,6 +14,7 @@ import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@ -108,7 +109,7 @@ public abstract class Database {
*/
@SuppressWarnings("SameParameterValue")
protected final String[] getSchemaStatements(@NotNull String schemaFileName) throws IOException {
return formatStatementTables(new String(resourceReader.getResource(schemaFileName)
return formatStatementTables(new String(Objects.requireNonNull(resourceReader.getResource(schemaFileName))
.readAllBytes(), StandardCharsets.UTF_8)).split(";");
}

@ -1,8 +1,7 @@
package net.william278.husksync.util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.InputStream;
/**
@ -14,15 +13,8 @@ public interface ResourceReader {
* Gets the resource with given filename and reads it as an {@link InputStream}
*
* @param fileName Name of the resource file to read
* @return The resource, read as an {@link InputStream}
* @return The resource, read as an {@link InputStream}; or {@code null} if the resource was not found
*/
@NotNull InputStream getResource(String fileName);
/**
* Gets the plugin data folder where plugin configuration and data are kept
*
* @return the plugin data directory
*/
@NotNull File getDataFolder();
@Nullable InputStream getResource(String fileName);
}

Loading…
Cancel
Save