Abstract Class: Balance.java - Hempfest/Hemponomics GitHub Wiki
Anything that maintains a persistent balance in Hemponomics subclasses the Balance
abstract class found in the com.youtube.hempfest.economy.construct.account
.
Constructor
Balance(EconomyEntity)
Balance has one constructor that accepts an EconomyEntity. Commonly this is handled in subclasses and passed into the call to super().
For instance:
public abstract class PlayerWallet extends Wallet { // Wallet subclasses Balance
protected PlayerWallet(OfflinePlayer player) {
super(new PlayerEntity(player));
}
// continued
Here we create a new PlayerEntity
object which allows us to process the player into a String id()
that can be used to greatly simplify data storage.
Let's take a closer look at PlayerEntity#id()
:
/**
* Designed to represent players, even persistently. Uses UniqueId internally.
*/
public final class PlayerEntity extends PlayerEconomyEntityBase {
public PlayerEntity(@NotNull OfflinePlayer offlinePlayer) {
super(offlinePlayer);
}
/**
* Represent this player by their UniqueId
* @return String following "p_uid=%uuid%" format
*/
@Override
public @NotNull String id() {
return "p_uid=" + uid;
}
}
For any PlayerEntity
created from offlinePlayer
with getUniqueId uuid
, id() will always return a String of format "p_uid=uuid
"
Finally. We don't have to manually reconstruct/serialize UUIDs to work as String keys. We don't have to worry about when and how to store whether this UUID identifies a player or an NPC. Wallets can be implemented as simply as defining a Map<String, BigDecimal>
-type structure. We immediately process and release the original player object after obtaining its uid as the id() function provides a matching view every time.
If you're interested in how else the id() system can be used, take a look at TemporaryPlayerEntity
(uses player's name which is only guaranteed to be unique for the current session) and NpcEntity
(Uses plugin reference + String key to identify NPCs, namespaced per-plugin).
Methods to override
// Strings `world` correspond to a specific world name
public abstract void setBalance(BigDecimal amount); // Define setBalance logic
public abstract void setBalance(BigDecimal amount, String world);
public abstract boolean exists(); // Return true if this Balance exists
public abstract boolean exists(String world); // Perform this operation in the context of world=world
@Nullable
public abstract BigDecimal getBalance(); // Return balance or null if !exists()
@Nullable
public abstract BigDecimal getBalance(String world);
public abstract boolean has(BigDecimal amount); // Define logic to test balance for an amount
public abstract boolean has(BigDecimal amount, String world);
public abstract EconomyAction deposit(BigDecimal amount); // Add `amount` to balance
public abstract EconomyAction deposit(BigDecimal amount, String world);
public abstract EconomyAction withdraw(BigDecimal amount); // Remove `amount` from balance
public abstract EconomyAction withdraw(BigDecimal amount, String world);