Skip to content

Commit

Permalink
Merge pull request #96 from banna12345bob/1.20.1/dev
Browse files Browse the repository at this point in the history
[ComputerCraft] Add new lua functions to Bank Terminal Peripheral
  • Loading branch information
techno-sam authored Aug 6, 2024
2 parents 153032e + 33cae4d commit 4d5f452
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> getAccounts() throws LuaException {
List<String> output = new ArrayList<String>();
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$;
Expand All @@ -55,6 +99,64 @@ public final int getBalance(String accountID) throws LuaException {
return bankAccount.getBalance();
}

@LuaFunction
public final List<String> 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<String> output = new ArrayList<String>();
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$;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";
}
}

0 comments on commit 4d5f452

Please sign in to comment.