feat: make portal and desti implement the same tab auto complete

This commit is contained in:
sekwah 2023-12-03 14:50:44 +00:00 committed by Sekwah
parent 00ab7e4fec
commit 6921749128
8 changed files with 151 additions and 95 deletions

View File

@ -1,20 +1,26 @@
package com.sekwah.advancedportals.core.commands.subcommands.desti;
import com.google.inject.Inject;
import com.sekwah.advancedportals.core.commands.SubCommand;
import com.sekwah.advancedportals.core.commands.subcommands.reusable.CreateTaggedSubCommand;
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.destination.Destination;
import com.sekwah.advancedportals.core.registry.TagRegistry;
import com.sekwah.advancedportals.core.serializeddata.DataTag;
import com.sekwah.advancedportals.core.permissions.PortalPermissions;
import com.sekwah.advancedportals.core.services.DestinationServices;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.util.TagReader;
import com.sekwah.advancedportals.core.warphandler.Tag;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CreateDestiSubCommand implements SubCommand {
public class CreateDestiSubCommand extends CreateTaggedSubCommand {
@Inject
TagRegistry tagRegistry;
@Inject
DestinationServices destinationServices;
@ -29,7 +35,16 @@ public class CreateDestiSubCommand implements SubCommand {
}
ArrayList<DataTag> destinationTags = TagReader.getTagsFromArgs(args);
Destination destination = destinationServices.createDesti(args[1], player, player.getLoc(), destinationTags);
// Find the tag with the "name" NAME
DataTag nameTag = destinationTags.stream().findFirst().filter(tag -> tag.NAME.equals("name")).orElse(null);
// If the tag is null, check if arg[1] has a : to check it's not a tag.
if(nameTag == null && !args[1].contains(":")) {
nameTag = new DataTag("name", args[1]);
destinationTags.add(nameTag);
}
Destination destination = destinationServices.createDesti(player, player.getLoc(), destinationTags);
if(destination != null) {
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.createdesti.complete"));
sender.sendMessage(Lang.translate("command.create.tags"));
@ -64,8 +79,10 @@ public class CreateDestiSubCommand implements SubCommand {
}
@Override
public List<String> onTabComplete(CommandSenderContainer sender, String[] args) {
return null;
protected List<Tag> getRelatedTags() {
var tags = tagRegistry.getTags();
// Filter tags that support Destination
return tags.stream().filter(tag -> Arrays.asList(tag.getTagTypes()).contains(Tag.TagType.DESTINATION)).toList();
}
@Override

View File

@ -2,6 +2,7 @@ package com.sekwah.advancedportals.core.commands.subcommands.portal;
import com.google.inject.Inject;
import com.sekwah.advancedportals.core.commands.SubCommand;
import com.sekwah.advancedportals.core.commands.subcommands.reusable.CreateTaggedSubCommand;
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.registry.TagRegistry;
@ -18,14 +19,11 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CreatePortalSubCommand implements SubCommand {
public class CreatePortalSubCommand extends CreateTaggedSubCommand {
@Inject
PortalServices portalServices;
@Inject
InfoLogger infoLogger;
@Inject
TagRegistry tagRegistry;
@ -71,83 +69,10 @@ public class CreatePortalSubCommand implements SubCommand {
}
@Override
public List<String> onTabComplete(CommandSenderContainer sender, String[] args) {
if(TagReader.isClosedString(args)) {
return List.of();
}
List<Tag> allTags = tagRegistry.getTags();
List<String> suggestions = new ArrayList<>();
if(args.length > 0) {
var lastArg = args[args.length - 1];
// Check if the split results in exactly 2 or if its 1 and ends with :
var split = lastArg.split(":");
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) {
// 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
for (String tagSuggestion : tagSuggestions) {
suggestions.add(split[0] + ":" + tagSuggestion);
}
}
}
}
return suggestions;
}
}
ArrayList<DataTag> portalTags = TagReader.getTagsFromArgs(args);
allTags.stream().filter(tag -> {
for (DataTag portalTag : portalTags) {
if(portalTag.NAME.equals(tag.getName())) {
return false;
}
// check the tag aliases
for (String alias : tag.getAliases()) {
if(portalTag.NAME.equals(alias)) {
return false;
}
}
}
return true;
}).forEach(tag -> {
suggestions.add(tag.getName());
var aliases = tag.getAliases();
if(aliases != null) {
suggestions.addAll(Arrays.stream(aliases).toList());
}
});
// Loop over all suggestions and add : to the end
for (int i = 0; i < suggestions.size(); i++) {
suggestions.set(i, suggestions.get(i) + ":");
}
return suggestions;
protected List<Tag> getRelatedTags() {
var tags = tagRegistry.getTags();
// Filter tags that support Destination
return tags.stream().filter(tag -> Arrays.asList(tag.getTagTypes()).contains(Tag.TagType.PORTAL)).toList();
}
@Override

View File

@ -0,0 +1,102 @@
package com.sekwah.advancedportals.core.commands.subcommands.reusable;
import com.google.inject.Inject;
import com.sekwah.advancedportals.core.commands.SubCommand;
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.permissions.PortalPermissions;
import com.sekwah.advancedportals.core.portal.AdvancedPortal;
import com.sekwah.advancedportals.core.registry.TagRegistry;
import com.sekwah.advancedportals.core.serializeddata.DataTag;
import com.sekwah.advancedportals.core.services.PortalServices;
import com.sekwah.advancedportals.core.util.InfoLogger;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.util.TagReader;
import com.sekwah.advancedportals.core.warphandler.Tag;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class CreateTaggedSubCommand implements SubCommand {
protected abstract List<Tag> getRelatedTags();
@Override
public List<String> onTabComplete(CommandSenderContainer sender, String[] args) {
if(TagReader.isClosedString(args)) {
return List.of();
}
List<Tag> allTags = this.getRelatedTags();
List<String> suggestions = new ArrayList<>();
if(args.length > 0) {
var lastArg = args[args.length - 1];
// Check if the split results in exactly 2 or if its 1 and ends with :
var split = lastArg.split(":");
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) {
// 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
for (String tagSuggestion : tagSuggestions) {
suggestions.add(split[0] + ":" + tagSuggestion);
}
}
}
}
return suggestions;
}
}
ArrayList<DataTag> portalTags = TagReader.getTagsFromArgs(args);
allTags.stream().filter(tag -> {
for (DataTag portalTag : portalTags) {
if(portalTag.NAME.equals(tag.getName())) {
return false;
}
// check the tag aliases
for (String alias : tag.getAliases()) {
if(portalTag.NAME.equals(alias)) {
return false;
}
}
}
return true;
}).forEach(tag -> {
suggestions.add(tag.getName());
var aliases = tag.getAliases();
if(aliases != null) {
suggestions.addAll(Arrays.stream(aliases).toList());
}
});
// Loop over all suggestions and add : to the end
for (int i = 0; i < suggestions.size(); i++) {
suggestions.set(i, suggestions.get(i) + ":");
}
return suggestions;
}
}

