Skip to content

Commit

Permalink
ComputerCraft API peripheral for Bank Terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
techno-sam committed Jul 21, 2024
1 parent 05acc43 commit d751b6a
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals;

import com.simibubi.create.foundation.utility.Components;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dev.ithundxr.createnumismatics.Numismatics;
import dev.ithundxr.createnumismatics.content.backend.BankAccount;
import dev.ithundxr.createnumismatics.content.backend.IDeductable;
import dev.ithundxr.createnumismatics.content.backend.ReasonHolder;
import dev.ithundxr.createnumismatics.content.backend.sub_authorization.Authorization;
import dev.ithundxr.createnumismatics.content.backend.sub_authorization.SubAccount;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

public enum BankTerminalPeripheral implements IPeripheral {
INSTANCE
;

@LuaFunction
public final int getBalance(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.getBalance();
}

@LuaFunction
public final int getMaxAvailableWithdrawal(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());
}

IDeductable deductable = subAccount.getDeductor(authorization);

if (deductable == null) {
throw new LuaException("Deductor not found");
}

return deductable.getMaxWithdrawal();
}

@LuaFunction(mainThread = true)
public final void transfer(String fromAccountID, String fromAuthorizationID, String toAccountID, int amount) throws LuaException {
UUID from$, fromAuthorization$, to$;
try {
from$ = UUID.fromString(fromAccountID);
fromAuthorization$ = UUID.fromString(fromAuthorizationID);
to$ = UUID.fromString(toAccountID);
} catch (IllegalArgumentException e) {
throw new LuaException("Invalid UUID");
}

Authorization authorization = new Authorization.Anonymous(fromAuthorization$);

BankAccount account = Numismatics.BANK.getAccount(from$);
BankAccount toAccount = Numismatics.BANK.getAccount(to$);
if (account == null || toAccount == null) {
throw new LuaException("Account not found");
}

ReasonHolder reasonHolder = new ReasonHolder();
SubAccount subAccount = account.getSubAccount(authorization, reasonHolder);

if (subAccount == null) {
Component errorMessage = reasonHolder.getMessageOrDefault(Components.translatable("error.numismatics.authorized_card.account_not_found"));
throw new LuaException(errorMessage.getString());
}

IDeductable deductable = subAccount.getDeductor(authorization);

if (deductable == null) {
throw new LuaException("Deductor not found");
}

if (deductable.getMaxWithdrawal() < amount) {
throw new LuaException("Insufficient funds");
}

if (deductable.deduct(amount, reasonHolder)) {
toAccount.deposit(amount);
} else {
throw new LuaException(reasonHolder.getMessageOrDefault().getString());
}
}

@Override
public String getType() {
return "Numismatics_BankTerminal";
}

@Override
public void attach(IComputerAccess computer) {
IPeripheral.super.attach(computer);
}

