From 3558629f8e6e5ea2fc2426de7a6dc5386c54e3f3 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Sun, 17 Dec 2023 06:19:43 +0000 Subject: [PATCH] feat: add the ability for tags to reject values --- .../core/AdvancedPortalsCore.java | 1 - .../desti/ReloadDestiSubCommand.java | 48 ------------------- .../portal/ShowPortalSubCommand.java | 2 +- .../{ErrorCode.java => CommandErrorCode.java} | 5 +- .../core/registry/CommandException.java | 6 +-- .../core/registry/TagRegistry.java | 3 +- .../impl/DestinationRepositoryImpl.java | 6 +++ .../core/services/Creation.java | 7 +++ .../core/services/DestinationServices.java | 31 +++++------- .../core/services/PortalServices.java | 4 ++ .../core/tags/activation/DestiTag.java | 3 +- .../core/tags/activation/NameTag.java | 23 ++++++++- .../advancedportals/core/util/Response.java | 13 ----- .../advancedportals/core/warphandler/Tag.java | 3 +- lang/src/main/resources/lang/en_GB.lang | 1 + 15 files changed, 61 insertions(+), 95 deletions(-) delete mode 100644 core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/ReloadDestiSubCommand.java rename core/src/main/java/com/sekwah/advancedportals/core/registry/{ErrorCode.java => CommandErrorCode.java} (62%) create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/services/Creation.java delete mode 100644 core/src/main/java/com/sekwah/advancedportals/core/util/Response.java 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 e1e52be0..8e52dd03 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -138,7 +138,6 @@ private void registerDestinationCommand(CommandRegister commandRegister) { this.destiCommand.registerSubCommand("remove", new RemoveDestiSubCommand()); this.destiCommand.registerSubCommand("list", new ListDestiSubCommand()); this.destiCommand.registerSubCommand("show", new ShowDestiSubCommand()); - this.destiCommand.registerSubCommand("reload", new ReloadDestiSubCommand()); commandRegister.registerCommand("destination", this.destiCommand); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/ReloadDestiSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/ReloadDestiSubCommand.java deleted file mode 100644 index 3f936207..00000000 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/ReloadDestiSubCommand.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.sekwah.advancedportals.core.commands.subcommands.desti; - -import com.google.inject.Inject; -import com.sekwah.advancedportals.core.AdvancedPortalsCore; -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.connector.containers.ServerContainer; -import com.sekwah.advancedportals.core.services.DestinationServices; -import com.sekwah.advancedportals.core.services.PortalTempDataServices; -import com.sekwah.advancedportals.core.util.GameScheduler; -import com.sekwah.advancedportals.core.util.Lang; - -import java.util.List; - -/** - * This will be different from the old show command and I believe it is 1.16+ till the latest version as of writing this. - */ -public class ReloadDestiSubCommand implements SubCommand { - - @Inject - DestinationServices destinationServices; - - @Override - public void onCommand(CommandSenderContainer sender, String[] args) { - sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.destination.reload")); - } - - @Override - public boolean hasPermission(CommandSenderContainer sender) { - return true; - } - - @Override - public List onTabComplete(CommandSenderContainer sender, String[] args) { - return null; - } - - @Override - public String getBasicHelpText() { - return Lang.translate("command.destination.reload.help"); - } - - @Override - public String getDetailedHelpText() { - return Lang.translate("command.destination.reload.detailedhelp"); - } -} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java index ba5d1921..6d0fe5ee 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java @@ -74,7 +74,7 @@ public void registered() { return; } - + } /*sender.sendMessage("Debug"); if(sender.getPlayerContainer() != null) { diff --git a/core/src/main/java/com/sekwah/advancedportals/core/registry/ErrorCode.java b/core/src/main/java/com/sekwah/advancedportals/core/registry/CommandErrorCode.java similarity index 62% rename from core/src/main/java/com/sekwah/advancedportals/core/registry/ErrorCode.java rename to core/src/main/java/com/sekwah/advancedportals/core/registry/CommandErrorCode.java index 30821278..7bdb4b85 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/registry/ErrorCode.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/registry/CommandErrorCode.java @@ -1,12 +1,11 @@ package com.sekwah.advancedportals.core.registry; -public enum ErrorCode { +public enum CommandErrorCode { INSUFFICIENT_ARGUMENTS(""), NO_PERMISSION(""); - ; - ErrorCode(String message) { + CommandErrorCode(String message) { } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/registry/CommandException.java b/core/src/main/java/com/sekwah/advancedportals/core/registry/CommandException.java index 69895d46..19ac776c 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/registry/CommandException.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/registry/CommandException.java @@ -1,15 +1,15 @@ package com.sekwah.advancedportals.core.registry; public class CommandException { - private ErrorCode errorCode; + private CommandErrorCode errorCode; private String message; - public CommandException(ErrorCode errorCode, String message) { + public CommandException(CommandErrorCode errorCode, String message) { this.errorCode = errorCode; this.message = message; } - public ErrorCode getErrorCode() { + public CommandErrorCode getErrorCode() { return errorCode; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java b/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java index a3907138..5f87ed73 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java @@ -1,7 +1,6 @@ package com.sekwah.advancedportals.core.registry; import com.google.inject.Inject; -import com.google.inject.Singleton; import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.warphandler.Tag; @@ -53,7 +52,7 @@ public Tag.Creation getCreationHandler(String arg) { * @param arg * @return */ - public Tag.TagStatus getTagStatusHandler(String arg) { + public Tag.TagStatus getStatusHandler(String arg) { return this.statusTags.get(arg); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java index 3dee4f00..e2cd2f9c 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java @@ -5,6 +5,7 @@ import com.sekwah.advancedportals.core.destination.Destination; import com.sekwah.advancedportals.core.repository.IDestinationRepository; import com.sekwah.advancedportals.core.serializeddata.DataStorage; +import com.sekwah.advancedportals.core.tags.activation.NameTag; import com.sekwah.advancedportals.core.util.InfoLogger; import javax.inject.Singleton; @@ -58,6 +59,11 @@ public List getAll() { List allFiles = dataStorage.listAllFiles(fileLocation, false); for (String fileName : allFiles) { Destination destination = dataStorage.loadJson(Destination.class, fileLocation + fileName); + // Forces the name tag to be up-to-date on load + String[] name = destination.getArgValues(NameTag.TAG_NAME); + if(name != null && name.length > 0) { + destination.setArgValues(NameTag.TAG_NAME, new String[]{fileName.replace(".json", "")}); + } destinations.add(destination); } return destinations; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/Creation.java b/core/src/main/java/com/sekwah/advancedportals/core/services/Creation.java new file mode 100644 index 00000000..754c0041 --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/Creation.java @@ -0,0 +1,7 @@ +package com.sekwah.advancedportals.core.services; + +public enum Creation { + SUCCESS, + NAME_IN_USE, + TAG_REJECTED +} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java b/core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java index 4d0a96c0..f6e25aea 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java @@ -3,12 +3,13 @@ import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; +import com.sekwah.advancedportals.core.registry.TagRegistry; import com.sekwah.advancedportals.core.serializeddata.DataTag; import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; import com.sekwah.advancedportals.core.destination.Destination; import com.sekwah.advancedportals.core.repository.IDestinationRepository; import com.sekwah.advancedportals.core.util.Lang; -import com.sekwah.advancedportals.core.util.Response; +import com.sekwah.advancedportals.core.warphandler.Tag; import javax.inject.Singleton; import java.util.ArrayList; @@ -22,22 +23,11 @@ public class DestinationServices { @Inject private IDestinationRepository destinationRepository; - private final Map destinationCache = new HashMap<>(); - public Response.Creation create(String name, Destination destination) { - if (!destinationRepository.containsKey(name)) { - destinationRepository.save(name, destination); - return Response.Creation.SUCCESS; - } - return Response.Creation.NAME_IN_USE; - } + @Inject + TagRegistry tagRegistry; - public Boolean delete(String name) { - if (!destinationRepository.containsKey(name)) { - destinationRepository.delete(name); - } - return false; - } + private final Map destinationCache = new HashMap<>(); public List getDestinationNames() { return destinationRepository.getAllNames(); @@ -49,6 +39,7 @@ public List getDestinations() { public void loadDestinations() { List destinationNames = destinationRepository.getAllNames(); + destinationCache.clear(); for (String name : destinationNames) { Destination destination = destinationRepository.get(name); destinationCache.put(name, destination); @@ -80,17 +71,17 @@ public Destination createDesti(PlayerContainer player, PlayerLocation playerLoca desti.setArgValues(portalTag); } for (DataTag destiTag : tags) { - // TODO sort tag handle registry - /*TagHandler.Creation creation = AdvancedPortalsCore.getDestinationTagRegistry().getCreationHandler(destiTag.NAME); + Tag.Creation creation = tagRegistry.getCreationHandler(destiTag.NAME); if(creation != null) { - creation.created(desti, player, destiTag.VALUE); - }*/ + if(!creation.created(desti, player, destiTag.VALUES)) { + return null; + } + } } try { if(this.destinationRepository.save(name, desti)) { this.destinationCache.put(name, desti); } else { - player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("desti.error.save")); return null; } } catch (Exception e) { 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 4b8e3055..1b32519d 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 @@ -3,6 +3,7 @@ import com.google.common.collect.ImmutableList; 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.repository.IDestinationRepository; import com.sekwah.advancedportals.core.repository.IPortalRepository; import com.sekwah.advancedportals.core.serializeddata.DataTag; @@ -15,6 +16,7 @@ import javax.inject.Singleton; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.Map; @Singleton @@ -29,6 +31,8 @@ public class PortalServices { @Inject private PortalTempDataServices portalTempDataServices; + private final Map portalCache = new HashMap<>(); + public void loadPortals() { } 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 f5a40e2d..8596031c 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 @@ -11,6 +11,7 @@ public class DestiTag implements Tag.Activation, Tag.AutoComplete { + public static String TAG_NAME = "name"; @Inject DestinationServices destinationServices; @@ -23,7 +24,7 @@ public TagType[] getTagTypes() { @Override public String getName() { - return "destination"; + return TAG_NAME; } @Override diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java index 6bed6a64..e8a1a0aa 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java @@ -15,7 +15,9 @@ *

* Most tags shouldn't be like this unless they are to be paired with another tag. */ -public class NameTag implements Tag.AutoComplete { +public class NameTag implements Tag.AutoComplete, Tag.Creation { + + public static String TAG_NAME = "name"; private final TagType[] tagTypes = new TagType[]{ TagType.PORTAL, TagType.DESTINATION }; @@ -26,7 +28,7 @@ public TagType[] getTagTypes() { @Override public String getName() { - return "name"; + return TAG_NAME; } @Override @@ -43,4 +45,21 @@ public String description() { public List autoComplete(String argData) { return null; } + + @Override + public boolean created(TagTarget target, PlayerContainer player, String[] argData) { + if (argData.length > 0) { + String name = argData[0]; + if (name.contains(" ")) { + player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("tag.name.error.nospaces")); + return false; + } + } + return true; + } + + @Override + public void destroyed(TagTarget target, PlayerContainer player, String[] argData) { + + } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/util/Response.java b/core/src/main/java/com/sekwah/advancedportals/core/util/Response.java deleted file mode 100644 index 4997272a..00000000 --- a/core/src/main/java/com/sekwah/advancedportals/core/util/Response.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.sekwah.advancedportals.core.util; - -/** - * Need to make more of the plugin use this rather than just returning true or false. - */ -public class Response { - - public enum Creation { - SUCCESS, - NAME_IN_USE, - } - -} 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 026365af..812bace9 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 @@ -67,8 +67,9 @@ interface Creation extends Tag { * * @param player if null then created by the server or a plugin * @param argData + * @return If the tag is valid or allowed creation */ - void created(TagTarget target, PlayerContainer player, String[] argData); + boolean created(TagTarget target, PlayerContainer player, String[] argData); /** * Example if the player does not have access to remove the portal or destination. diff --git a/lang/src/main/resources/lang/en_GB.lang b/lang/src/main/resources/lang/en_GB.lang index ea1a202b..863b615e 100644 --- a/lang/src/main/resources/lang/en_GB.lang +++ b/lang/src/main/resources/lang/en_GB.lang @@ -145,3 +145,4 @@ items.interact.left=Left Click items.interact.right=Right Click tag.desti.description=Sets the destination of the portal +tag.name.error.nospaces= The name cannot contain spaces.