fix: allow null values for auto complete suggestions

This commit is contained in:
Sekwah 2023-12-02 20:22:59 +00:00
parent b68153d744
commit 93ceb0b422
6 changed files with 95 additions and 15 deletions

View File

@ -11,6 +11,7 @@ import com.sekwah.advancedportals.core.serializeddata.DataStorage;
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 class AdvancedPortalsCore {
}
private void registerTags() {
this.tagRegistry.registerTag(new NameTag());
this.tagRegistry.registerTag(new DestiTag());
}

View File

@ -88,7 +88,23 @@ public class CreatePortalSubCommand implements SubCommand {
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 class CreatePortalSubCommand implements SubCommand {
}
}
}
// This is returning right but something is going wrong with "desti:A" whenever anything is typed after :
return suggestions;
}
}
@ -120,7 +136,10 @@ public class CreatePortalSubCommand implements SubCommand {
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

View File

@ -69,16 +69,18 @@ public class TagRegistry {
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;
}
}
// Add name and aliases to literalTags to check for clashes
var aliases = tag.getAliases();
this.literalTags.add(tagName);
Collections.addAll(this.literalTags, tag.getAliases());
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;
}
}
Collections.addAll(this.literalTags, aliases);
}
if (tagName == null) {
this.portalsCore.getInfoLogger().logWarning("A tag cannot be null.");
@ -99,7 +101,7 @@ public class TagRegistry {
public List<Tag> 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;
}

View File

@ -53,8 +53,8 @@ public class DestiTag implements Tag.Activation, Tag.AutoComplete {
List<String> autoCompletes = new ArrayList<>();
// Get all and filter by the argData
autoCompletes.add("AIR");
autoCompletes.add("WATER");
autoCompletes.add("somedesti");
autoCompletes.add("hereigo");
return autoCompletes;
}

View File

@ -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<String> autoComplete(String argData) {
return null;
}
}

View File

@ -3,6 +3,7 @@ package com.sekwah.advancedportals.core.warphandler;
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 @@ public interface Tag {
String getName();
@Nullable
String[] getAliases();
String description();
@ -44,6 +46,7 @@ public interface Tag {
* @param argData
* @return
*/
@Nullable
List<String> autoComplete(String argData);
}