Skip to content

Commit

Permalink
Rename Json...Configuration to Json...Data and add JsonShopData cache
Browse files Browse the repository at this point in the history
  • Loading branch information
SparklingComet committed Oct 13, 2023
1 parent 6371430 commit e2d0a16
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/shanerx/tradeshop/TradeShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import org.shanerx.tradeshop.data.config.Language;
import org.shanerx.tradeshop.data.config.Setting;
import org.shanerx.tradeshop.data.storage.DataStorage;
import org.shanerx.tradeshop.data.storage.DataType;
import org.shanerx.tradeshop.data.storage.Json.JsonShopConfiguration;
import org.shanerx.tradeshop.player.JoinEventListener;
import org.shanerx.tradeshop.player.Permissions;
import org.shanerx.tradeshop.shop.ShopSign;
Expand All @@ -48,6 +46,7 @@
import org.shanerx.tradeshop.shop.listeners.ShopProtectionListener;
import org.shanerx.tradeshop.shop.listeners.ShopRestockListener;
import org.shanerx.tradeshop.shop.listeners.ShopTradeListener;
import org.shanerx.tradeshop.shop.listeners.ChunkUnloadListener;
import org.shanerx.tradeshop.utils.debug.Debug;
import org.shanerx.tradeshop.utils.logging.transactionlogging.TransactionLogger;
import org.shanerx.tradeshop.utils.logging.transactionlogging.listeners.SuccessfulTradeEventListener;
Expand Down Expand Up @@ -156,6 +155,7 @@ private void registration() {
pm.registerEvents(new ShopTradeListener(), this);
pm.registerEvents(new ShopRestockListener(this), this);
pm.registerEvents(new SuccessfulTradeEventListener(this), this);
pm.registerEvents(new ChunkUnloadListener(this), this);

if (getServer().getVersion().toLowerCase().contains("paper")) {
pm.registerEvents(new PaperShopProtectionListener(), this);
Expand Down
71 changes: 41 additions & 30 deletions src/main/java/org/shanerx/tradeshop/data/storage/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.data.storage.Json.JsonLinkageConfiguration;
import org.shanerx.tradeshop.data.storage.Json.JsonPlayerConfiguration;
import org.shanerx.tradeshop.data.storage.Json.JsonShopConfiguration;
import org.shanerx.tradeshop.data.storage.Json.JsonLinkageData;
import org.shanerx.tradeshop.data.storage.Json.JsonPlayerData;
import org.shanerx.tradeshop.data.storage.Json.JsonShopData;
import org.shanerx.tradeshop.item.ShopItemSide;
import org.shanerx.tradeshop.player.PlayerSetting;
import org.shanerx.tradeshop.shop.Shop;
Expand Down Expand Up @@ -101,7 +101,7 @@ public boolean validate() {

//Check for err files
Bukkit.getServer().getWorlds().forEach((w) -> {
File[] list = JsonShopConfiguration.getFilesInFolder(w.getName());
File[] list = JsonShopData.getFilesInFolder(w.getName());
if (list != null && list.length > 0) {
errFiles.addAll(Arrays.stream(list).filter((f) -> FilenameUtils.getExtension(f.getName()).toLowerCase().contains("err")).collect(Collectors.toList()));
}
Expand All @@ -111,7 +111,7 @@ public boolean validate() {

//Check for and correct malformed files
Bukkit.getServer().getWorlds().forEach((w) -> {
File[] list = JsonShopConfiguration.getFilesInFolder(w.getName());
File[] list = JsonShopData.getFilesInFolder(w.getName());
if (list != null && list.length > 0) {
Arrays.stream(list).forEach((f) -> {
try {
Expand Down Expand Up @@ -148,7 +148,7 @@ public boolean validate() {

TradeShop.getPlugin().getDebugger().log("Removing empty player files... ", DebugLevels.DATA_VERIFICATION);

File[] playerFiles = JsonPlayerConfiguration.getAllPlayers();
File[] playerFiles = JsonPlayerData.getAllPlayers();
if (playerFiles != null) {
List<String> deletedResults = new ArrayList<>();
Map<String, Exception> failedResults = new HashMap<>();
Expand Down Expand Up @@ -179,34 +179,34 @@ public boolean validate() {

public Shop loadShopFromSign(ShopLocation sign) {
Shop cached = shopCache.getIfPresent(sign.serialize());
return cached != null ? cached : getShopConfiguration(sign.getChunk()).load(sign);
return cached != null ? cached : getShopData(sign.getChunk()).load(sign);
}

public Shop loadShopFromStorage(ShopLocation chest) {
return loadShopFromSign(getLinkageConfiguration(chest.getWorld()).getLinkedShop(chest));
return loadShopFromSign(getLinkageData(chest.getWorld()).getLinkedShop(chest));
}

public void saveShop(Shop shop) {
shopCache.put(shop.getShopLocationAsSL().serialize(), shop);
getShopConfiguration(shop.getShopLocation().getChunk()).save(shop);
getShopData(shop.getShopLocation().getChunk()).save(shop);
}

public void removeShop(Shop shop) {
shopCache.invalidate(shop.getShopLocationAsSL().serialize());
getShopConfiguration(shop.getShopLocation().getChunk()).remove(shop.getShopLocationAsSL());
getLinkageConfiguration(shop.getShopLocationAsSL().getWorld()).removeShop(shop.getShopLocationAsSL());
getShopData(shop.getShopLocation().getChunk()).remove(shop.getShopLocationAsSL());
getLinkageData(shop.getShopLocationAsSL().getWorld()).removeShop(shop.getShopLocationAsSL());
}

public int getShopCountInChunk(Chunk chunk) {
return getShopConfiguration(chunk).size();
return getShopData(chunk).size();
}

public List<Shop> getMatchingShopsInChunk(ChunkSnapshot chunk, boolean inStock, List<ItemStack> desiredCosts, List<ItemStack> desiredProducts) {
List<Shop> matchingShops = new ArrayList<>();
ShopChunk shopChunk = new ShopChunk(chunk);

if (chunkExists(shopChunk)) {
ShopConfiguration config = getShopConfiguration(shopChunk);
ShopConfiguration config = getShopData(shopChunk);

config.list().forEach((shopLoc) -> {
Shop shop = config.loadASync(shopLoc);
Expand Down Expand Up @@ -234,7 +234,7 @@ public int getShopCountInWorld(World world) {
if (folder.exists() && folder.listFiles() != null) {
for (File file : folder.listFiles()) {
if (file.getName().contains(worldName))
count.addAndGet(new JsonShopConfiguration(ShopChunk.deserialize(file.getName().replace(".json", ""))).size());
count.addAndGet(new JsonShopData(ShopChunk.deserialize(file.getName().replace(".json", ""))).size());
}
}
break;
Expand All @@ -247,65 +247,76 @@ public int getShopCountInWorld(World world) {
}

public PlayerSetting loadPlayer(UUID uuid) {
PlayerSetting playerSetting = playerCache.getIfPresent(uuid) != null ? playerCache.getIfPresent(uuid) : getPlayerConfiguration(uuid).load();
PlayerSetting playerSetting = playerCache.getIfPresent(uuid) != null ? playerCache.getIfPresent(uuid) : getPlayerData(uuid).load();

//If playerSetting data not find create new and return
return playerSetting != null ? playerSetting : new PlayerSetting(uuid);
}

public void savePlayer(PlayerSetting playerSetting) {
playerCache.put(playerSetting.getUuid(), playerSetting);
getPlayerConfiguration(playerSetting.getUuid()).save(playerSetting);
getPlayerData(playerSetting.getUuid()).save(playerSetting);
}

public void removePlayer(PlayerSetting playerSetting) {
playerCache.invalidate(playerSetting.getUuid());
getPlayerConfiguration(playerSetting.getUuid()).remove();
getPlayerData(playerSetting.getUuid()).remove();
}

public ShopLocation getChestLinkage(ShopLocation chestLocation) {
return getLinkageConfiguration(chestLocation.getWorld()).getLinkedShop(chestLocation);
return getLinkageData(chestLocation.getWorld()).getLinkedShop(chestLocation);
}

public void addChestLinkage(ShopLocation chestLocation, ShopLocation shopLocation) {
if (Bukkit.isPrimaryThread() && getChestLinkage(chestLocation) == null)
getLinkageConfiguration(chestLocation.getWorld()).add(chestLocation, shopLocation);
getLinkageData(chestLocation.getWorld()).add(chestLocation, shopLocation);
}

public void removeChestLinkage(ShopLocation chestLocation) {
getLinkageConfiguration(chestLocation.getWorld()).removeChest(chestLocation);
getLinkageData(chestLocation.getWorld()).removeChest(chestLocation);
}

protected PlayerConfiguration getPlayerConfiguration(UUID uuid) {
protected PlayerConfiguration getPlayerData(UUID uuid) {
if (dataType == DataType.FLATFILE) {
return new JsonPlayerConfiguration(uuid);
return new JsonPlayerData(uuid);
}
throw new NotImplementedException("Data storage type " + dataType + " has not been implemented yet.");
}

protected ShopConfiguration getShopConfiguration(Chunk chunk) {
return getShopConfiguration(new ShopChunk(chunk));
protected ShopConfiguration getShopData(Chunk chunk) {
return getShopData(new ShopChunk(chunk));
}

protected ShopConfiguration getShopConfiguration(ShopChunk chunk) {
private Map<String, JsonShopData> chunkDataCache = new HashMap<>();
protected ShopConfiguration getShopData(ShopChunk chunk) {
if (dataType == DataType.FLATFILE) {
return new JsonShopConfiguration(chunk);
String serializedChunk = chunk.serialize();
if (chunkDataCache.containsKey(serializedChunk))
return chunkDataCache.get(serializedChunk);
JsonShopData data = new JsonShopData(chunk);
chunkDataCache.put(serializedChunk, data);
return new JsonShopData(chunk);
}

throw new NotImplementedException("Data storage type " + dataType + " has not been implemented yet.");
}

public void dropShopData(ShopChunk chunk) {
chunkDataCache.remove(chunk.serialize());
}

protected boolean chunkExists(ShopChunk chunk) {
if (dataType == DataType.FLATFILE) {
return JsonShopConfiguration.doesConfigExist(chunk);
return JsonShopData.doesConfigExist(chunk);
}
throw new NotImplementedException("Data storage type " + dataType + " has not been implemented yet.");
}

protected LinkageConfiguration getLinkageConfiguration(World w) {
protected LinkageConfiguration getLinkageData(World w) {

if (linkCache.getIfPresent(w) == null) {
if (dataType == DataType.FLATFILE) {
linkCache.put(w, new JsonLinkageConfiguration(w));
linkCache.put(w, new JsonLinkageData(w));
}
}

Expand All @@ -319,7 +330,7 @@ protected LinkageConfiguration getLinkageConfiguration(World w) {
public void ensureFinalSave() {
// for onDisable !!!
if (dataType == DataType.FLATFILE) {
JsonShopConfiguration.SaveThreadMaster.getInstance().saveEverythingNow();
JsonShopData.SaveThreadMaster.getInstance().saveEverythingNow();
}
// SQLITE will have an analogous branch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import java.util.HashMap;
import java.util.Map;

public class JsonLinkageConfiguration extends JsonConfiguration implements LinkageConfiguration {
public class JsonLinkageData extends JsonConfiguration implements LinkageConfiguration {

Map<String, String> linkageData;

public JsonLinkageConfiguration(World world) {
public JsonLinkageData(World world) {
super(world.getName(), "chest_linkage");
load();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
import java.util.Map;
import java.util.UUID;

public class JsonPlayerConfiguration extends JsonConfiguration implements PlayerConfiguration {
public class JsonPlayerData extends JsonConfiguration implements PlayerConfiguration {

private final transient UUID playerUUID;
private transient PlayerSetting playerSetting;
private static final String playerFolder = "Players";

public JsonPlayerConfiguration(UUID uuid) {
public JsonPlayerData(UUID uuid) {
super(playerFolder, uuid.toString());

playerUUID = uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;

public class JsonShopConfiguration extends JsonConfiguration implements ShopConfiguration {
public class JsonShopData extends JsonConfiguration implements ShopConfiguration {

private final ShopChunk chunk;

public JsonShopConfiguration(ShopChunk chunk) {
public JsonShopData(ShopChunk chunk) {
super(chunk.getWorldName(), chunk.serialize());
this.chunk = chunk;
}
Expand Down Expand Up @@ -272,9 +272,9 @@ public void run() {
}

try {
FileChannel chan = FileChannel.open(file.toPath(), StandardOpenOption.WRITE, StandardOpenOption.WRITE);
FileChannel chan = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
chan.write(ByteBuffer.wrap(str.getBytes()));
chan.force(false);
chan.force(true);
chan.close();
} catch (IOException e) {
logger.log(Level.SEVERE, "Could not save " + file.getName() + " file! Data may be lost!", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.shanerx.tradeshop.shop.listeners;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.shoplocation.ShopChunk;

public class ChunkUnloadListener implements Listener {

private TradeShop plugin;

public ChunkUnloadListener(TradeShop plugin) {
this.plugin = plugin;
}

@EventHandler
public void onChunkUnload(ChunkUnloadEvent e) {
ShopChunk shopChunk = new ShopChunk(e.getChunk());
TradeShop.getPlugin().getVarManager().getDataStorage().dropShopData(shopChunk);
}
}

0 comments on commit e2d0a16

Please sign in to comment.