From 287dffee434ce3711b07c8596dff4bdd65883c85 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Sun, 15 Nov 2020 12:50:05 +0100 Subject: [PATCH] Added tab complete --- .../themeparkplus/ThemeParkPlus.java | 6 +- .../commands/TPPTabComplete.java | 100 ++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPTabComplete.java diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java index 3586349..3d85318 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java @@ -4,6 +4,7 @@ import club.minnced.discord.webhook.WebhookClient; import club.minnced.discord.webhook.WebhookClientBuilder; import net.milkbowl.vault.economy.Economy; import nl.sbdeveloper.themeparkplus.commands.TPPCMD; +import nl.sbdeveloper.themeparkplus.commands.TPPTabComplete; import nl.sbdeveloper.themeparkplus.listeners.*; import nl.sbdeveloper.themeparkplus.managers.DBManager; import nl.sbdeveloper.themeparkplus.sbutils.UpdateManager; @@ -135,7 +136,8 @@ public final class ThemeParkPlus extends JavaPlugin { } Bukkit.getLogger().info("[ThemeParkPlus] Loading commands..."); - Objects.requireNonNull(getCommand("themeparkplus"), "Couldn't read command from plugin.yml!").setExecutor(new TPPCMD()); + getCommand("themeparkplus").setExecutor(new TPPCMD()); + getCommand("themeparkplus").setTabCompleter(new TPPTabComplete()); Bukkit.getLogger().info("[ThemeParkPlus] Loading listeners..."); Bukkit.getPluginManager().registerEvents(new DirectionalGateListener(), this); @@ -155,7 +157,7 @@ public final class ThemeParkPlus extends JavaPlugin { WebhookClientBuilder builder = new WebhookClientBuilder(URL); builder.setThreadFactory((job) -> { Thread thread = new Thread(job); - thread.setName("Hello"); + thread.setName("ThemeParkPlus"); thread.setDaemon(true); return thread; }); diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPTabComplete.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPTabComplete.java new file mode 100644 index 0000000..b5926d5 --- /dev/null +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPTabComplete.java @@ -0,0 +1,100 @@ +package nl.sbdeveloper.themeparkplus.commands; + +import nl.iobyte.themepark.api.API; +import nl.sbdeveloper.themeparkplus.api.enums.WalkingDirection; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; +import org.bukkit.util.StringUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class TPPTabComplete implements TabCompleter { + private static final List COMMANDS = Arrays.asList("info", "help", "opengate", "closegate", "lampon", "lampoff", "lampson", "lampsoff", "redstonetimer", "givefpticket"); + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) { + //Aliases + if (label.equalsIgnoreCase("themeparkplus") || label.equalsIgnoreCase("tpp") || label.equalsIgnoreCase("themeparkp")) { + if (args.length == 1) { + return StringUtil.copyPartialMatches(args[0], COMMANDS, new ArrayList<>()); + } else if (args.length == 2) { + if (args[0].equalsIgnoreCase("opengate") + || args[0].equalsIgnoreCase("closegate") + || args[0].equalsIgnoreCase("lampon") + || args[0].equalsIgnoreCase("lampoff") + || args[0].equalsIgnoreCase("redstonetimer")) { + if (sender instanceof Player) { + Player p = (Player) sender; + Location targetLoc = getTarget(p).getLocation(); + return StringUtil.copyPartialMatches(args[1], Collections.singletonList("" + targetLoc.getWorld().getName()), new ArrayList<>()); + } + } else if (args[0].equalsIgnoreCase("givefpticket")) { + return StringUtil.copyPartialMatches(args[1], API.getAttractions().keySet(), new ArrayList<>()); + } + } else if (args.length == 3) { + if (args[0].equalsIgnoreCase("opengate") + || args[0].equalsIgnoreCase("closegate") + || args[0].equalsIgnoreCase("lampon") + || args[0].equalsIgnoreCase("lampoff") + || args[0].equalsIgnoreCase("redstonetimer")) { + if (sender instanceof Player) { + Player p = (Player) sender; + Location targetLoc = getTarget(p).getLocation(); + return StringUtil.copyPartialMatches(args[2], Collections.singletonList("" + targetLoc.getBlockX()), new ArrayList<>()); + } + } else if (args[0].equalsIgnoreCase("givefpticket")) { + return StringUtil.copyPartialMatches(args[1], Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()), new ArrayList<>()); + } + } else if (args.length == 4) { + if (args[0].equalsIgnoreCase("opengate") + || args[0].equalsIgnoreCase("closegate") + || args[0].equalsIgnoreCase("lampon") + || args[0].equalsIgnoreCase("lampoff") + || args[0].equalsIgnoreCase("redstonetimer")) { + if (sender instanceof Player) { + Player p = (Player) sender; + Location targetLoc = getTarget(p).getLocation(); + return StringUtil.copyPartialMatches(args[3], Collections.singletonList("" + targetLoc.getBlockY()), new ArrayList<>()); + } + } + } else if (args.length == 5) { + if (args[0].equalsIgnoreCase("opengate") + || args[0].equalsIgnoreCase("closegate") + || args[0].equalsIgnoreCase("lampon") + || args[0].equalsIgnoreCase("lampoff") + || args[0].equalsIgnoreCase("redstonetimer")) { + if (sender instanceof Player) { + Player p = (Player) sender; + Location targetLoc = getTarget(p).getLocation(); + return StringUtil.copyPartialMatches(args[4], Collections.singletonList("" + targetLoc.getBlockZ()), new ArrayList<>()); + } + } + } else if (args.length == 6) { + if (args[0].equalsIgnoreCase("opengate")) { + return StringUtil.copyPartialMatches(args[5], Arrays.stream(WalkingDirection.values()).map(Enum::toString).collect(Collectors.toList()), new ArrayList<>()); + } + } else if (args.length == 7) { + if (args[0].equalsIgnoreCase("opengate")) { + return StringUtil.copyPartialMatches(args[6], Arrays.stream(WalkingDirection.values()).map(Enum::toString).collect(Collectors.toList()), new ArrayList<>()); + } + } + } + + return null; + } + + private Block getTarget(Player p) { + return p.getTargetBlock(null, 200); + } +}