feat: temporary player data is now stored for more perma use

This commit is contained in:
Sekwah 2024-01-14 17:53:12 +00:00
parent 01d864b1e9
commit a0a4633b9e
13 changed files with 146 additions and 40 deletions

View File

@ -14,6 +14,7 @@ import com.sekwah.advancedportals.core.module.AdvancedPortalsModule;
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 class AdvancedPortalsCore {
}
public void onDisable() {
for(var playerContainer : this.serverContainer.getPlayers()) {
playerDataRepository.playerLeave(playerContainer);
}
this.infoLogger.log(Lang.translate("logger.plugindisable"));
}

View File

@ -11,7 +11,7 @@ import com.sekwah.advancedportals.core.serializeddata.PlayerLocation;
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 @@ import java.util.Objects;
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 class CoreListeners {
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 @@ public class CoreListeners {
}
public void worldChange(PlayerContainer player) {
this.portalTempDataServices.activateCooldown(player);
this.playerDataServices.activateCooldown(player);
}
public boolean preventEntityCombust(EntityContainer entity) {

View File

@ -9,8 +9,7 @@ import com.sekwah.advancedportals.core.connector.containers.ServerContainer;
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 @@ import java.util.List;
public class ShowDestiSubCommand implements SubCommand, SubCommand.SubCommandOnInit {
@Inject
PortalTempDataServices tempDataServices;
PlayerDataServices tempDataServices;
@Inject
GameScheduler gameScheduler;

View File

@ -10,7 +10,7 @@ import com.sekwah.advancedportals.core.portal.AdvancedPortal;
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 com.sekwah.advancedportals.core.util.Lang;
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;

View File

@ -4,6 +4,8 @@ import com.google.inject.*;
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.IPortalRepository;
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 class AdvancedPortalsModule extends AbstractModule {
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

View File

@ -10,8 +10,6 @@ public interface IJsonRepository<T> {
boolean delete(String name);
boolean update(String name, T t);
T get(String name);
List<String> getAllNames();

View File

@ -3,10 +3,11 @@ package com.sekwah.advancedportals.core.repository;
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<PlayerData> {
void addSelectedPortal(UUID selectedPlayer, String portal);
void removeSelectedPortal(UUID uuid);

View File

@ -36,11 +36,6 @@ public class DestinationRepositoryImpl implements IDestinationRepository {
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");
}

View File

@ -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<String> getAllNames() {
return null;
}
@Override
public List<PlayerData> 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;
}
}

View File

@ -42,11 +42,6 @@ public class PortalRepositoryImpl implements IPortalRepository {
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");

View File

@ -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 @@ package com.sekwah.advancedportals.core.serializeddata;
*
* 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<String, String> perPortalCooldowns = new HashMap<>();
private String selectedPortal;
public BlockLocation getPos1() {

View File

@ -3,8 +3,9 @@ package com.sekwah.advancedportals.core.services;
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.Map;
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<UUID, PlayerTempData> tempDataMap = new HashMap<>();
private Map<UUID, PlayerData> 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 final class PortalTempDataServices {
}
public void playerLeave(PlayerContainer player) {
tempDataRepository.save(player.getUUID().toString(), getPlayerTempData(player));
tempDataMap.remove(player.getUUID());
}

View File

@ -9,9 +9,8 @@ import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
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 class PortalServices {
}
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 class PortalServices {
}
public AdvancedPortal createPortal(PlayerContainer player, ArrayList<DataTag> 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"));