mirror of
https://github.com/sekwah41/Advanced-Portals.git
synced 2024-11-22 02:25:49 +01:00
feat: add the ability to split tags based on a splitstring
This commit is contained in:
parent
805e8dc3d0
commit
116c45f5a5
@ -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<String> 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);
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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<String> autoComplete(String argData) {
|
||||
boolean endsWithComma = argData.endsWith(",");
|
||||
String[] items = argData.split(",");
|
||||
Set<String> 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<String> 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 ",";
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -61,10 +61,14 @@ public class UpdatePortalSubCommand implements SubCommand {
|
||||
config.getInt(portalName + ".pos2.Z"));
|
||||
List<DataTag> 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");
|
||||
|
Loading…
Reference in New Issue
Block a user