diff --git a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/BankTerminalPeripheral.java b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/BankTerminalPeripheral.java index 8cb0bec..0c81e76 100644 --- a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/BankTerminalPeripheral.java +++ b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/BankTerminalPeripheral.java @@ -24,6 +24,7 @@ import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; import dev.ithundxr.createnumismatics.Numismatics; +import dev.ithundxr.createnumismatics.config.NumismaticsConfig; import dev.ithundxr.createnumismatics.content.backend.BankAccount; import dev.ithundxr.createnumismatics.content.backend.IDeductable; import dev.ithundxr.createnumismatics.content.backend.ReasonHolder; @@ -32,12 +33,55 @@ import net.minecraft.network.chat.Component; import org.jetbrains.annotations.Nullable; -import java.util.UUID; +import java.util.*; public enum BankTerminalPeripheral implements IPeripheral { INSTANCE ; + @LuaFunction + public final List getAccounts() throws LuaException { + List output = new ArrayList(); + for (UUID uuid : Numismatics.BANK.accounts.keySet()) { + output.add(uuid.toString()); + } + return output; + } + + @LuaFunction + public final String getAccountLabel(String accountID) throws LuaException { + UUID account$; + try { + account$ = UUID.fromString(accountID); + } catch (IllegalArgumentException e) { + throw new LuaException("Invalid UUID"); + } + + BankAccount bankAccount = Numismatics.BANK.getAccount(account$); + if (bankAccount == null) { + throw new LuaException("Account not found"); + } + + return bankAccount.getDisplayName().getString(); + } + + @LuaFunction + public final boolean isPlayerOwned(String accountID) throws LuaException { + UUID account$; + try { + account$ = UUID.fromString(accountID); + } catch (IllegalArgumentException e) { + throw new LuaException("Invalid UUID"); + } + + BankAccount bankAccount = Numismatics.BANK.getAccount(account$); + if (bankAccount == null) { + throw new LuaException("Account not found"); + } + + return (bankAccount.type == BankAccount.Type.PLAYER); + } + @LuaFunction public final int getBalance(String accountID) throws LuaException { UUID account$; @@ -55,6 +99,64 @@ public final int getBalance(String accountID) throws LuaException { return bankAccount.getBalance(); } + @LuaFunction + public final List getSubAccounts(String accountID) throws LuaException { + if (NumismaticsConfig.server().getSubAccountsCommand.get()) { + UUID account$; + try { + account$ = UUID.fromString(accountID); + } catch (IllegalArgumentException e) { + throw new LuaException("Invalid UUID"); + } + + BankAccount bankAccount = Numismatics.BANK.getAccount(account$); + if (bankAccount == null) { + throw new LuaException("Account not found"); + } + + List output = new ArrayList(); + bankAccount.getSubAccounts().forEach((temp) -> { + output.add(temp.getAuthorizationID().toString()); + }); + + if (output.isEmpty()) { + throw new LuaException("No sub accounts"); + } + return output; + } else { + throw new LuaException("Function disabled by config"); + } + } + + @LuaFunction + public final String getSubAccountLabel(String accountID, String authorizationID) throws LuaException { + UUID account$, authorization$; + try { + account$ = UUID.fromString(accountID); + authorization$ = UUID.fromString(authorizationID); + } catch (IllegalArgumentException e) { + throw new LuaException("Invalid UUID"); + } + + Authorization authorization = new Authorization.Anonymous(authorization$); + + BankAccount bankAccount = Numismatics.BANK.getAccount(account$); + if (bankAccount == null) { + throw new LuaException("Account not found"); + } + + ReasonHolder reasonHolder = new ReasonHolder(); + SubAccount subAccount = bankAccount.getSubAccount(authorization, reasonHolder); + + if (subAccount == null) { + Component errorMessage = reasonHolder.getMessageOrDefault(Components.translatable("error.numismatics.authorized_card.account_not_found")); + throw new LuaException(errorMessage.getString()); + } + + + return subAccount.getLabel(); + } + @LuaFunction public final int getMaxAvailableWithdrawal(String accountID, String authorizationID) throws LuaException { UUID account$, authorization$; diff --git a/common/src/main/java/dev/ithundxr/createnumismatics/config/CServer.java b/common/src/main/java/dev/ithundxr/createnumismatics/config/CServer.java index da6fb2c..67ad2b4 100644 --- a/common/src/main/java/dev/ithundxr/createnumismatics/config/CServer.java +++ b/common/src/main/java/dev/ithundxr/createnumismatics/config/CServer.java @@ -33,6 +33,10 @@ public class CServer extends ConfigBase { public final ConfigInt starterCogs = i(0, 0, "starter_cogs"); public final ConfigInt starterCrowns = i(0, 0, "starter_crowns"); public final ConfigInt starterSuns = i(0, 0, "starter_suns"); + + public final ConfigGroup computerCraft = group(0, "computerCraft", Comments.computerCraft); + + public final ConfigBool getSubAccountsCommand = b(false, "get_sub_accounts_command", Comments.getSubAccountsCommand); //public final ConfigGroup misc = group(0, "misc", Comments.misc); @@ -46,9 +50,13 @@ public String getName() { private static class Comments { static final String coins = "Coin settings"; - + static final String starterCurrency = "How much of this coin type should players receive in their bank account on first join";; - + + static final String computerCraft = "Settings relating to ComputerCraft compatibility"; + + static final String getSubAccountsCommand = "Enables the getSubAccounts function. Disabled by default due to security concerns."; + static final String misc = "Miscellaneous settings"; } } \ No newline at end of file