Skip to content

Commit

Permalink
Computercraft compat (#81)
Browse files Browse the repository at this point in the history
Co-authored-by: Szedann <[email protected]>
  • Loading branch information
IThundxr and Szedann authored Jun 5, 2024
1 parent 808ea76 commit ac4defb
Show file tree
Hide file tree
Showing 19 changed files with 533 additions and 4 deletions.
8 changes: 8 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ repositories {
maven("https://maven.siphalor.de/") { // Amecs API (required by Carry On)
name = "Siphalor's Maven"
}
maven("https://squiddev.cc/maven/") {// CC Tweaked
content {
includeGroup("cc.tweaked")
}
}
}

dependencies {
Expand All @@ -45,6 +50,9 @@ dependencies {
modCompileOnly("tschipp.carryon:carryon-fabric-${"minecraft_version"()}:${"carryon_fabric_version"()}")

implementation(annotationProcessor("io.github.llamalad7:mixinextras-common:${"mixin_extras_version"()}")!!)


compileOnly("cc.tweaked:cc-tweaked-${"minecraft_version"()}-common-api:${"cc_version"()}")
}

tasks.processResources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package dev.ithundxr.createnumismatics;

import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.registry.*;

public class ModSetup {
Expand All @@ -28,5 +29,7 @@ public static void register() {
NumismaticsBlocks.register();
NumismaticsMenuTypes.register();
NumismaticsTags.register();

ComputerCraftProxy.register();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
*/
public enum Mods {
CARRYON("carryon"),
SODIUM("sodium")
SODIUM("sodium"),
COMPUTERCRAFT("computercraft")
;

public final boolean isLoaded;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.compat.computercraft;

import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.compat.computercraft.FallbackComputerBehaviour;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.ithundxr.createnumismatics.compat.Mods;

import java.util.function.Function;

public class ComputerCraftProxy {
public static void register() {
fallbackFactory = FallbackComputerBehaviour::new;
Mods.COMPUTERCRAFT.executeIfInstalled(() -> ComputerCraftProxy::registerWithDependency);
}

@ExpectPlatform
static void registerWithDependency() {
throw new AssertionError();
}

public static Function<SmartBlockEntity, ? extends AbstractComputerBehaviour> fallbackFactory;
public static Function<SmartBlockEntity, ? extends AbstractComputerBehaviour> computerFactory;

@ExpectPlatform
public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe) {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

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

import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour;
import com.tterrag.registrate.util.nullness.NonNullSupplier;
import dan200.computercraft.api.peripheral.IPeripheral;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals.BrassDepositorPeripheral;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals.VendorPeripheral;
import dev.ithundxr.createnumismatics.content.depositor.BrassDepositorBlockEntity;
import dev.ithundxr.createnumismatics.content.vendor.VendorBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;

public class ComputerBehaviour extends AbstractComputerBehaviour {
NonNullSupplier<IPeripheral> peripheralSupplier;

public static IPeripheral peripheralProvider(Level level, BlockPos blockPos) {
AbstractComputerBehaviour behavior = BlockEntityBehaviour.get(level, blockPos, AbstractComputerBehaviour.TYPE);
if (behavior instanceof ComputerBehaviour real)
return real.getPeripheral();
return null;
}

public ComputerBehaviour(SmartBlockEntity te) {
super(te);
this.peripheralSupplier = getPeripheralFor(te);
}

public static NonNullSupplier<IPeripheral> getPeripheralFor(SmartBlockEntity be) {
if (be instanceof BrassDepositorBlockEntity scbe)
return () -> new BrassDepositorPeripheral(scbe);
if (be instanceof VendorBlockEntity scbe)
return () -> new VendorPeripheral(scbe);

throw new IllegalArgumentException("No peripheral available for " + be.getType());
}

@Override
public <T> T getPeripheral() {
//noinspection unchecked
return (T) peripheralSupplier.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU 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.compat.computercraft.implementation.peripherals.SyncedPeripheral;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dev.ithundxr.createnumismatics.content.backend.Coin;
import dev.ithundxr.createnumismatics.content.depositor.BrassDepositorBlockEntity;

import java.util.List;
import java.util.Map;

import static dev.ithundxr.createnumismatics.content.backend.Coin.getCoinFromName;
import static dev.ithundxr.createnumismatics.content.backend.Coin.getCoinsFromSpurAmount;

public class BrassDepositorPeripheral extends SyncedPeripheral<BrassDepositorBlockEntity> {
public BrassDepositorPeripheral(BrassDepositorBlockEntity blockEntity) {
super(blockEntity);
}

@LuaFunction(mainThread = true)
public final void setCoinAmount(String coinName, int amount) throws LuaException {
Coin coin = getCoinFromName(coinName);
if(coin == null) throw new LuaException("incorrect coin name");
blockEntity.setPrice(coin, amount);
blockEntity.notifyUpdate();
}

@LuaFunction(mainThread = true)
public final void setTotalPrice(int spurAmount) {
List<Map.Entry<Coin, Integer>> coins = getCoinsFromSpurAmount(spurAmount);
for (Map.Entry<Coin, Integer> coin : coins) {
blockEntity.setPrice(coin.getKey(), coin.getValue());
}
blockEntity.notifyUpdate();
}

@LuaFunction
public final int getTotalPrice() {
return blockEntity.getTotalPrice();
}

@LuaFunction
public final int getPrice(String coinName) throws LuaException {
Coin coin = getCoinFromName(coinName);
if(coin == null) throw new LuaException("incorrect coin name");
return blockEntity.getPrice(coin);
}

@Override
public String getType() {
return "Numismatics_Depositor";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU 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.compat.computercraft.implementation.peripherals.SyncedPeripheral;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dev.ithundxr.createnumismatics.content.backend.Coin;
import dev.ithundxr.createnumismatics.content.vendor.VendorBlockEntity;

import java.util.List;
import java.util.Map;

import static dev.ithundxr.createnumismatics.content.backend.Coin.getCoinFromName;
import static dev.ithundxr.createnumismatics.content.backend.Coin.getCoinsFromSpurAmount;

public class VendorPeripheral extends SyncedPeripheral<VendorBlockEntity> {
public VendorPeripheral(VendorBlockEntity blockEntity) {
super(blockEntity);
}

@LuaFunction(mainThread = true)
public final void setCoinAmount(String coinName, int amount) throws LuaException {
Coin coin = getCoinFromName(coinName);
if(coin == null) throw new LuaException("incorrect coin name");
blockEntity.setPrice(coin, amount);
blockEntity.notifyUpdate();
}

@LuaFunction(mainThread = true)
public final void setTotalPrice(int spurAmount) {
List<Map.Entry<Coin, Integer>> coins = getCoinsFromSpurAmount(spurAmount);
for (Map.Entry<Coin, Integer> coin : coins) {
blockEntity.setPrice(coin.getKey(), coin.getValue());
}
blockEntity.notifyUpdate();
}

@LuaFunction
public final int getTotalPrice() {
return blockEntity.getTotalPrice();
}

@LuaFunction
public final int getPrice(String coinName) throws LuaException {
Coin coin = getCoinFromName(coinName);
if(coin == null) throw new LuaException("incorrect coin name");
return blockEntity.getPrice(coin);
}

@Override
public String getType() {
return "Numismatics_Vendor";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
import com.simibubi.create.foundation.gui.AllIcons;
import com.simibubi.create.foundation.utility.Components;
import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.Pair;
import dev.ithundxr.createnumismatics.registry.NumismaticsIcons;
import dev.ithundxr.createnumismatics.registry.NumismaticsItems;
import dev.ithundxr.createnumismatics.util.TextUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Rarity;

import java.util.List;
import java.util.Locale;
import java.util.*;

import static dev.ithundxr.createnumismatics.registry.NumismaticsIcons.*;

Expand Down Expand Up @@ -144,4 +144,25 @@ public static Coin closest(int value) {
}
return closest;
}

public static List<Map.Entry<Coin, Integer>> getCoinsFromSpurAmount(int spurAmount){
List<Map.Entry<Coin, Integer>> coins = new ArrayList<>();
for(Coin coin : Arrays.stream(Coin.values()).sorted(Comparator.comparingInt(c -> -c.value)).toList()){
Couple<Integer> coinAmount = coin.convert(spurAmount);
coins.add(new AbstractMap.SimpleEntry<>(coin, coinAmount.getFirst()));
spurAmount = coinAmount.getSecond();
}
return coins;
}

public static Coin getCoinFromName(String name){
Coin selectedCoin = null;
for (Coin coin : Coin.values()){
if(coin.getName().equals(name)){
selectedCoin = coin;
break;
}
}
return selectedCoin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

package dev.ithundxr.createnumismatics.content.depositor;

import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour;
import com.simibubi.create.foundation.utility.Components;
import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.Lang;
import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.content.backend.Coin;
import dev.ithundxr.createnumismatics.content.backend.behaviours.SliderStylePriceBehaviour;
import dev.ithundxr.createnumismatics.content.backend.trust_list.TrustListMenu;
Expand All @@ -47,6 +49,7 @@
public class BrassDepositorBlockEntity extends AbstractDepositorBlockEntity implements MenuProvider {

private SliderStylePriceBehaviour price;
public AbstractComputerBehaviour computerBehaviour;

public BrassDepositorBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
Expand All @@ -55,6 +58,7 @@ public BrassDepositorBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockSta
@Override
public void addBehaviours(List<BlockEntityBehaviour> behaviours) {
price = new SliderStylePriceBehaviour(this, this::addCoin, this::getCoinCount);
behaviours.add(computerBehaviour = ComputerCraftProxy.behaviour(this));
behaviours.add(price);
}

Expand Down
Loading

0 comments on commit ac4defb

Please sign in to comment.