Skip to content

Commit

Permalink
feat: temporary player data is now stored for more perma use
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Jan 14, 2024
1 parent 01d864b commit a0a4633
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,9 @@ public class AdvancedPortalsCore {
@Inject
private DestinationServices destinationServices;

@Inject
private PlayerDataServices playerDataRepository;

@Inject
private GameScheduler gameScheduler;

Expand Down Expand Up @@ -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"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,7 +20,7 @@
public class CoreListeners {

@Inject
private PortalTempDataServices portalTempDataServices;
private PlayerDataServices playerDataServices;

@Inject
private PortalServices portalServices;
Expand All @@ -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() {
Expand Down Expand Up @@ -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 &&
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +23,7 @@
public class ShowDestiSubCommand implements SubCommand, SubCommand.SubCommandOnInit {

@Inject
PortalTempDataServices tempDataServices;
PlayerDataServices tempDataServices;

@Inject
GameScheduler gameScheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
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;
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.
Expand All @@ -31,7 +29,7 @@ public class ShowPortalSubCommand implements SubCommand, SubCommand.SubCommandOn
boolean alternate_show_trigger = true;

@Inject
PortalTempDataServices tempDataServices;
PlayerDataServices tempDataServices;

@Inject
GameScheduler gameScheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerData> {
void addSelectedPortal(UUID selectedPlayer, String portal);

void removeSelectedPortal(UUID uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.sekwah.advancedportals.core.serializeddata;

import java.util.HashMap;

/**
* Possibly one of the only files in this package not designed to be serialised.
*
* Any temporary data about players will be stored here and cleaned up when the player leaves the server.
*
* 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
Expand All @@ -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
Expand All @@ -39,6 +41,8 @@ public class PlayerTempData {
*/
private long globalCooldown;

private HashMap<String, String> perPortalCooldowns = new HashMap<>();

private String selectedPortal;

public BlockLocation getPos1() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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) {
Expand All @@ -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());
}

Expand Down
Loading

0 comments on commit a0a4633

Please sign in to comment.