Skip to content

Commit

Permalink
fix: allow null values for auto complete suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Nov 18, 2024
1 parent c64c342 commit 412bc4f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -72,6 +73,7 @@ public void onEnable() {
}

private void registerTags() {
this.tagRegistry.registerTag(new NameTag());
this.tagRegistry.registerTag(new DestiTag());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,23 @@ public List<String> 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
Expand All @@ -98,7 +114,7 @@ public List<String> onTabComplete(CommandSenderContainer sender, String[] args)
}
}
}
// This is returning right but something is going wrong with "desti:A" whenever anything is typed after :

return suggestions;
}
}
Expand All @@ -120,7 +136,10 @@ public List<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -99,7 +101,7 @@ public boolean registerTag(Tag tag) {


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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public List<String> autoComplete(String argData) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -32,6 +33,7 @@ enum TagType {

String getName();

@Nullable
String[] getAliases();

String description();
Expand All @@ -44,6 +46,7 @@ interface AutoComplete extends Tag {
* @param argData
* @return
*/
@Nullable
List<String> autoComplete(String argData);

}
Expand Down

0 comments on commit 412bc4f

Please sign in to comment.