Migrates the entire package nomenclature to be more direct and straightforward. (#17)

Signed-off-by: Paul Reilly <pawereus@gmail.com>
This commit is contained in:
Paldiu
2023-08-01 22:34:18 -05:00
committed by GitHub
parent e1a6b5e587
commit 21463c50fe
146 changed files with 595 additions and 608 deletions
@@ -0,0 +1,128 @@
package fns.datura.user;
import fns.patchwork.economy.EconomicEntity;
import fns.patchwork.economy.EconomicEntityData;
/**
* Represents the server's economy holder.
* <br>
* <br>
* This is effectively a Bank object which is meant to represent the server itself, which can store a balance and
* perform transactions with other EconomicEntity objects.
* <br>
* <br>
* The server is initially given a maximum balance of {@link Long#MAX_VALUE}, though this can be changed using the
* constructor {@link #ServerEconomyHolder(String, long)}. The value that this bank object holds is persistent, which
* means that the total economic resources available are of limited supply.
* <br>
* <br>
* Please be aware, if the server's economy falls below 0, it will have drastic consequences.
*/
public class ServerEconomyHolder implements EconomicEntity, EconomicEntityData
{
private final String name;
private long balance;
/**
* Constructs a new ServerEconomyHolder with the specified name and a balance of {@link Long#MAX_VALUE}.
*
* @param name The name of this server economy holder.
*/
public ServerEconomyHolder(final String name)
{
this.name = name;
this.balance = Long.MAX_VALUE;
}
/**
* Constructs a new ServerEconomyHolder with the specified name and balance.
*
* @param name The name of this server economy holder.
* @param balance The balance of this server economy holder.
*/
public ServerEconomyHolder(final String name, final long balance)
{
this.name = name;
this.balance = balance;
}
/**
* This method will return this object, as it is both the EconomicEntity and the EconomicEntityData. This is due to
* the fact that the server should only ever have one singular concrete representation of it's economic entity and
* the respective data.
*
* @return this object.
*/
@Override
public EconomicEntityData getEconomicData()
{
return this;
}
/**
* @return The name of this server economy holder.
*/
@Override
public String getName()
{
return name;
}
/**
* This method will always return false, as the server should not ever be prevented from performing transactions.
*
* @return false
*/
@Override
public boolean areTransactionsFrozen()
{
return false;
}
/**
* @return The server's current available balance.
*/
@Override
public long getBalance()
{
return balance;
}
/**
* Sets the server's balance to the specified value.
*
* @param newBalance The new balance to set.
*/
@Override
public void setBalance(final long newBalance)
{
balance = newBalance;
}
/**
* Adds the specified amount to the server's balance. This method mutates the balance and returns the new balance.
*
* @param amount The amount to add.
* @return The new balance.
*/
@Override
public long addToBalance(final long amount)
{
balance += amount;
return balance;
}
/**
* Removes the specified amount from the server's balance. This method mutates the balance and returns the new
* balance.
*
* @param amount The amount to remove.
* @return The new balance.
*/
@Override
public long removeFromBalance(final long amount)
{
balance -= amount;
return balance;
}
}
@@ -0,0 +1,244 @@
package fns.datura.user;
import fns.datura.event.UserDataUpdateEvent;
import fns.datura.perms.FreedomUser;
import fns.patchwork.base.Patchwork;
import fns.patchwork.display.adminchat.AdminChatFormat;
import fns.patchwork.security.Group;
import fns.patchwork.sql.SQL;
import fns.patchwork.user.User;
import fns.patchwork.user.UserData;
import fns.patchwork.utils.logging.FreedomLogger;
import java.sql.SQLException;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class SimpleUserData implements UserData
{
private final UUID uuid;
private final String username;
private final User user;
private final UserDataUpdateEvent event = new UserDataUpdateEvent(this);
private Group group;
private long playtime;
private boolean canInteract;
private AtomicLong balance;
private boolean transactionsFrozen;
private boolean hasCustomACFormat = false;
private String customACFormat;
public SimpleUserData(final Player player)
{
this.uuid = player.getUniqueId();
this.username = player.getName();
this.user = new FreedomUser(player);
Patchwork.getInstance()
.getEventBus()
.addEvent(event);
}
private SimpleUserData(
final UUID uuid,
final String username,
final User user,
final Group group,
final long playtime,
final boolean canInteract,
final long balance,
final boolean transactionsFrozen)
{
this.uuid = uuid;
this.username = username;
this.user = user;
this.group = group;
this.playtime = playtime;
this.canInteract = canInteract;
this.balance = new AtomicLong(balance);
this.transactionsFrozen = transactionsFrozen;
this.customACFormat = AdminChatFormat.DEFAULT.serialize();
}
public static SimpleUserData fromSQL(final SQL sql, final String uuid)
{
return sql.executeQuery("SELECT * FROM users WHERE UUID = ?", uuid)
.thenApplyAsync(result ->
{
try
{
if (result.next())
{
final String g = result.getString("group");
final UUID u = UUID.fromString(uuid);
final String username = result.getString("username");
final Player player = Bukkit.getPlayer(u);
if (player == null)
throw new IllegalStateException("Player should be online but they are not!");
final User user = new FreedomUser(player);
final Group group = Patchwork.getInstance()
.getRegistrations()
.getGroupRegistry()
.getGroup(g);
final long playtime = result.getLong("playtime");
final boolean canInteract = result.getBoolean("canInteract");
final long balance = result.getLong("balance");
final boolean transactionsFrozen = result.getBoolean("transactionsFrozen");
return new SimpleUserData(u, username, user, group, playtime,
canInteract, balance, transactionsFrozen);
}
} catch (SQLException ex)
{
final String sb = "An error occurred while trying to retrieve user data for" +
" UUID " +
uuid +
" from the database." +
"\nCaused by: " +
ExceptionUtils.getRootCauseMessage(ex) +
"\nStack trace: " +
ExceptionUtils.getStackTrace(ex);
FreedomLogger.getLogger("Datura")
.error(sb);
}
final Player player = Bukkit.getPlayer(UUID.fromString(uuid));
if (player == null) throw new IllegalStateException("Player should be online but they are not!");
return new SimpleUserData(player);
}, Patchwork.getInstance()
.getExecutor()
.getAsync())
.join();
}
@Override
public @NotNull UUID getUniqueId()
{
return uuid;
}
@Override
public String getUsername()
{
return username;
}
@Override
public User getUser()
{
return user;
}
@Override
public @Nullable Group getGroup()
{
return group;
}
@Override
public void setGroup(@Nullable final Group group)
{
event.ping();
this.group = group;
}
@Override
public long getPlaytime()
{
return playtime;
}
@Override
public void setPlaytime(final long playtime)
{
event.ping();
this.playtime = playtime;
}
@Override
public void addPlaytime(final long playtime)
{
event.ping();
this.playtime += playtime;
}
@Override
public void resetPlaytime()
{
event.ping();
this.playtime = 0L;
}
@Override
public boolean canInteract()
{
return canInteract;
}
@Override
public void setInteractionState(final boolean canInteract)
{
event.ping();
this.canInteract = canInteract;
}
@Override
public boolean areTransactionsFrozen()
{
return transactionsFrozen;
}
@Override
public long getBalance()
{
return balance.get();
}
@Override
public void setBalance(final long newBalance)
{
balance.set(newBalance);
}
@Override
public long addToBalance(final long amount)
{
return balance.addAndGet(amount);
}
@Override
public long removeFromBalance(final long amount)
{
return balance.addAndGet(-amount);
}
@Override
public boolean hasCustomACFormat()
{
return hasCustomACFormat;
}
@Override
public AdminChatFormat getCustomACFormat()
{
return AdminChatFormat.deserialize(customACFormat);
}
@Override
public void setCustomACFormat(final String format)
{
this.hasCustomACFormat = format.equals(AdminChatFormat.DEFAULT.serialize());
this.customACFormat = format;
}
}