From ad3f117f049322224620781c5d9dbff0d2a8e9a0 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Mon, 15 Jan 2024 01:20:12 +0000 Subject: [PATCH] fix: portal activation issue --- .../core/AdvancedPortalsCore.java | 7 +++++ .../common/CreateTaggedSubCommand.java | 29 ++++++++++++------- .../desti/CreateDestiSubCommand.java | 1 + .../portal/CreatePortalSubCommand.java | 1 + .../connector/containers/EntityContainer.java | 1 + .../impl/PlayerDataRepositoryImpl.java | 3 -- .../repository/impl/PortalRepositoryImpl.java | 8 ++--- .../core/services/PortalServices.java | 8 ++--- .../core/tags/activation/DestiTag.java | 10 ++++++- .../core/tags/activation/TriggerBlockTag.java | 26 +---------------- .../advancedportals/core/util/TagReader.java | 2 ++ .../advancedportals/core/warphandler/Tag.java | 2 +- .../container/SpigotEntityContainer.java | 5 ++++ 13 files changed, 54 insertions(+), 49 deletions(-) diff --git a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java index fd480146..f940c3cb 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -43,6 +43,8 @@ public class AdvancedPortalsCore { private final ServerContainer serverContainer; + private static AdvancedPortalsCore instance; + @Inject private CommandRegister commandRegister; @@ -68,6 +70,7 @@ public class AdvancedPortalsCore { private GameScheduler gameScheduler; public AdvancedPortalsCore(String mcVersion, File dataStorageLoc, InfoLogger infoLogger, ServerContainer serverContainer) { + instance = this; this.serverContainer = serverContainer; this.dataStorage = new DataStorage(dataStorageLoc); this.infoLogger = infoLogger; @@ -202,6 +205,10 @@ public class AdvancedPortalsCore { return mcVersion; } + public static AdvancedPortalsCore getInstance() { + return instance; + } + public ServerContainer getServerContainer() { return serverContainer; } 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 7586a514..b286125f 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 @@ -1,10 +1,8 @@ package com.sekwah.advancedportals.core.commands.subcommands.common; -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.serializeddata.DataTag; -import com.sekwah.advancedportals.core.util.InfoLogger; import com.sekwah.advancedportals.core.util.TagReader; import com.sekwah.advancedportals.core.warphandler.Tag; @@ -55,18 +53,16 @@ public abstract class CreateTaggedSubCommand implements SubCommand { var tagSuggestions = autoComplete.autoComplete(argData); if(tagSuggestions != null) { - if(tag instanceof Tag.SplitTag splitTag) { + if(tag instanceof Tag.Split 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); + String baseString = endsWithSplit ? argData : argData.substring(0, argData.lastIndexOf(multiTagSplit) + 1); tagSuggestions = tagSuggestions.stream() // Remove already listed items @@ -90,17 +86,17 @@ public abstract class CreateTaggedSubCommand implements SubCommand { } } - ArrayList portalTags = TagReader.getTagsFromArgs(args); + ArrayList tagsFromArgs = TagReader.getTagsFromArgs(args); allTags.stream().filter(tag -> { - for (DataTag portalTag : portalTags) { - if(portalTag.NAME.equals(tag.getName())) { + for (DataTag argTag : tagsFromArgs) { + if(argTag.NAME.equals(tag.getName())) { return false; } var aliases = tag.getAliases(); if(aliases != null) { for (String alias : aliases) { - if(portalTag.NAME.equals(alias)) { + if(argTag.NAME.equals(alias)) { return false; } } @@ -127,13 +123,24 @@ public abstract class CreateTaggedSubCommand implements SubCommand { List relatedTags = this.getRelatedTags(); List processedTags = new ArrayList<>(); - for (DataTag dataTag : dataTags) { + for (var dataTag : dataTags) { for (Tag tag : relatedTags) { + if(tag instanceof Tag.Split splitTag) { + var splitString = splitTag.splitString(); + if(splitString != null) { + List newValues = new ArrayList<>(); + for(String split : dataTag.VALUES) { + newValues.addAll(Arrays.stream(split.split(splitString)).map(String::trim).toList()); + } + dataTag = new DataTag(dataTag.NAME, newValues.toArray(new String[0])); + } + } if (dataTag.NAME.equals(tag.getName())) { // DataTag name matches the tag's main name, add as is processedTags.add(dataTag); break; } else if (tag.getAliases() != null && Arrays.asList(tag.getAliases()).contains(dataTag.NAME)) { + // There is currently an edge case where if someone uses multiple aliases or names for tags at the same time, it'll only use the last one applied to the mapping. // DataTag name matches an alias, create a new DataTag with the main name processedTags.add(new DataTag(tag.getName(), dataTag.VALUES)); break; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/CreateDestiSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/CreateDestiSubCommand.java index 6e3aecf4..62e28dd9 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/CreateDestiSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/CreateDestiSubCommand.java @@ -33,6 +33,7 @@ public class CreateDestiSubCommand extends CreateTaggedSubCommand { sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.create.destination.console")); return; } + ArrayList destinationTags = TagReader.getTagsFromArgs(args); // Find the tag with the "name" NAME diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java index 060e77e2..bef35c69 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java @@ -43,6 +43,7 @@ public class CreatePortalSubCommand extends CreateTaggedSubCommand { sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.create.console")); return; } + ArrayList portalTags = TagReader.getTagsFromArgs(args); // Find the tag with the "name" NAME diff --git a/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/EntityContainer.java b/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/EntityContainer.java index ddc106a2..657a0a8a 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/EntityContainer.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/EntityContainer.java @@ -15,4 +15,5 @@ public interface EntityContainer { WorldContainer getWorld(); + String getName(); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java index 96674af0..1e77bc49 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java @@ -17,9 +17,6 @@ public class PlayerDataRepositoryImpl implements IPlayerDataRepository { private final String fileLocation = "playerData/"; - @Inject - private ConfigRepository configRepository; - @Inject DataStorage dataStorage; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java index 8793d3a0..948f3e0a 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java @@ -1,12 +1,10 @@ package com.sekwah.advancedportals.core.repository.impl; -import com.google.common.collect.ImmutableMap; import com.google.inject.Inject; import com.google.inject.Singleton; -import com.sekwah.advancedportals.core.destination.Destination; +import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.portal.AdvancedPortal; import com.sekwah.advancedportals.core.serializeddata.DataStorage; -import com.sekwah.advancedportals.core.serializeddata.WorldLocation; import com.sekwah.advancedportals.core.repository.IPortalRepository; import com.sekwah.advancedportals.core.tags.activation.NameTag; @@ -44,7 +42,9 @@ public class PortalRepositoryImpl implements IPortalRepository { @Override public AdvancedPortal get(String name) { - return dataStorage.loadJson(AdvancedPortal.class, fileLocation + name + ".json"); + var portal = dataStorage.loadJson(AdvancedPortal.class, fileLocation + name + ".json"); + AdvancedPortalsCore.getInstance().getModule().getInjector().injectMembers(portal); + return portal; } @Override diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java b/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java index b9d7000a..066b91fd 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java @@ -86,10 +86,10 @@ public class PortalServices { var blockEntityTopMaterial = world.getBlock(blockEntityTopLoc); for (AdvancedPortal portal : portalCache.values()) { - if (portal.isLocationInPortal(toLoc) - || portal.isLocationInPortal(blockEntityTopLoc) - || portal.isTriggerBlock(blockMaterial) - || portal.isTriggerBlock(blockEntityTopMaterial)) { + if ((portal.isLocationInPortal(toLoc) + && portal.isTriggerBlock(blockMaterial)) + || (portal.isLocationInPortal(blockEntityTopLoc) + && portal.isTriggerBlock(blockEntityTopMaterial))) { portal.activate(player); } } 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 4293b355..a3eb4d07 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 @@ -8,9 +8,11 @@ import com.sekwah.advancedportals.core.services.DestinationServices; 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.List; -public class DestiTag implements Tag.Activation, Tag.AutoComplete { +public class DestiTag implements Tag.Activation, Tag.AutoComplete, Tag.Split { public static String TAG_NAME = "destination"; @Inject @@ -63,4 +65,10 @@ public class DestiTag implements Tag.Activation, Tag.AutoComplete { public List autoComplete(String argData) { return destinationServices.getDestinationNames(); } + + @Nullable + @Override + public String splitString() { + return ","; + } } 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 ec4305e7..3cb3dc6d 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 @@ -1,21 +1,14 @@ 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.connector.containers.ServerContainer; -import com.sekwah.advancedportals.core.registry.TagTarget; -import com.sekwah.advancedportals.core.services.DestinationServices; 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, Tag.SplitTag { +public class TriggerBlockTag implements Tag.AutoComplete, Tag.Split { @Inject private ServerContainer serverContainer; @@ -47,23 +40,6 @@ public class TriggerBlockTag implements Tag.AutoComplete, Tag.SplitTag { @Override public List autoComplete(String argData) { 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()); } @Nullable diff --git a/core/src/main/java/com/sekwah/advancedportals/core/util/TagReader.java b/core/src/main/java/com/sekwah/advancedportals/core/util/TagReader.java index f80075ea..42bf6051 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/util/TagReader.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/util/TagReader.java @@ -1,9 +1,11 @@ package com.sekwah.advancedportals.core.util; import com.sekwah.advancedportals.core.serializeddata.DataTag; +import com.sekwah.advancedportals.core.warphandler.Tag; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; public class TagReader { 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 2be4303d..a0003a73 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,7 +57,7 @@ public interface Tag { } - interface SplitTag extends Tag { + interface Split extends Tag { /** * This is used to split the tag into the arguments if multiple are supported diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotEntityContainer.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotEntityContainer.java index 191af645..24e66941 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotEntityContainer.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotEntityContainer.java @@ -58,4 +58,9 @@ public class SpigotEntityContainer implements EntityContainer { public WorldContainer getWorld() { return new SpigotWorldContainer(this.entity.getWorld()); } + + @Override + public String getName() { + return this.entity.getName(); + } }