@Override
public boolean equals(@Nullable IPeripheral other) {
return other == INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
package dev.ithundxr.createnumismatics.util;

import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.ithundxr.createnumismatics.Numismatics;
import dev.ithundxr.createnumismatics.multiloader.Env;
import net.minecraft.core.Registry;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
Expand All @@ -45,4 +48,12 @@ public static boolean testClientPlayerOrElse(Predicate<Player> predicate, boolea
public static void openScreen(ServerPlayer player, MenuProvider factory, Consumer<FriendlyByteBuf> extraDataWriter) {
throw new AssertionError();
}

public static <R> void runOnceRegistered(ResourceKey<? extends Registry<R>> registryType, Runnable callback) {
if (Numismatics.registrate().isRegistered(registryType)) {
callback.run();
} else {
Numismatics.registrate().addRegisterCallback(registryType, callback);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,71 @@

package dev.ithundxr.createnumismatics.compat.computercraft.fabric;

import com.google.common.collect.ImmutableMap;
import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import dan200.computercraft.api.detail.VanillaDetailRegistries;
import dan200.computercraft.api.peripheral.PeripheralLookup;
import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals.BankTerminalPeripheral;
import dev.ithundxr.createnumismatics.content.bank.AuthorizedCardItem;
import dev.ithundxr.createnumismatics.content.bank.CardItem;
import dev.ithundxr.createnumismatics.content.bank.IDCardItem;
import dev.ithundxr.createnumismatics.registry.NumismaticsBlocks;
import dev.ithundxr.createnumismatics.registry.NumismaticsTags;
import dev.ithundxr.createnumismatics.util.Utils;
import net.minecraft.core.registries.Registries;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.UUID;

public class ComputerCraftProxyImpl {
public static void registerWithDependency() {
/* Comment if computercraft.implementation is not in the source set */
ComputerCraftProxy.computerFactory = ComputerBehaviour::new;

PeripheralLookup.get().registerFallback((level, blockPos, blockState, blockEntity, direction) -> ComputerBehaviour.peripheralProvider(level, blockPos));

Utils.runOnceRegistered(Registries.BLOCK, () -> {
PeripheralLookup.get().registerForBlocks(
(world, pos, state, blockEntity, context) -> BankTerminalPeripheral.INSTANCE,
NumismaticsBlocks.BANK_TERMINAL.get()
);
});

VanillaDetailRegistries.ITEM_STACK.addProvider((detailMap, stack) -> {
Map<Object, @Nullable Object> cardDetails = null;
if (NumismaticsTags.AllItemTags.CARDS.matches(stack)) {
UUID accountID = CardItem.get(stack);
if (accountID != null) {
cardDetails = Map.of(
"AccountID", accountID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.AUTHORIZED_CARDS.matches(stack)) {
AuthorizedCardItem.AuthorizationPair authorizationPair = AuthorizedCardItem.get(stack);
if (authorizationPair != null) {
UUID accountID = authorizationPair.accountID();
UUID authorizationID = authorizationPair.authorizationID();
cardDetails = Map.of(
"AccountID", accountID.toString(),
"AuthorizationID", authorizationID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.ID_CARDS.matches(stack)) {
UUID id = IDCardItem.get(stack);
if (id != null) {
cardDetails = Map.of(
"ID", id.toString()
);
}
}

if (cardDetails != null)
detailMap.put("numismatics", ImmutableMap.of("card", cardDetails));
});
}

public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,72 @@

package dev.ithundxr.createnumismatics.compat.computercraft.forge;

import com.google.common.collect.ImmutableMap;
import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import dan200.computercraft.api.ForgeComputerCraftAPI;
import dan200.computercraft.api.detail.VanillaDetailRegistries;
import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals.BankTerminalPeripheral;
import dev.ithundxr.createnumismatics.content.bank.AuthorizedCardItem;
import dev.ithundxr.createnumismatics.content.bank.CardItem;
import dev.ithundxr.createnumismatics.content.bank.IDCardItem;
import dev.ithundxr.createnumismatics.registry.NumismaticsBlocks;
import dev.ithundxr.createnumismatics.registry.NumismaticsTags;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.UUID;

public class ComputerCraftProxyImpl {
public static void registerWithDependency() {
/* Comment if computercraft.implementation is not in the source set */
ComputerCraftProxy.computerFactory = ComputerBehaviour::new;

ForgeComputerCraftAPI.registerPeripheralProvider((level, pos, direction) -> {
BlockState state = level.getBlockState(pos);

if (NumismaticsBlocks.BANK_TERMINAL.has(state)) {
return LazyOptional.of(() -> BankTerminalPeripheral.INSTANCE);
}

return LazyOptional.empty();
});

VanillaDetailRegistries.ITEM_STACK.addProvider((detailMap, stack) -> {
Map<Object, @Nullable Object> cardDetails = null;
if (NumismaticsTags.AllItemTags.CARDS.matches(stack)) {
UUID accountID = CardItem.get(stack);
if (accountID != null) {
cardDetails = Map.of(
"AccountID", accountID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.AUTHORIZED_CARDS.matches(stack)) {
AuthorizedCardItem.AuthorizationPair authorizationPair = AuthorizedCardItem.get(stack);
if (authorizationPair != null) {
UUID accountID = authorizationPair.accountID();
UUID authorizationID = authorizationPair.authorizationID();
cardDetails = Map.of(
"AccountID", accountID.toString(),
"AuthorizationID", authorizationID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.ID_CARDS.matches(stack)) {
UUID id = IDCardItem.get(stack);
if (id != null) {
cardDetails = Map.of(
"ID", id.toString()
);
}
}

if (cardDetails != null)
detailMap.put("numismatics", ImmutableMap.of("card", cardDetails));
});
}
public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe) {
if (ComputerCraftProxy.computerFactory == null)
Expand Down

0 comments on commit d751b6a

Please sign in to comment.