From 412bc4f07f2bce14deb193ae5c492b302496223e Mon Sep 17 00:00:00 2001 From: Sekwah Date: Sat, 2 Dec 2023 20:22:59 +0000 Subject: [PATCH] fix: allow null values for auto complete suggestions --- .../core/AdvancedPortalsCore.java | 2 + .../portal/CreatePortalSubCommand.java | 25 +++++++-- .../core/registry/TagRegistry.java | 20 +++---- .../core/tags/activation/DestiTag.java | 4 +- .../core/tags/activation/NameTag.java | 54 +++++++++++++++++++ .../advancedportals/core/warphandler/Tag.java | 3 ++ 6 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java diff --git a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java index ebf13cde..6f019925 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -11,6 +11,7 @@ import com.sekwah.advancedportals.core.module.AdvancedPortalsModule; import com.sekwah.advancedportals.core.repository.ConfigRepository; import com.sekwah.advancedportals.core.tags.activation.DestiTag; +import com.sekwah.advancedportals.core.tags.activation.NameTag; import com.sekwah.advancedportals.core.util.InfoLogger; import com.sekwah.advancedportals.core.util.Lang; @@ -72,6 +73,7 @@ public void onEnable() { } private void registerTags() { + this.tagRegistry.registerTag(new NameTag()); this.tagRegistry.registerTag(new DestiTag()); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java index 446bb604..9046080a 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java @@ -88,7 +88,23 @@ public List onTabComplete(CommandSenderContainer sender, String[] args) if(split.length == 2 || (split.length == 1 && lastArg.endsWith(":"))) { // Loop over tags in allTags and check if the first half of split is equal to the tag name or alias for(Tag tag : allTags) { - if(tag instanceof Tag.AutoComplete autoComplete) { + // Check if the last tag starts with the tag name or alias + var startsWith = false; + if(lastArg.startsWith(tag.getName())) { + startsWith = true; + } else { + var aliases = tag.getAliases(); + if(aliases != null) { + for (String alias : aliases) { + if(lastArg.startsWith(alias)) { + startsWith = true; + break; + } + } + } + } + + if(tag instanceof Tag.AutoComplete autoComplete && startsWith) { var tagSuggestions = autoComplete.autoComplete(split.length == 2 ? split[1] : ""); if(tagSuggestions != null) { // Loop over suggestions and add split[0] + ":" to the start @@ -98,7 +114,7 @@ public List onTabComplete(CommandSenderContainer sender, String[] args) } } } - // This is returning right but something is going wrong with "desti:A" whenever anything is typed after : + return suggestions; } } @@ -120,7 +136,10 @@ public List onTabComplete(CommandSenderContainer sender, String[] args) return true; }).forEach(tag -> { suggestions.add(tag.getName()); - suggestions.addAll(Arrays.stream(tag.getAliases()).toList()); + var aliases = tag.getAliases(); + if(aliases != null) { + suggestions.addAll(Arrays.stream(aliases).toList()); + } }); // Loop over all suggestions and add : to the end diff --git a/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java b/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java index 4406ae28..30eecc9f 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java @@ -69,16 +69,18 @@ public boolean registerTag(Tag tag) { return false; } - for (String alias : tag.getAliases()) { - if(this.literalTags.contains(alias)) { - this.portalsCore.getInfoLogger().logWarning("A tag with the alias " + alias + " already exists."); - return false; + var aliases = tag.getAliases(); + this.literalTags.add(tagName); + if(aliases != null) { + for (String alias : aliases) { + if(this.literalTags.contains(alias)) { + this.portalsCore.getInfoLogger().logWarning("A tag with the alias " + alias + " already exists."); + return false; + } } - } - // Add name and aliases to literalTags to check for clashes - this.literalTags.add(tagName); - Collections.addAll(this.literalTags, tag.getAliases()); + Collections.addAll(this.literalTags, aliases); + } if (tagName == null) { this.portalsCore.getInfoLogger().logWarning("A tag cannot be null."); @@ -99,7 +101,7 @@ public boolean registerTag(Tag tag) { public List getTags() { - // Make a copy of the list to prevent issues with modification + // TODO Make a copy of the list to prevent issues with modification return this.tags; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/DestiTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/DestiTag.java index 82cabead..2c516b67 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/DestiTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/DestiTag.java @@ -53,8 +53,8 @@ public List autoComplete(String argData) { List autoCompletes = new ArrayList<>(); // Get all and filter by the argData - autoCompletes.add("AIR"); - autoCompletes.add("WATER"); + autoCompletes.add("somedesti"); + autoCompletes.add("hereigo"); return autoCompletes; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java new file mode 100644 index 00000000..706caeeb --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java @@ -0,0 +1,54 @@ +package com.sekwah.advancedportals.core.tags.activation; + +import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; +import com.sekwah.advancedportals.core.registry.TagTarget; +import com.sekwah.advancedportals.core.util.Lang; +import com.sekwah.advancedportals.core.warphandler.ActivationData; +import com.sekwah.advancedportals.core.warphandler.Tag; + +import java.util.List; + +public class NameTag implements Tag.Activation, Tag.AutoComplete { + + private final TagType[] tagTypes = new TagType[]{ TagType.PORTAL }; + + @Override + public TagType[] getTagTypes() { + return tagTypes; + } + + @Override + public String getName() { + return "name"; + } + + @Override + public String[] getAliases() { + return null; + } + + @Override + public String description() { + return Lang.translate("tag.name.description"); + } + + @Override + public boolean preActivated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) { + return false; + } + + @Override + public void postActivated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) { + + } + + @Override + public boolean activated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) { + return false; + } + + @Override + public List autoComplete(String argData) { + return null; + } +} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/warphandler/Tag.java b/core/src/main/java/com/sekwah/advancedportals/core/warphandler/Tag.java index bada7c22..6ccdd9af 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/warphandler/Tag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/warphandler/Tag.java @@ -3,6 +3,7 @@ import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.registry.TagTarget; +import javax.annotation.Nullable; import java.util.List; /** @@ -32,6 +33,7 @@ enum TagType { String getName(); + @Nullable String[] getAliases(); String description(); @@ -44,6 +46,7 @@ interface AutoComplete extends Tag { * @param argData * @return */ + @Nullable List autoComplete(String argData); }