Merge pull request #12 from MidasSword/enchantinfo-autocomplete

/enchantinfo autocomplete
This commit is contained in:
Auxilor 2020-10-12 08:54:30 +01:00 committed by GitHub
commit a6e9fb1faf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 105 additions and 4 deletions

38
.github/workflows/compile.yml vendored Normal file
View File

@ -0,0 +1,38 @@
name: Test EcoEnchants
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
name: Build Maven project
steps:
- uses: actions/checkout@v1
- uses: actions/cache@v2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
# - name: Build Spigot
# # Takes 3 minutes
# # We only build 1.15 because it is the only Spigot version that is not
# # available on their Maven repo
# run: |
# cd ..
# wget https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar
# java -jar BuildTools.jar --rev 1.15
- name: Set up JDK 8
uses: actions/setup-java@v1
with:
java-version: 8
- name: Build EcoEnchants
run: |
mvn initialize --batch-mode
mvn package install --batch-mode

View File

@ -16,7 +16,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.15-R0.1-SNAPSHOT</version>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -22,7 +22,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.15-R0.1-SNAPSHOT</version>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -6,6 +6,7 @@ import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import java.util.Arrays;
@ -43,7 +44,9 @@ public abstract class AbstractCommand implements CommandExecutor, Registerable {
@Override
public final void register() {
Bukkit.getPluginCommand(name).setExecutor(this);
PluginCommand pluginCommand = Bukkit.getPluginCommand(name);
pluginCommand.setExecutor(this);
pluginCommand.setTabCompleter(new EcoEnchantsTabCompleter());
}
public abstract void onExecute(CommandSender sender, List<String> args);

View File

@ -0,0 +1,60 @@
package com.willfp.ecoenchants.command;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class EcoEnchantsTabCompleter implements TabCompleter {
private final List<String> enchantsNames;
public EcoEnchantsTabCompleter() {
// Fetching the list of all enchants takes longer than anything inside onTabComplete, so caching is effective
enchantsNames = EcoEnchants.getAll().stream().map(EcoEnchant::getName).collect(Collectors.toList());
}
private static String removePrefix(String s, String prefix) {
if (s != null && prefix != null && s.startsWith(prefix)) {
return s.substring(prefix.length());
}
return s;
}
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
try {
if (command.getLabel().equalsIgnoreCase("enchantinfo") && (sender.isOp() || sender.hasPermission("ecoenchants.enchantinfo"))) {
List<String> completions = new ArrayList<>();
if (args.length == 0) {
// Currently, this case is not ever reached
return enchantsNames;
}
StringUtil.copyPartialMatches(String.join(" ", args), enchantsNames, completions);
if (args.length > 1) { // Remove all previous words from the candidate of completions
ArrayList<String> finishedArgs = new ArrayList<>(Arrays.asList(args));
finishedArgs.remove(args.length - 1);
String prefix = String.join(" ", finishedArgs);
completions = completions.stream().map(enchantName -> removePrefix(enchantName, prefix).trim()).collect(Collectors.toList());
}
Collections.sort(completions);
return completions;
}
} catch (Exception ignored) {}
return null;
}
}

View File

@ -126,7 +126,6 @@ public final class CommandEnchantinfo extends AbstractCommand {
final String finalTargets = allTargets;
final String finalConflicts = allConflicts;
final String finalMaxLevel = maxLevel;
Arrays.asList(ConfigManager.getLang().getMessage("enchantinfo").split("\\r?\\n")).forEach((string -> {
string = string.replaceAll("%name%", finalName)
.replaceAll("%description%", finalDescription)

View File

@ -5,6 +5,7 @@ import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.command.commands.CommandEcodebug;
import com.willfp.ecoenchants.command.commands.CommandEcoreload;
import com.willfp.ecoenchants.command.commands.CommandEnchantinfo;
import com.willfp.ecoenchants.command.EcoEnchantsTabCompleter;
import com.willfp.ecoenchants.config.ConfigManager;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.packets.PacketOpenWindowMerchant;