Skip to content

Commit

Permalink
Adds TransientWalletOwnerManager
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoismysign committed Jun 24, 2024
1 parent 096e9bd commit 36ddf94
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 12 deletions.
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.25</version>
<version>1.698.26</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.25</version>
<version>1.698.26</version>
<packaging>pom</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,26 @@ public static <T extends WalletOwner> WalletOwnerManager<T> SIMPLE_WALLET_OWNER_
public static <T extends WalletOwner> WalletOwnerManager<T> WALLET_OWNER_MANAGER(ManagerDirector managerDirector,
Function<BlobCrudable, BlobCrudable> newBorn,
Function<BlobCrudable, T> walletOwner,
String crudableName, boolean logActivity,
String crudableName,
boolean logActivity,
@Nullable Function<T, Event> joinEvent,
@Nullable Function<T, Event> quitEvent,
@NotNull EventPriority joinPriority,
@NotNull EventPriority quitPriority) {
return new <T>WalletOwnerManager<T>(managerDirector, newBorn, walletOwner, crudableName, logActivity, joinEvent, quitEvent,
joinPriority, quitPriority);
}

public static <T extends WalletOwner> WalletOwnerManager<T> TRANSIENT_WALLET_OWNER_MANAGER(ManagerDirector managerDirector,
Function<BlobCrudable, BlobCrudable> newBorn,
Function<BlobCrudable, T> walletOwner,
String crudableName,
boolean logActivity,
@Nullable Function<T, Event> joinEvent,
@Nullable Function<T, Event> quitEvent,
@NotNull EventPriority joinPriority,
@NotNull EventPriority quitPriority) {
return new <T>TransientWalletOwnerManager<T>(managerDirector, newBorn, walletOwner, crudableName, logActivity, joinEvent, quitEvent,
joinPriority, quitPriority);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package us.mytheria.bloblib.entities.currency;

import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.entities.BlobCrudable;
import us.mytheria.bloblib.managers.ManagerDirector;

import java.util.function.Function;

public class TransientWalletOwnerManager<T extends WalletOwner> extends WalletOwnerManager<T> {
protected TransientWalletOwnerManager(ManagerDirector managerDirector,
Function<BlobCrudable, BlobCrudable> newBorn,
Function<BlobCrudable, T> generator,
String crudableName,
boolean logActivity,
@Nullable Function<T, Event> joinEvent,
@Nullable Function<T, Event> quitEvent,
@NotNull EventPriority joinPriority,
@NotNull EventPriority quitPriority) {
super(managerDirector, newBorn, generator, crudableName, logActivity, joinEvent, quitEvent,
joinPriority, quitPriority);
}

@Override
public BlobCrudable read(String key) {
return new BlobCrudable(key);
}

@Override
public void update(BlobCrudable crudable) {
}

@Override
public boolean exists(String key) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ public void onJoin(PlayerJoinEvent e) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
if (player == null || !player.isOnline())
return;
BlobCrudable crudable = crudManager.read(uuid.toString());
BlobCrudable crudable = read(uuid.toString());
Bukkit.getScheduler().runTask(plugin, () -> {
if (player == null || !player.isOnline())
return;
T applied = generator.apply(crudable);
BlobCrudable serialized = applied.serializeAllAttributes();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(),
() -> crudManager.update(serialized));
() -> update(serialized));
walletOwners.put(uuid, applied);
if (joinEvent == null)
return;
Expand All @@ -124,7 +124,7 @@ public void run() {
}
BlobCrudable serialized = applied.serializeAllAttributes();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(),
() -> crudManager.update(serialized));
() -> update(serialized));
}
}.runTaskTimer(getPlugin(), 20 * 60 * 5,
20 * 60 * 5));
Expand All @@ -145,7 +145,7 @@ public void onQuit(PlayerQuitEvent e) {
autoSave.remove(uuid);
BlobCrudable crudable = walletOwner.serializeAllAttributes();
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
crudManager.update(crudable);
update(crudable);
removeObject(uuid);
saving.remove(uuid);
});
Expand Down Expand Up @@ -187,7 +187,7 @@ public void ifIsOnlineThenUpdateElse(UUID uuid, Consumer<T> consumer,
if (isPresent) {
T walletOwner = optional.get();
consumer.accept(walletOwner);
crudManager.update(walletOwner.serializeAllAttributes());
update(walletOwner.serializeAllAttributes());
} else {
runnable.run();
}
Expand All @@ -199,20 +199,24 @@ public void ifIsOnlineThenUpdate(UUID uuid, Consumer<T> consumer) {
}

private void saveAll() {
walletOwners.values().forEach(walletOwner -> crudManager.update(walletOwner.serializeAllAttributes()));
walletOwners.values().forEach(walletOwner -> update(walletOwner.serializeAllAttributes()));
}

public CompletableFuture<T> readAsynchronously(String key) {
CompletableFuture<T> future = new CompletableFuture<>();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () ->
future.complete(generator.apply(crudManager.read(key))));
future.complete(generator.apply(read(key))));
return future;
}

public void readThenUpdate(String key, Consumer<T> consumer) {
T walletOwner = generator.apply(crudManager.read(key));
T walletOwner = generator.apply(read(key));
consumer.accept(walletOwner);
crudManager.update(walletOwner.serializeAllAttributes());
update(walletOwner.serializeAllAttributes());
}

public BlobCrudable read(String key) {
return crudManager.read(key);
}

public void update(BlobCrudable crudable) {
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,43 @@ public <T extends WalletOwner> void addWalletOwnerManager(String key,
joinPriority, quitPriority));
}

/**
* Adds a wallet owner manager to the director.
* This wallet owner manager doesn't save nor store data.
*
* @param key The key of the manager
* @param newBorn A function that by passing a UUID, it will fill a BlobCrudable
* with default key-value pairs.
* This is used to create new/fresh WalletOwners.
* @param walletOwner A function that by passing a BlobCrudable, it will return a WalletOwner.
* WalletOwners use this to store their data inside databases.
* @param crudableName The name of the BlobCrudable. This will be used for
* as the column name in the database.
* @param logActivity Whether to log activity in the console.
* @param joinEvent A function that by passing a WalletOwner, it will return a join event.
* It's called SYNCHRONOUSLY.
* It's called when a player joins the server.
* @param quitEvent A function that by passing a WalletOwner, it will return a quit event.
* It's called SYNCHRONOUSLY.
* It's called when a player quits/leaves the server.
* @param joinPriority The priority of the join event.
* @param quitPriority The priority of the quit event.
* @param <T> The type of WalletOwner.
*/
public <T extends WalletOwner> void addTransientWalletOwnerManager(String key,
Function<BlobCrudable, BlobCrudable> newBorn,
Function<BlobCrudable, T> walletOwner,
String crudableName, boolean logActivity,
@Nullable Function<T, Event> joinEvent,
@Nullable Function<T, Event> quitEvent,
@NotNull EventPriority joinPriority,
@NotNull EventPriority quitPriority) {
addManager(key,
EconomyFactory.TRANSIENT_WALLET_OWNER_MANAGER(this,
newBorn, walletOwner, crudableName, logActivity, joinEvent, quitEvent,
joinPriority, quitPriority));
}

/**
* Adds a wallet owner manager to the director.
* Uses NORMAL priority for join and quit listeners.
Expand Down

0 comments on commit 36ddf94

Please sign in to comment.