View File

@ -27,7 +27,7 @@ import java.util.Set;
public class Destination implements TagTarget {
@Inject
TagRegistry tagRegistry;
transient TagRegistry tagRegistry;
@SerializedName("l")
private PlayerLocation loc;

View File

@ -5,12 +5,7 @@ public class DataTag {
public final String NAME;
public final String[] VALUES;
public DataTag(String argName, String values) {
this.NAME = argName;
this.VALUES = new String[]{values};
}
public DataTag(String argName, String[] values) {
public DataTag(String argName, String... values) {
this.NAME = argName;
this.VALUES = values;
}

View File

@ -58,8 +58,18 @@ public class DestinationServices {
public Destination createDesti(String name, PlayerContainer player, PlayerLocation playerLocation, ArrayList<DataTag> tags) {
// TODO change to write messages
public Destination createDesti(PlayerContainer player, PlayerLocation playerLocation, ArrayList<DataTag> tags) {
// Find the tag with the "name" NAME
DataTag nameTag = tags.stream().findFirst().filter(tag -> tag.NAME.equals("name")).orElse(null);
String name = nameTag == null ? null : nameTag.VALUES[0];
// If the name is null, send an error saying that the name is required.
if(nameTag == null) {
player.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("desti.error.noname"));
return null;
}
if(name == null || name.equals("")) {
player.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("desti.error.noname"));
return null;

View File

@ -20,6 +20,12 @@ import java.util.List;
*/
public interface Tag {
/**
* By default, all tags should be able to use either.
*
* Though you may have use cases where you want to limit it to one or the other.
*
*/
enum TagType {
PORTAL,
DESTINATION

View File

@ -105,6 +105,7 @@ command.error.noname= No name has been given.
command.error.notags= No tags have been given. You need to include at least &ename:(name)&c.
desti.error.takenname=The name given for the portal is already taken.
desti.error.noname=You must specify a name for the destination. (name:destinationname)
error.notplayer=Only players can do that.