From 2f18e4d8efdb17604fb29080286c8dbedcb07ca3 Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 20 May 2023 04:54:55 +0100 Subject: [PATCH] Add economic entity interfaces I decided to add these interfaces, instead of just building upon the User & UserData interface, as support for commerce between clans & users might be something that we want to add in the future in Clans. --- .../datura/user/SimpleUserData.java | 32 +++++++++++++++++-- .../totalfreedom/economy/EconomicEntity.java | 8 +++++ .../economy/EconomicEntityData.java | 12 +++++++ .../main/java/me/totalfreedom/user/User.java | 17 +++++++++- .../java/me/totalfreedom/user/UserData.java | 3 +- 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 Patchwork/src/main/java/me/totalfreedom/economy/EconomicEntity.java create mode 100644 Patchwork/src/main/java/me/totalfreedom/economy/EconomicEntityData.java diff --git a/Datura/src/main/java/me/totalfreedom/datura/user/SimpleUserData.java b/Datura/src/main/java/me/totalfreedom/datura/user/SimpleUserData.java index cb6db2a..8b4fbce 100644 --- a/Datura/src/main/java/me/totalfreedom/datura/user/SimpleUserData.java +++ b/Datura/src/main/java/me/totalfreedom/datura/user/SimpleUserData.java @@ -26,6 +26,7 @@ public class SimpleUserData implements UserData private boolean frozen; private boolean canInteract; private boolean caged; + private long balance; public SimpleUserData(final Player player) { @@ -42,7 +43,8 @@ public class SimpleUserData implements UserData final long playtime, final boolean frozen, final boolean canInteract, - final boolean caged) + final boolean caged, + final long balance) { this.uuid = uuid; this.username = username; @@ -52,6 +54,7 @@ public class SimpleUserData implements UserData this.frozen = frozen; this.canInteract = canInteract; this.caged = caged; + this.balance = balance; } public static SimpleUserData fromSQL(SQL sql, String uuid) @@ -82,7 +85,8 @@ public class SimpleUserData implements UserData boolean frozen = result.getBoolean("frozen"); boolean canInteract = result.getBoolean("canInteract"); boolean caged = result.getBoolean("caged"); - return new SimpleUserData(u, username, user, group, playtime, frozen, canInteract, caged); + long balance = result.getLong("balance"); + return new SimpleUserData(u, username, user, group, playtime, frozen, canInteract, caged, balance); } } catch (SQLException ex) { @@ -197,4 +201,28 @@ public class SimpleUserData implements UserData { this.caged = caged; } + + @Override + public long getBalance() + { + return balance; + } + + @Override + public void addToBalance(long amount) + { + this.balance += amount; + } + + @Override + public void removeFromBalance(long amount) + { + this.balance -= amount; + } + + @Override + public void setBalance(long newBalance) + { + this.balance = newBalance; + } } diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/EconomicEntity.java b/Patchwork/src/main/java/me/totalfreedom/economy/EconomicEntity.java new file mode 100644 index 0000000..1855d1e --- /dev/null +++ b/Patchwork/src/main/java/me/totalfreedom/economy/EconomicEntity.java @@ -0,0 +1,8 @@ +package me.totalfreedom.economy; + +public interface EconomicEntity +{ + EconomicEntityData getEconomicData(); + + String getName(); +} diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/EconomicEntityData.java b/Patchwork/src/main/java/me/totalfreedom/economy/EconomicEntityData.java new file mode 100644 index 0000000..9546489 --- /dev/null +++ b/Patchwork/src/main/java/me/totalfreedom/economy/EconomicEntityData.java @@ -0,0 +1,12 @@ +package me.totalfreedom.economy; + +public interface EconomicEntityData +{ + long getBalance(); + + void addToBalance(final long amount); + + void removeFromBalance(final long amount); + + void setBalance(final long newBalance); +} diff --git a/Patchwork/src/main/java/me/totalfreedom/user/User.java b/Patchwork/src/main/java/me/totalfreedom/user/User.java index 222e2a9..61e8ca3 100644 --- a/Patchwork/src/main/java/me/totalfreedom/user/User.java +++ b/Patchwork/src/main/java/me/totalfreedom/user/User.java @@ -1,10 +1,25 @@ package me.totalfreedom.user; +import me.totalfreedom.economy.EconomicEntity; +import me.totalfreedom.economy.EconomicEntityData; import me.totalfreedom.security.PermissionHolder; import net.kyori.adventure.text.Component; -public interface User extends PermissionHolder +public interface User extends PermissionHolder, EconomicEntity { + // Implement a few EconomicEntity methods in the User interface + @Override + default String getName() + { + return getUserData().getUsername(); + } + + @Override + default EconomicEntityData getEconomicData() + { + return getUserData(); + } + UserData getUserData(); Component getDisplayName(); diff --git a/Patchwork/src/main/java/me/totalfreedom/user/UserData.java b/Patchwork/src/main/java/me/totalfreedom/user/UserData.java index ac2e277..f7ac1d1 100644 --- a/Patchwork/src/main/java/me/totalfreedom/user/UserData.java +++ b/Patchwork/src/main/java/me/totalfreedom/user/UserData.java @@ -1,12 +1,13 @@ package me.totalfreedom.user; +import me.totalfreedom.economy.EconomicEntityData; import me.totalfreedom.security.Group; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.UUID; -public interface UserData +public interface UserData extends EconomicEntityData { @NotNull UUID getUniqueId();