Skip to content

Commit

Permalink
Use uuid as map index for backpack cache
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgH93 committed Jun 27, 2023
1 parent 01706e4 commit 795cc63
Showing 1 changed file with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import at.pcgamingfreaks.Minepacks.Bukkit.Database.UnCacheStrategies.OnDisconnect;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.UnCacheStrategies.UnCacheStrategy;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;

import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -36,11 +37,9 @@

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;

public abstract class Database implements Listener
{
Expand All @@ -51,7 +50,7 @@ public abstract class Database implements Listener
protected final boolean onlineUUIDs, bungeeCordMode, forceSaveOnUnload;
protected boolean useUUIDSeparators, asyncSave = true;
protected long maxAge;
private final Map<OfflinePlayer, Backpack> backpacks = new ConcurrentHashMap<>();
private final Map<UUID, Backpack> backpacks = new ConcurrentHashMap<>();
private final UnCacheStrategy unCacheStrategie;
private final File backupFolder;

Expand All @@ -78,7 +77,7 @@ public void close()
{
HandlerList.unregisterAll(this);
asyncSave = false;
backpacks.forEach((key, value) -> value.closeAll());
backpacks.forEach((key, value) -> { if (forceSaveOnUnload) { value.setChanged(); } value.closeAll(); });
backpacks.clear();
unCacheStrategie.close();
}
Expand All @@ -91,10 +90,10 @@ public void close()
ConnectionProvider connectionProvider = null;
if(dbType.equals("shared") || dbType.equals("external") || dbType.equals("global"))
{
/*if[STANDALONE]
plugin.getLogger().warning(ConsoleColor.RED + "The shared database connection option is not available in standalone mode!" + ConsoleColor.RESET);
return null;
else[STANDALONE]*/
/*if[STANDALONE]
plugin.getLogger().warning(ConsoleColor.RED + "The shared database connection option is not available in standalone mode!" + ConsoleColor.RESET);
return null;
else[STANDALONE]*/
at.pcgamingfreaks.PluginLib.Database.DatabaseConnectionPool pool = at.pcgamingfreaks.PluginLib.Bukkit.PluginLib.getInstance().getDatabaseConnectionPool();
if(pool == null)
{
Expand Down Expand Up @@ -184,12 +183,12 @@ protected String getPlayerFormattedUUID(OfflinePlayer player)
/**
* Gets a backpack for a player. This only includes backpacks that are cached! Do not use it unless you are sure that you only want to use cached data!
*
* @param player The player who's backpack should be retrieved.
* @param player The player whose backpack should be retrieved.
* @return The backpack for the player. null if the backpack is not in the cache.
*/
public @Nullable Backpack getBackpack(@Nullable OfflinePlayer player)
{
return (player == null) ? null : backpacks.get(player);
return (player == null) ? null : backpacks.get(player.getUniqueId());
}

public void getBackpack(final OfflinePlayer player, final Callback<at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack> callback, final boolean createNewOnFail)
Expand All @@ -198,15 +197,15 @@ public void getBackpack(final OfflinePlayer player, final Callback<at.pcgamingfr
{
return;
}
Backpack lbp = backpacks.get(player);
Backpack lbp = backpacks.get(player.getUniqueId());
if(lbp == null)
{
loadBackpack(player, new Callback<Backpack>()
{
@Override
public void onResult(Backpack backpack)
{
backpacks.put(player, backpack);
backpacks.put(player.getUniqueId(), backpack);
callback.onResult(backpack);
}

Expand All @@ -216,7 +215,7 @@ public void onFail()
if(createNewOnFail)
{
Backpack backpack = new Backpack(player);
backpacks.put(player, backpack);
backpacks.put(player.getUniqueId(), backpack);
callback.onResult(backpack);
}
else
Expand All @@ -239,6 +238,7 @@ public void getBackpack(final OfflinePlayer player, final Callback<at.pcgamingfr

public void unloadBackpack(Backpack backpack)
{
plugin.getLogger().log(Level.INFO, "Unloading backpack of {} ({})", new Object[] {backpack.getOwner().getName(), backpack.getOwner().getUniqueId()});
if (forceSaveOnUnload)
{
backpack.forceSave();
Expand All @@ -247,25 +247,25 @@ public void unloadBackpack(Backpack backpack)
{
backpack.save();
}
backpacks.remove(backpack.getOwner());
backpacks.remove(backpack.getOwner().getUniqueId());
}

public void asyncLoadBackpack(final OfflinePlayer player)
{
if(player != null && backpacks.get(player) == null)
if(player != null && backpacks.get(player.getUniqueId()) == null)
{
loadBackpack(player, new Callback<Backpack>()
{
@Override
public void onResult(Backpack backpack)
{
backpacks.put(player, backpack);
backpacks.put(player.getUniqueId(), backpack);
}

@Override
public void onFail()
{
backpacks.put(player, new Backpack(player));
backpacks.put(player.getUniqueId(), new Backpack(player));
}
});
}
Expand Down

0 comments on commit 795cc63

Please sign in to comment.