From a0a4633b9e01d0ff42484ffbf4b2f85a5abe95f3 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Sun, 14 Jan 2024 17:53:12 +0000 Subject: [PATCH] feat: temporary player data is now stored for more perma use --- .../core/AdvancedPortalsCore.java | 7 ++ .../advancedportals/core/CoreListeners.java | 14 +-- .../desti/ShowDestiSubCommand.java | 5 +- .../portal/ShowPortalSubCommand.java | 6 +- .../core/module/AdvancedPortalsModule.java | 4 +- .../core/repository/IJsonRepository.java | 2 - ...sitory.java => IPlayerDataRepository.java} | 3 +- .../impl/DestinationRepositoryImpl.java | 5 - .../impl/PlayerDataRepositoryImpl.java | 95 +++++++++++++++++++ .../repository/impl/PortalRepositoryImpl.java | 5 - .../{PlayerTempData.java => PlayerData.java} | 8 +- ...aServices.java => PlayerDataServices.java} | 23 ++++- .../core/services/PortalServices.java | 9 +- 13 files changed, 146 insertions(+), 40 deletions(-) rename core/src/main/java/com/sekwah/advancedportals/core/repository/{IPortalTempDataRepository.java => IPlayerDataRepository.java} (83%) create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java rename core/src/main/java/com/sekwah/advancedportals/core/serializeddata/{PlayerTempData.java => PlayerData.java} (92%) rename core/src/main/java/com/sekwah/advancedportals/core/services/{PortalTempDataServices.java => PlayerDataServices.java} (67%) 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 e541b851..fd480146 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -14,6 +14,7 @@ import com.sekwah.advancedportals.core.repository.ConfigRepository; import com.sekwah.advancedportals.core.services.DestinationServices; import com.sekwah.advancedportals.core.services.PortalServices; +import com.sekwah.advancedportals.core.services.PlayerDataServices; import com.sekwah.advancedportals.core.tags.activation.DestiTag; import com.sekwah.advancedportals.core.tags.activation.NameTag; import com.sekwah.advancedportals.core.tags.activation.TriggerBlockTag; @@ -60,6 +61,9 @@ public class AdvancedPortalsCore { @Inject private DestinationServices destinationServices; + @Inject + private PlayerDataServices playerDataRepository; + @Inject private GameScheduler gameScheduler; @@ -168,6 +172,9 @@ public void loadPortalConfig() { } public void onDisable() { + for(var playerContainer : this.serverContainer.getPlayers()) { + playerDataRepository.playerLeave(playerContainer); + } this.infoLogger.log(Lang.translate("logger.plugindisable")); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java b/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java index a45fbb75..2ee89138 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java @@ -11,7 +11,7 @@ import com.sekwah.advancedportals.core.permissions.PortalPermissions; import com.sekwah.advancedportals.core.repository.ConfigRepository; import com.sekwah.advancedportals.core.services.PortalServices; -import com.sekwah.advancedportals.core.services.PortalTempDataServices; +import com.sekwah.advancedportals.core.services.PlayerDataServices; import com.sekwah.advancedportals.core.util.GameScheduler; import com.sekwah.advancedportals.core.util.Lang; @@ -20,7 +20,7 @@ public class CoreListeners { @Inject - private PortalTempDataServices portalTempDataServices; + private PlayerDataServices playerDataServices; @Inject private PortalServices portalServices; @@ -32,15 +32,15 @@ public class CoreListeners { private GameScheduler gameScheduler; public void playerJoin(PlayerContainer player) { - this.portalTempDataServices.activateCooldown(player); + this.playerDataServices.activateCooldown(player); } public void teleportEvent(PlayerContainer player) { - this.portalTempDataServices.activateCooldown(player); + this.playerDataServices.activateCooldown(player); } public void playerLeave(PlayerContainer player) { - this.portalTempDataServices.playerLeave(player); + this.playerDataServices.playerLeave(player); } public void tick() { @@ -134,7 +134,7 @@ public boolean playerInteractWithBlock(PlayerContainer player, String blockMater if(itemName != null && (player.isOp() || PortalPermissions.CREATE_PORTAL.hasPermission(player)) && itemMaterialName.equalsIgnoreCase(this.configRepository.getSelectorMaterial()) && (!this.configRepository.getUseOnlySpecialAxe() || itemName.equals("\u00A7ePortal Region Selector"))) { - this.portalTempDataServices.playerSelectorActivate(player, blockLoc, leftClick); + this.playerDataServices.playerSelectorActivate(player, blockLoc, leftClick); return false; } else if(itemName != null && leftClick && @@ -157,7 +157,7 @@ else if(itemName != null && leftClick && } public void worldChange(PlayerContainer player) { - this.portalTempDataServices.activateCooldown(player); + this.playerDataServices.activateCooldown(player); } public boolean preventEntityCombust(EntityContainer entity) { diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/ShowDestiSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/ShowDestiSubCommand.java index 29cac99f..96b1858b 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/ShowDestiSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/ShowDestiSubCommand.java @@ -9,8 +9,7 @@ import com.sekwah.advancedportals.core.destination.Destination; import com.sekwah.advancedportals.core.repository.ConfigRepository; import com.sekwah.advancedportals.core.services.DestinationServices; -import com.sekwah.advancedportals.core.services.PortalTempDataServices; -import com.sekwah.advancedportals.core.tags.activation.NameTag; +import com.sekwah.advancedportals.core.services.PlayerDataServices; import com.sekwah.advancedportals.core.util.Debug; import com.sekwah.advancedportals.core.util.GameScheduler; import com.sekwah.advancedportals.core.util.Lang; @@ -24,7 +23,7 @@ public class ShowDestiSubCommand implements SubCommand, SubCommand.SubCommandOnInit { @Inject - PortalTempDataServices tempDataServices; + PlayerDataServices tempDataServices; @Inject GameScheduler gameScheduler; 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 4038d0e0..02ea046a 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 @@ -10,7 +10,7 @@ import com.sekwah.advancedportals.core.repository.ConfigRepository; import com.sekwah.advancedportals.core.serializeddata.BlockLocation; import com.sekwah.advancedportals.core.services.PortalServices; -import com.sekwah.advancedportals.core.services.PortalTempDataServices; +import com.sekwah.advancedportals.core.services.PlayerDataServices; import com.sekwah.advancedportals.core.tags.activation.NameTag; import com.sekwah.advancedportals.core.util.Debug; import com.sekwah.advancedportals.core.util.GameScheduler; @@ -18,8 +18,6 @@ import java.awt.*; import java.util.List; -import java.util.function.BiConsumer; -import java.util.function.Consumer; /** * This will be different from the old show command and I believe it is 1.16+ till the latest version as of writing this. @@ -31,7 +29,7 @@ public class ShowPortalSubCommand implements SubCommand, SubCommand.SubCommandOn boolean alternate_show_trigger = true; @Inject - PortalTempDataServices tempDataServices; + PlayerDataServices tempDataServices; @Inject GameScheduler gameScheduler; 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 9cc7ae6c..f7fa7151 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 @@ -4,6 +4,8 @@ import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.connector.containers.ServerContainer; import com.sekwah.advancedportals.core.registry.TagRegistry; +import com.sekwah.advancedportals.core.repository.IPlayerDataRepository; +import com.sekwah.advancedportals.core.repository.impl.PlayerDataRepositoryImpl; import com.sekwah.advancedportals.core.serializeddata.config.Config; import com.sekwah.advancedportals.core.serializeddata.config.ConfigProvider; import com.sekwah.advancedportals.core.serializeddata.DataStorage; @@ -13,7 +15,6 @@ import com.sekwah.advancedportals.core.repository.impl.ConfigRepositoryImpl; import com.sekwah.advancedportals.core.repository.impl.DestinationRepositoryImpl; import com.sekwah.advancedportals.core.repository.impl.PortalRepositoryImpl; -import com.sekwah.advancedportals.core.util.GameScheduler; import com.sekwah.advancedportals.core.util.InfoLogger; import javax.annotation.Nonnull; @@ -40,6 +41,7 @@ public AdvancedPortalsModule(AdvancedPortalsCore advancedPortalsCore) { protected void configure() { bind(IPortalRepository.class).to(PortalRepositoryImpl.class).in(Scopes.SINGLETON); bind(IDestinationRepository.class).to(DestinationRepositoryImpl.class).in(Scopes.SINGLETON); + bind(IPlayerDataRepository.class).to(PlayerDataRepositoryImpl.class).in(Scopes.SINGLETON); bind(ConfigRepository.class).to(ConfigRepositoryImpl.class).in(Scopes.SINGLETON); // Instances 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 d0afa2db..869d1af3 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 @@ -10,8 +10,6 @@ public interface IJsonRepository { boolean delete(String name); - boolean update(String name, T t); - T get(String name); List getAllNames(); diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/IPortalTempDataRepository.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/IPlayerDataRepository.java similarity index 83% rename from core/src/main/java/com/sekwah/advancedportals/core/repository/IPortalTempDataRepository.java rename to core/src/main/java/com/sekwah/advancedportals/core/repository/IPlayerDataRepository.java index e5df0fd9..15727b78 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/IPortalTempDataRepository.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/IPlayerDataRepository.java @@ -3,10 +3,11 @@ import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.serializeddata.BlockLocation; import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; +import com.sekwah.advancedportals.core.serializeddata.PlayerData; import java.util.UUID; -public interface IPortalTempDataRepository { +public interface IPlayerDataRepository extends IJsonRepository { void addSelectedPortal(UUID selectedPlayer, String portal); void removeSelectedPortal(UUID uuid); 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 2e8a8ef8..d55eb8ce 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 @@ -36,11 +36,6 @@ public boolean delete(String name) { return dataStorage.deleteFile(fileLocation + name + ".json"); } - @Override - public boolean update(String name, Destination destination) { - return false; - } - public Destination get(String name) { return dataStorage.loadJson(Destination.class, fileLocation + name + ".json"); } 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 new file mode 100644 index 00000000..96674af0 --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java @@ -0,0 +1,95 @@ +package com.sekwah.advancedportals.core.repository.impl; + +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.ConfigRepository; +import com.sekwah.advancedportals.core.repository.IPlayerDataRepository; +import com.sekwah.advancedportals.core.serializeddata.BlockLocation; +import com.sekwah.advancedportals.core.serializeddata.DataStorage; +import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; +import com.sekwah.advancedportals.core.serializeddata.PlayerData; + +import java.util.List; +import java.util.UUID; + +public class PlayerDataRepositoryImpl implements IPlayerDataRepository { + + private final String fileLocation = "playerData/"; + + @Inject + private ConfigRepository configRepository; + + @Inject + DataStorage dataStorage; + + @Override + public boolean save(String name, PlayerData playerData) { + return dataStorage.storeJson(playerData, fileLocation + name + ".json"); + } + + @Override + public boolean containsKey(String name) { + return false; + } + + @Override + public boolean delete(String name) { + return dataStorage.deleteFile(fileLocation + name + ".json"); + } + + @Override + public PlayerData get(String name) { + return dataStorage.loadJson(PlayerData.class, fileLocation + name + ".json"); + } + + @Override + public List getAllNames() { + return null; + } + + @Override + public List getAll() { + return null; + } + + @Override + public void addSelectedPortal(UUID selectedPlayer, String portal) { + + } + + @Override + public void removeSelectedPortal(UUID uuid) { + + } + + @Override + public void addSelectedPosition(UUID uuid, boolean isPos1, BlockLocation portalLocation) { + + } + + @Override + public void removeSelectedPosition(UUID uuid, boolean isPos1) { + + } + + @Override + public void removeAllSelectedHand(UUID uuid) { + + } + + @Override + public void activateCooldown(PlayerContainer player) { + + } + + @Override + public void playerLeave(PlayerContainer player) { + + } + + @Override + public boolean inPortalRegion(PlayerLocation loc) { + return false; + } +} 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 430bb42c..8793d3a0 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 @@ -42,11 +42,6 @@ public boolean delete(String name) { return dataStorage.deleteFile(fileLocation + name + ".json"); } - @Override - public boolean update(String name, AdvancedPortal portal) { - return false; - } - @Override public AdvancedPortal get(String name) { return dataStorage.loadJson(AdvancedPortal.class, fileLocation + name + ".json"); diff --git a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerTempData.java b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerData.java similarity index 92% rename from core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerTempData.java rename to core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerData.java index ef06a0e0..23d5d35d 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerTempData.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerData.java @@ -1,5 +1,7 @@ package com.sekwah.advancedportals.core.serializeddata; +import java.util.HashMap; + /** * Possibly one of the only files in this package not designed to be serialised. * @@ -7,7 +9,7 @@ * * This is not a place to store long term data e.g. if you want to make a player unable to use a portal over hours/days. */ -public class PlayerTempData { +public class PlayerData { /** * Portal selection position 1 @@ -22,7 +24,7 @@ public class PlayerTempData { /** * If to show portals near the player */ - private boolean portalVisible = true; + private boolean portalVisible = false; /** * If to show destination blocks near the player @@ -39,6 +41,8 @@ public class PlayerTempData { */ private long globalCooldown; + private HashMap perPortalCooldowns = new HashMap<>(); + private String selectedPortal; public BlockLocation getPos1() { diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalTempDataServices.java b/core/src/main/java/com/sekwah/advancedportals/core/services/PlayerDataServices.java similarity index 67% rename from core/src/main/java/com/sekwah/advancedportals/core/services/PortalTempDataServices.java rename to core/src/main/java/com/sekwah/advancedportals/core/services/PlayerDataServices.java index 713ec073..8c142c61 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalTempDataServices.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/PlayerDataServices.java @@ -3,8 +3,9 @@ import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.repository.ConfigRepository; +import com.sekwah.advancedportals.core.repository.IPlayerDataRepository; import com.sekwah.advancedportals.core.serializeddata.BlockLocation; -import com.sekwah.advancedportals.core.serializeddata.PlayerTempData; +import com.sekwah.advancedportals.core.serializeddata.PlayerData; import com.sekwah.advancedportals.core.util.Lang; import javax.inject.Singleton; @@ -13,18 +14,29 @@ import java.util.UUID; @Singleton -public final class PortalTempDataServices { +public final class PlayerDataServices { + /** * Possibly change to the cache map Aztec was talking about */ - private Map tempDataMap = new HashMap<>(); + private Map tempDataMap = new HashMap<>(); + + @Inject + private IPlayerDataRepository tempDataRepository; @Inject private ConfigRepository configRepository; - public PlayerTempData getPlayerTempData(PlayerContainer player) { - return tempDataMap.computeIfAbsent(player.getUUID(), uuid -> new PlayerTempData()); + public PlayerData getPlayerTempData(PlayerContainer player) { + return tempDataMap.computeIfAbsent(player.getUUID(), uuid -> { + var tempData = tempDataRepository.get(player.getUUID().toString()); + + if(tempData == null) { + tempData = new PlayerData(); + } + return tempData; + }); } public void activateCooldown(PlayerContainer player) { @@ -33,6 +45,7 @@ public void activateCooldown(PlayerContainer player) { } public void playerLeave(PlayerContainer player) { + tempDataRepository.save(player.getUUID().toString(), getPlayerTempData(player)); tempDataMap.remove(player.getUUID()); } 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 dcb327eb..b9d7000a 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 @@ -9,9 +9,8 @@ import com.sekwah.advancedportals.core.serializeddata.DataTag; import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; import com.sekwah.advancedportals.core.portal.AdvancedPortal; -import com.sekwah.advancedportals.core.serializeddata.PlayerTempData; +import com.sekwah.advancedportals.core.serializeddata.PlayerData; import com.sekwah.advancedportals.core.tags.activation.NameTag; -import com.sekwah.advancedportals.core.util.InfoLogger; import com.sekwah.advancedportals.core.util.Lang; import com.sekwah.advancedportals.core.warphandler.Tag; @@ -25,7 +24,7 @@ public class PortalServices { private IPortalRepository portalRepository; @Inject - private PortalTempDataServices portalTempDataServices; + private PlayerDataServices playerDataServices; @Inject private ConfigRepository configRepository; @@ -74,7 +73,7 @@ public boolean inPortalRegion(BlockLocation loc, int extraBlocks) { } public void playerMove(PlayerContainer player, PlayerLocation toLoc) { - PlayerTempData tempData = portalTempDataServices.getPlayerTempData(player); + PlayerData tempData = playerDataServices.getPlayerTempData(player); if(tempData.getGlobalCooldown() > System.currentTimeMillis()) { return; @@ -118,7 +117,7 @@ public AdvancedPortal createPortal(BlockLocation pos1, BlockLocation pos2, List< } public AdvancedPortal createPortal(PlayerContainer player, ArrayList tags) { - PlayerTempData tempData = portalTempDataServices.getPlayerTempData(player); + PlayerData tempData = playerDataServices.getPlayerTempData(player); if(tempData.getPos1() == null || tempData.getPos2() == null) { player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("portal.error.selection.missing"));