Skip to content

Commit

Permalink
Adds auto saving and fixes an old bug if server crashes during first …
Browse files Browse the repository at this point in the history
…join of a player
  • Loading branch information
anjoismysign committed Mar 2, 2024
1 parent 27e8a99 commit e12cb63
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;

public class ProxiedSharedSerializableManager<T extends SharedSerializable<?>>
extends Manager implements Listener, PluginMessageListener {
protected final HashMap<String, T> cache;
protected final Map<String, T> cache;
protected BlobCrudManager<BlobCrudable> crudManager;
private final BlobPlugin plugin;
private final String tag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.managers.BlobPlugin;
Expand All @@ -25,8 +27,9 @@
import java.util.function.Function;

public class BlobSerializableManager<T extends BlobSerializable> extends Manager implements BlobSerializableHandler {
protected final HashMap<UUID, T> serializables;
private final HashSet<UUID> saving;
protected final Map<UUID, T> serializables;
private final Map<UUID, BukkitTask> autoSave;
private final Set<UUID> saving;
protected BlobCrudManager<BlobCrudable> crudManager;
private final BlobPlugin plugin;
private final Function<BlobCrudable, T> generator;
Expand Down Expand Up @@ -56,6 +59,7 @@ protected BlobSerializableManager(ManagerDirector managerDirector, Function<Blob
registerJoinListener(pluginManager, joinPriority);
registerQuitListener(pluginManager, quitPriority);
serializables = new HashMap<>();
autoSave = new HashMap<>();
this.generator = generator;
this.joinEvent = joinEvent;
this.quitEvent = quitEvent;
Expand Down Expand Up @@ -91,10 +95,26 @@ public void onJoin(PlayerJoinEvent e) {
if (player == null || !player.isOnline())
return;
T applied = generator.apply(crudable);
BlobCrudable serialized = applied.serializeAllAttributes();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(),
() -> crudManager.update(serialized));
serializables.put(uuid, applied);
if (joinEvent == null)
return;
Bukkit.getPluginManager().callEvent(joinEvent.apply(applied));
autoSave.put(uuid, new BukkitRunnable() {
@Override
public void run() {
if (player == null || !player.isOnline()) {
cancel();
return;
}
BlobCrudable serialized = applied.serializeAllAttributes();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(),
() -> crudManager.update(serialized));
}
}.runTaskTimer(getPlugin(), 20 * 60 * 5,
20 * 60 * 5));
});
});
}
Expand All @@ -109,6 +129,7 @@ public void onQuit(PlayerQuitEvent e) {
if (quitEvent != null)
Bukkit.getPluginManager().callEvent(quitEvent.apply(serializable));
saving.add(uuid);
autoSave.remove(uuid);
BlobCrudable crudable = serializable.serializeAllAttributes();
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
crudManager.update(crudable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.entities.BlobCrudable;
Expand All @@ -30,8 +32,9 @@
import java.util.stream.Collectors;

public class WalletOwnerManager<T extends WalletOwner> extends Manager implements BlobSerializableHandler {
protected final HashMap<UUID, T> walletOwners;
private final HashSet<UUID> saving;
protected final Map<UUID, T> walletOwners;
private final Map<UUID, BukkitTask> autoSave;
private final Set<UUID> saving;
protected BlobCrudManager<BlobCrudable> crudManager;
private final BlobPlugin plugin;
private final Function<BlobCrudable, T> generator;
Expand All @@ -42,7 +45,7 @@ public class WalletOwnerManager<T extends WalletOwner> extends Manager implement
protected ObjectDirector<Currency> currencyDirector;
@Nullable
private EconomyPHExpansion<T> economyPHExpansion;
private HashMap<String, CurrencyEconomy> implementations;
private Map<String, CurrencyEconomy> implementations;

protected WalletOwnerManager(ManagerDirector managerDirector, Function<BlobCrudable, BlobCrudable> newBorn,
Function<BlobCrudable, T> generator,
Expand All @@ -67,6 +70,7 @@ protected WalletOwnerManager(ManagerDirector managerDirector, Function<BlobCruda
registerJoinListener(pluginManager, joinPriority);
registerQuitListener(pluginManager, quitPriority);
walletOwners = new HashMap<>();
autoSave = new HashMap<>();
this.generator = generator;
this.joinEvent = joinEvent;
this.quitEvent = quitEvent;
Expand Down Expand Up @@ -109,10 +113,26 @@ public void onJoin(PlayerJoinEvent e) {
if (player == null || !player.isOnline())
return;
T applied = generator.apply(crudable);
BlobCrudable serialized = applied.serializeAllAttributes();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(),
() -> crudManager.update(serialized));
walletOwners.put(uuid, applied);
if (joinEvent == null)
return;
Bukkit.getPluginManager().callEvent(joinEvent.apply(applied));
autoSave.put(uuid, new BukkitRunnable() {
@Override
public void run() {
if (player == null || !player.isOnline()) {
cancel();
return;
}
BlobCrudable serialized = applied.serializeAllAttributes();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(),
() -> crudManager.update(serialized));
}
}.runTaskTimer(getPlugin(), 20 * 60 * 5,
20 * 60 * 5));
});
}

Expand All @@ -126,6 +146,7 @@ public void onQuit(PlayerQuitEvent e) {
if (quitEvent != null)
Bukkit.getPluginManager().callEvent(quitEvent.apply(walletOwner));
saving.add(uuid);
autoSave.remove(uuid);
BlobCrudable crudable = walletOwner.serializeAllAttributes();
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
crudManager.update(crudable);
Expand Down

0 comments on commit e12cb63

Please sign in to comment.