From 98b3e8c3d3f563e64849ed186c2c74fb8b74346b Mon Sep 17 00:00:00 2001 From: Sekwah Date: Mon, 15 Jan 2024 01:20:12 +0000 Subject: [PATCH] feat: add the ability to split tags based on a splitstring --- .../common/CreateTaggedSubCommand.java | 29 ++++++++++++- .../core/portal/AdvancedPortal.java | 8 +++- .../core/tags/activation/DestiTag.java | 7 ++++ .../core/tags/activation/TriggerBlockTag.java | 42 +++++++++++-------- .../advancedportals/core/warphandler/Tag.java | 16 +++++-- .../portal/UpdatePortalSubCommand.java | 10 +++-- 6 files changed, 86 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/common/CreateTaggedSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/common/CreateTaggedSubCommand.java index 705826d..7586a51 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/common/CreateTaggedSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/common/CreateTaggedSubCommand.java @@ -11,6 +11,8 @@ import com.sekwah.advancedportals.core.warphandler.Tag; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; public abstract class CreateTaggedSubCommand implements SubCommand { @@ -49,8 +51,33 @@ public abstract class CreateTaggedSubCommand implements SubCommand { } if(tag instanceof Tag.AutoComplete autoComplete && startsWith) { - var tagSuggestions = autoComplete.autoComplete(split.length == 2 ? split[1] : ""); + var argData = split.length == 2 ? split[1] : ""; + var tagSuggestions = autoComplete.autoComplete(argData); + if(tagSuggestions != null) { + if(tag instanceof Tag.SplitTag splitTag) { + var multiTagSplit = splitTag.splitString(); + System.out.printf("Splitting with %s%n", multiTagSplit); + boolean endsWithSplit = argData.endsWith(multiTagSplit); + String[] items = argData.split(multiTagSplit); + System.out.printf("Splitting with %s%n", Arrays.toString(items)); + Set existingItems = Arrays.stream(items, 0, endsWithSplit ? items.length : items.length - 1) + .map(String::trim) + .collect(Collectors.toSet()); + + String partialInput = endsWithSplit ? "" : items[items.length - 1].trim(); + String baseString = endsWithSplit ? argData : argData.substring(0, argData.lastIndexOf(",") + 1); + + tagSuggestions = tagSuggestions.stream() + // Remove already listed items + .filter(s -> !existingItems.contains(s)) + // Remove items that don't match the currently typed input + .filter(s -> s.startsWith(partialInput)) + // Remap so the auto completes actually show + .map(s -> baseString + s) + .toList(); + } + // Loop over suggestions and add split[0] + ":" to the start for (String tagSuggestion : tagSuggestions) { suggestions.add(split[0] + ":" + tagSuggestion); diff --git a/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java b/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java index db6c56e..a3f30ca 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java @@ -104,13 +104,17 @@ public class AdvancedPortal implements TagTarget { for(DataTag portalTag : portalTags) { Tag.Activation activationHandler = tagRegistry.getActivationHandler(portalTag.NAME); if(activationHandler != null) { - activationHandler.preActivated(this, player, data, this.getArgValues(portalTag.NAME)); + if(!activationHandler.preActivated(this, player, data, this.getArgValues(portalTag.NAME))) { + return false; + } } } for(DataTag portalTag : portalTags) { Tag.Activation activationHandler = tagRegistry.getActivationHandler(portalTag.NAME); if(activationHandler != null) { - activationHandler.activated(this, player, data, this.getArgValues(portalTag.NAME)); + if(!activationHandler.activated(this, player, data, this.getArgValues(portalTag.NAME))) { + return false; + } } } for(DataTag portalTag : portalTags) { 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 1807524..4293b35 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 @@ -2,6 +2,7 @@ package com.sekwah.advancedportals.core.tags.activation; import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; +import com.sekwah.advancedportals.core.destination.Destination; import com.sekwah.advancedportals.core.registry.TagTarget; import com.sekwah.advancedportals.core.services.DestinationServices; import com.sekwah.advancedportals.core.util.Lang; @@ -39,6 +40,12 @@ public class DestiTag implements Tag.Activation, Tag.AutoComplete { @Override public boolean preActivated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) { + // Check that the destination exists. + for (String destiName : destinationServices.getDestinationNames()) { + if (destiName.equalsIgnoreCase(argData[0])) { + return true; + } + } return false; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/TriggerBlockTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/TriggerBlockTag.java index 4a29fba..ec4305e 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/TriggerBlockTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/TriggerBlockTag.java @@ -9,12 +9,13 @@ import com.sekwah.advancedportals.core.util.Lang; import com.sekwah.advancedportals.core.warphandler.ActivationData; import com.sekwah.advancedportals.core.warphandler.Tag; +import javax.annotation.Nullable; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; -public class TriggerBlockTag implements Tag.AutoComplete { +public class TriggerBlockTag implements Tag.AutoComplete, Tag.SplitTag { @Inject private ServerContainer serverContainer; @@ -45,22 +46,29 @@ public class TriggerBlockTag implements Tag.AutoComplete { @Override public List autoComplete(String argData) { - boolean endsWithComma = argData.endsWith(","); - String[] items = argData.split(","); - Set existingItems = Arrays.stream(items, 0, endsWithComma ? items.length : items.length - 1) - .map(String::trim) - .collect(Collectors.toSet()); + return serverContainer.getTriggerBlocks(); +// boolean endsWithComma = argData.endsWith(","); +// String[] items = argData.split(","); +// Set existingItems = Arrays.stream(items, 0, endsWithComma ? items.length : items.length - 1) +// .map(String::trim) +// .collect(Collectors.toSet()); +// +// String partialInput = endsWithComma ? "" : items[items.length - 1].trim(); +// String baseString = endsWithComma ? argData : argData.substring(0, argData.lastIndexOf(",") + 1); +// +// return serverContainer.getTriggerBlocks().stream() +// // Remove already listed items +// .filter(s -> !existingItems.contains(s)) +// // Remove items that don't match the currently typed input +// .filter(s -> s.startsWith(partialInput)) +// // Remap so the auto completes actually show +// .map(s -> baseString + s) +// .collect(Collectors.toList()); + } - String partialInput = endsWithComma ? "" : items[items.length - 1].trim(); - String baseString = endsWithComma ? argData : argData.substring(0, argData.lastIndexOf(",") + 1); - - return serverContainer.getTriggerBlocks().stream() - // Remove already listed items - .filter(s -> !existingItems.contains(s)) - // Remove items that don't match the currently typed input - .filter(s -> s.startsWith(partialInput)) - // Remap so the auto completes actually show - .map(s -> baseString + s) - .collect(Collectors.toList()); + @Nullable + @Override + public String splitString() { + return ","; } } 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 812bace..2be4303 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 @@ -57,6 +57,18 @@ public interface Tag { } + interface SplitTag extends Tag { + + /** + * This is used to split the tag into the arguments if multiple are supported + * + * @return null if the tag does not support splitting + */ + @Nullable + String splitString(); + + } + /** * The events for portal creation and destroying */ @@ -127,13 +139,11 @@ public interface Tag { * You should do some second checks if it can be dependent on the preActivate, the destination tags will also be * triggered here if a desti is listed. * - * (You can still cancel here but it is advised to check properly in preActive) - * * @param player * @param activeData * @param argData * - * @return If the tag has allowed the warp + * @return Action performed (only return false if the tag failed to do anything) */ boolean activated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData); diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/UpdatePortalSubCommand.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/UpdatePortalSubCommand.java index b79419d..dd5d56e 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/UpdatePortalSubCommand.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/UpdatePortalSubCommand.java @@ -61,10 +61,14 @@ public class UpdatePortalSubCommand implements SubCommand { config.getInt(portalName + ".pos2.Z")); List args = new ArrayList<>(); args.add(new DataTag("name", portalName)); - args.add(new DataTag("triggerblock", config.getString(portalName + ".triggerblock"))); + var triggerblock = config.getString(portalName + ".triggerblock"); + if(triggerblock != null) args.add(new DataTag("triggerblock", triggerblock.split(","))); // It's called bungee as that's the implementation behind it - args.add(new DataTag("bungee", config.getString(portalName + ".bungee"))); - args.add(new DataTag("destination", config.getString(portalName + ".destination"))); + var bungee = config.getString(portalName + ".bungee"); + if(bungee != null) args.add(new DataTag("bungee", bungee.split(","))); + + var destination = config.getString(portalName + ".destination"); + if(destination != null) args.add(new DataTag("destination", destination.split(","))); ConfigurationSection portalConfigSection = config.getConfigurationSection(portalName); ConfigurationSection portalArgsConf = portalConfigSection.getConfigurationSection("portalArgs");