From 55de8093492ccdb47ea9da1358c7fd77dbdc0de9 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Fri, 7 Apr 2023 02:37:59 +0100 Subject: [PATCH] feat: add destination and portal services --- .../core/AdvancedPortalsCore.java | 4 +- .../core/commands/CommandWithSubCommands.java | 6 +- .../core/module/AdvancedPortalsModule.java | 2 +- .../repository/IDestinationRepository.java | 7 +- .../core/repository/IJsonRepository.java | 5 + .../{ => impl}/ConfigRepositoryImpl.java | 3 +- .../DestinationRepositoryImpl.java} | 18 +-- .../PortalRepositoryImpl.java} | 16 ++- .../core/services/DestinationServices.java | 107 ++++++++++++++++++ .../core/services/PortalServices.java | 52 +++++++++ .../core/services/Response.java | 10 ++ 11 files changed, 213 insertions(+), 17 deletions(-) rename core/src/main/java/com/sekwah/advancedportals/core/repository/{ => impl}/ConfigRepositoryImpl.java (90%) rename core/src/main/java/com/sekwah/advancedportals/core/repository/{DestinationRepository.java => impl/DestinationRepositoryImpl.java} (73%) rename core/src/main/java/com/sekwah/advancedportals/core/repository/{PortalRepository.java => impl/PortalRepositoryImpl.java} (58%) create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/services/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 7ba26c04..b5ca8b92 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -74,7 +74,7 @@ public class AdvancedPortalsCore { } private void registerPortalCommand(CommandRegister commandRegister) { - this.portalCommand = new CommandWithSubCommands(); + this.portalCommand = new CommandWithSubCommands(this); // TODO remove once annotations are done this.portalCommand.registerSubCommand("version", new VersionSubCommand()); @@ -91,7 +91,7 @@ public class AdvancedPortalsCore { } private void registerDestinationCommand(CommandRegister commandRegister) { - this.destiCommand = new CommandWithSubCommands(); + this.destiCommand = new CommandWithSubCommands(this); // TODO remove once annotations are done this.destiCommand.registerSubCommand("create", new CreateDestiSubCommand()); diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java index 4c6c1f40..35350f8f 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java @@ -1,5 +1,6 @@ package com.sekwah.advancedportals.core.commands; +import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer; import com.sekwah.advancedportals.core.registry.SubCommandRegistry; import com.sekwah.advancedportals.core.util.Lang; @@ -13,12 +14,15 @@ public class CommandWithSubCommands implements CommandTemplate { private final SubCommandRegistry subCommandRegistry; private final int subCommandsPerPage = 7; + private final AdvancedPortalsCore pluginCore; - public CommandWithSubCommands() { + public CommandWithSubCommands(AdvancedPortalsCore advancedPortalsCore) { this.subCommandRegistry = new SubCommandRegistry(); + this.pluginCore = advancedPortalsCore; } public boolean registerSubCommand(String arg, SubCommand subCommand, String... aliasArgs) { + pluginCore.getModule().getInjector().injectMembers(subCommand); boolean hasRegistered = false; for(String additionalArg : aliasArgs) { hasRegistered = hasRegistered || this.subCommandRegistry.registerSubCommand(additionalArg,subCommand); diff --git a/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java b/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java index 28184689..f834dc3c 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java @@ -6,7 +6,7 @@ import com.sekwah.advancedportals.core.config.Config; import com.sekwah.advancedportals.core.config.ConfigProvider; import com.sekwah.advancedportals.core.data.DataStorage; import com.sekwah.advancedportals.core.repository.ConfigRepository; -import com.sekwah.advancedportals.core.repository.ConfigRepositoryImpl; +import com.sekwah.advancedportals.core.repository.impl.ConfigRepositoryImpl; import com.sekwah.advancedportals.core.util.InfoLogger; import javax.annotation.Nonnull; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/IDestinationRepository.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/IDestinationRepository.java index fcb0eb63..2342a56a 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/IDestinationRepository.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/IDestinationRepository.java @@ -1,4 +1,9 @@ package com.sekwah.advancedportals.core.repository; -public interface IDestinationRepository extends IJsonRepository { +import com.sekwah.advancedportals.core.destination.Destination; + +import java.io.IOException; + +public interface IDestinationRepository extends IJsonRepository { + void addDestination(String name, Destination desti) throws IOException; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/IJsonRepository.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/IJsonRepository.java index f54869d7..e638de71 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/IJsonRepository.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/IJsonRepository.java @@ -1,5 +1,6 @@ package com.sekwah.advancedportals.core.repository; +import com.google.common.collect.ImmutableMap; import com.google.gson.Gson; public interface IJsonRepository { @@ -11,4 +12,8 @@ public interface IJsonRepository { boolean delete(String name); boolean update(String name, T t); + + T get(String name); + + ImmutableMap getAll(); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/ConfigRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java similarity index 90% rename from core/src/main/java/com/sekwah/advancedportals/core/repository/ConfigRepositoryImpl.java rename to core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java index 4e43c1fb..27ae372f 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/ConfigRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java @@ -1,8 +1,9 @@ -package com.sekwah.advancedportals.core.repository; +package com.sekwah.advancedportals.core.repository.impl; import com.google.inject.Singleton; import com.sekwah.advancedportals.core.config.Config; import com.sekwah.advancedportals.core.data.DataStorage; +import com.sekwah.advancedportals.core.repository.ConfigRepository; import java.util.HashMap; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/DestinationRepository.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java similarity index 73% rename from core/src/main/java/com/sekwah/advancedportals/core/repository/DestinationRepository.java rename to core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java index 322b68d6..2a2e5b36 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/DestinationRepository.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java @@ -1,7 +1,8 @@ -package com.sekwah.advancedportals.core.repository; +package com.sekwah.advancedportals.core.repository.impl; import com.google.common.collect.ImmutableMap; import com.sekwah.advancedportals.core.destination.Destination; +import com.sekwah.advancedportals.core.repository.IDestinationRepository; import javax.inject.Singleton; import java.io.FileWriter; @@ -12,21 +13,16 @@ import java.util.HashMap; import java.util.Map; @Singleton -public class DestinationRepository implements IDestinationRepository { +public class DestinationRepositoryImpl implements IDestinationRepository { private final String fileLocation = ""; private Map destinationCache = new HashMap(); - /*Is there any reason to load it into the array if it's not been used or connected? Q for Sekwah*/ - public void AddDestination(String name, Destination destination) throws IOException { + public void addDestination(String name, Destination destination) throws IOException { gson.toJson(destination, new FileWriter(fileLocation + name + ".json")); } - private void test() { - destinationCache.get(""); - } - @Override public boolean save(String name, Destination destination) { return false; @@ -51,7 +47,11 @@ public class DestinationRepository implements IDestinationRepository get(String s) { + public Destination get(String s) { + return null; + } + + public ImmutableMap getAll() { return null; } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/PortalRepository.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java similarity index 58% rename from core/src/main/java/com/sekwah/advancedportals/core/repository/PortalRepository.java rename to core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java index bf333154..61a894e3 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/PortalRepository.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java @@ -1,12 +1,14 @@ -package com.sekwah.advancedportals.core.repository; +package com.sekwah.advancedportals.core.repository.impl; +import com.google.common.collect.ImmutableMap; import com.google.inject.Singleton; import com.sekwah.advancedportals.core.data.WorldLocation; +import com.sekwah.advancedportals.core.repository.IPortalRepository; import java.util.UUID; @Singleton -public class PortalRepository implements IPortalRepository { +public class PortalRepositoryImpl implements IPortalRepository { public String getSelectedPortal(UUID uuid) { return null; @@ -31,4 +33,14 @@ public class PortalRepository implements IPortalRepository { public boolean update(String name, WorldLocation portalLocation) { return false; } + + @Override + public WorldLocation get(String name) { + return null; + } + + @Override + public ImmutableMap getAll() { + return null; + } } 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 new file mode 100644 index 00000000..6e42a727 --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java @@ -0,0 +1,107 @@ +package com.sekwah.advancedportals.core.services; + + +import com.google.common.collect.ImmutableMap; +import com.google.gson.reflect.TypeToken; +import com.google.inject.Inject; +import com.sekwah.advancedportals.core.AdvancedPortalsCore; +import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; +import com.sekwah.advancedportals.core.data.DataTag; +import com.sekwah.advancedportals.core.data.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.warphandler.TagHandler; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; + +/** + * Handles logic for all destination, this is a transient layer so it should + * not store any information. + */ +public class DestinationServices { + + + private final IDestinationRepository destinationRepository; + + @Inject + private DestinationServices(IDestinationRepository destinationRepository) { + this.destinationRepository = destinationRepository; + } + + 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; + } + + public Boolean delete(String name) { + if (!destinationRepository.containsKey(name)) { + destinationRepository.delete(name); + } + return false; + } + + public ImmutableMap getDestination() { + return destinationRepository.getAll(); + } + + public ImmutableMap getDestinations() { + return ImmutableMap.copyOf(destinationRepository.getAll()); + } + + + + + public Destination createDesti(String name, PlayerContainer player, PlayerLocation playerLocation, ArrayList tags) { + // TODO change to write messages + if(name == null || name.equals("")) { + player.sendMessage(Lang.translateColor("messageprefix.positive") + Lang.translate("desti.error.noname")); + return null; + } + else if(this.destinationRepository.containsKey(name)) { + player.sendMessage(Lang.translateColor("messageprefix.positive") + Lang.translate("desti.error.takenname")); + return null; + } + + Destination desti = new Destination(playerLocation); + for(DataTag portalTag : tags) { + desti.setArg(portalTag); + } + for(DataTag destiTag : tags) { + // TODO sort tag handle registry + /*TagHandler.Creation creation = AdvancedPortalsCore.getDestinationTagRegistry().getCreationHandler(destiTag.NAME); + if(creation != null) { + creation.created(desti, player, destiTag.VALUE); + }*/ + } + try { + this.destinationRepository.addDestination(name, desti); + } catch (IOException e) { + e.printStackTrace(); + } + this.saveDestinations(); + return desti; + } + + //TODO Change to repository + + public void loadDestinations() { + Type type = new TypeToken>() { + }.getType(); + //this.destiHashMap = this.portalsCore.getDataStorage().loadJson(type, "destinations.json"); + this.saveDestinations(); + } + + public void saveDestinations() { + /*if (this.destiHashMap == null) { + this.destiHashMap = new HashMap<>(); + } + this.portalsCore.getDataStorage().storeJson(this.destiHashMap, "destinations.json");*/ + } +} 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 new file mode 100644 index 00000000..016c6a9a --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java @@ -0,0 +1,52 @@ +package com.sekwah.advancedportals.core.services; + +import com.google.inject.Inject; +import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; +import com.sekwah.advancedportals.core.data.BlockLocation; +import com.sekwah.advancedportals.core.data.PlayerLocation; +import com.sekwah.advancedportals.core.repository.IPortalRepository; + +import java.util.UUID; + +public class PortalServices { + + private final IPortalRepository portalRepository; + + @Inject + public PortalServices(IPortalRepository portalRepository) { + this.portalRepository = portalRepository; + } + + public void addSelectedPortal(UUID selectedPlayer, String portal) { + //portalRepository.save(selectedPlayer, portal); + } + + public void removeSelectedPortal(UUID uuid) { + //selectedPortal.invalidate(uuid); + } + + public void addSelectedPosition(UUID uuid, boolean isPos1, BlockLocation portalLocation) { + //selectedPositions.put(uuid, isPos1, portalLocation); + } + + public void removeSelectedPosition(UUID uuid, boolean isPos1) { + //selectedPositions.remove(uuid, isPos1); + } + + public void removeAllSelectedHand(UUID uuid) { + //selectedPositions.remove(uuid, true); + //selectedPositions.remove(uuid, false); + } + + public void activateCooldown(PlayerContainer player) { + + } + + public void playerLeave(PlayerContainer player) { + + } + + public boolean inPortalRegion(PlayerLocation loc) { + return false; + } +} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/Response.java b/core/src/main/java/com/sekwah/advancedportals/core/services/Response.java new file mode 100644 index 00000000..a817a6f2 --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/Response.java @@ -0,0 +1,10 @@ +package com.sekwah.advancedportals.core.services; + +public class Response { + + public enum Creation { + SUCCESS, + NAME_IN_USE, + } + +}