From 463405f8e42e3a5a438ca62b914c3ddad606e158 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 5 Jun 2024 18:09:20 -0400 Subject: [PATCH] Fix CC compat on forge 2: Electric Boogaloo --- .../computercraft/ComputerCraftProxy.java | 3 +- .../implementation/ComputerBehaviour.java | 33 +++++++--- .../peripherals/BrassDepositorPeripheral.java | 20 ++++++- .../peripherals/VendorPeripheral.java | 27 ++++++--- .../fabric/ComputerCraftProxyImpl.java | 37 ++++++++---- .../forge/ComputerCraftProxyImpl.java | 35 +++++++---- .../self/ComputerBehaviourCapabilities.java | 60 +++++++++++++++++++ .../main/resources/numismatics.mixins.json | 1 + 8 files changed, 172 insertions(+), 44 deletions(-) create mode 100644 forge/src/main/java/dev/ithundxr/createnumismatics/forge/mixin/self/ComputerBehaviourCapabilities.java diff --git a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/ComputerCraftProxy.java b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/ComputerCraftProxy.java index 4a5ac465..fdc9410c 100644 --- a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/ComputerCraftProxy.java +++ b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/ComputerCraftProxy.java @@ -27,7 +27,6 @@ import java.util.function.Function; public class ComputerCraftProxy { - public static void register() { fallbackFactory = FallbackComputerBehaviour::new; Mods.COMPUTERCRAFT.executeIfInstalled(() -> ComputerCraftProxy::registerWithDependency); @@ -39,10 +38,10 @@ static void registerWithDependency() { } public static Function fallbackFactory; + public static Function computerFactory; @ExpectPlatform public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe) { throw new AssertionError(); } - } \ No newline at end of file diff --git a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/ComputerBehaviour.java b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/ComputerBehaviour.java index aac88f9b..c75cf912 100644 --- a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/ComputerBehaviour.java +++ b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/ComputerBehaviour.java @@ -1,9 +1,27 @@ +/* + * 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 . + */ + package dev.ithundxr.createnumismatics.compat.computercraft.implementation; -import com.jozufozu.flywheel.util.NonNullSupplier; 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; @@ -13,6 +31,7 @@ import net.minecraft.world.level.Level; public class ComputerBehaviour extends AbstractComputerBehaviour { + NonNullSupplier peripheralSupplier; public static IPeripheral peripheralProvider(Level level, BlockPos blockPos) { AbstractComputerBehaviour behavior = BlockEntityBehaviour.get(level, blockPos, AbstractComputerBehaviour.TYPE); @@ -21,18 +40,16 @@ public static IPeripheral peripheralProvider(Level level, BlockPos blockPos) { return null; } - IPeripheral peripheral; public ComputerBehaviour(SmartBlockEntity te) { super(te); - this.peripheral = getPeripheralFor(te); + this.peripheralSupplier = getPeripheralFor(te); } - public static IPeripheral getPeripheralFor(SmartBlockEntity be) { + public static NonNullSupplier getPeripheralFor(SmartBlockEntity be) { if (be instanceof BrassDepositorBlockEntity scbe) - return new BrassDepositorPeripheral(scbe); + return () -> new BrassDepositorPeripheral(scbe); if (be instanceof VendorBlockEntity scbe) - return new VendorPeripheral(scbe); - + return () -> new VendorPeripheral(scbe); throw new IllegalArgumentException("No peripheral available for " + be.getType()); } @@ -40,6 +57,6 @@ public static IPeripheral getPeripheralFor(SmartBlockEntity be) { @Override public T getPeripheral() { //noinspection unchecked - return (T) peripheral; + return (T) peripheralSupplier.get(); } } diff --git a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/BrassDepositorPeripheral.java b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/BrassDepositorPeripheral.java index a9176406..2b88fd86 100644 --- a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/BrassDepositorPeripheral.java +++ b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/BrassDepositorPeripheral.java @@ -1,3 +1,21 @@ +/* + * 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 . + */ + package dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals; import com.simibubi.create.compat.computercraft.implementation.peripherals.SyncedPeripheral; @@ -13,7 +31,6 @@ import static dev.ithundxr.createnumismatics.content.backend.Coin.getCoinsFromSpurAmount; public class BrassDepositorPeripheral extends SyncedPeripheral { - public BrassDepositorPeripheral(BrassDepositorBlockEntity blockEntity) { super(blockEntity); } @@ -25,6 +42,7 @@ public final void setCoinAmount(String coinName, int amount) throws LuaException blockEntity.setPrice(coin, amount); blockEntity.notifyUpdate(); } + @LuaFunction(mainThread = true) public final void setTotalPrice(int spurAmount){ List> coins = getCoinsFromSpurAmount(spurAmount); diff --git a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/VendorPeripheral.java b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/VendorPeripheral.java index cf271689..9d7b81bc 100644 --- a/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/VendorPeripheral.java +++ b/common/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/implementation/peripherals/VendorPeripheral.java @@ -1,3 +1,21 @@ +/* + * 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 . + */ + package dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals; import com.simibubi.create.compat.computercraft.implementation.peripherals.SyncedPeripheral; @@ -5,11 +23,6 @@ import dan200.computercraft.api.lua.LuaFunction; import dev.ithundxr.createnumismatics.content.backend.Coin; import dev.ithundxr.createnumismatics.content.vendor.VendorBlockEntity; -import net.minecraft.core.registries.Registries; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; import java.util.List; import java.util.Map; @@ -18,13 +31,10 @@ import static dev.ithundxr.createnumismatics.content.backend.Coin.getCoinsFromSpurAmount; public class VendorPeripheral extends SyncedPeripheral { - public VendorPeripheral(VendorBlockEntity blockEntity) { super(blockEntity); } - - @LuaFunction(mainThread = true) public final void setCoinAmount(String coinName, int amount) throws LuaException { Coin coin = getCoinFromName(coinName); @@ -32,6 +42,7 @@ public final void setCoinAmount(String coinName, int amount) throws LuaException blockEntity.setPrice(coin, amount); blockEntity.notifyUpdate(); } + @LuaFunction(mainThread = true) public final void setTotalPrice(int spurAmount){ List> coins = getCoinsFromSpurAmount(spurAmount); diff --git a/fabric/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/fabric/ComputerCraftProxyImpl.java b/fabric/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/fabric/ComputerCraftProxyImpl.java index 5ea2f07e..80d9cc2c 100644 --- a/fabric/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/fabric/ComputerCraftProxyImpl.java +++ b/fabric/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/fabric/ComputerCraftProxyImpl.java @@ -1,29 +1,40 @@ +/* + * 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 . + */ + package dev.ithundxr.createnumismatics.compat.computercraft.fabric; import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour; import com.simibubi.create.foundation.blockEntity.SmartBlockEntity; import dan200.computercraft.api.peripheral.PeripheralLookup; +import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy; import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour; -import java.util.function.Function; - -import static dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy.fallbackFactory; -import static dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour.peripheralProvider; - public class ComputerCraftProxyImpl { - - public static Function computerFactory; - public static void registerWithDependency() { /* Comment if computercraft.implementation is not in the source set */ - computerFactory = ComputerBehaviour::new; + ComputerCraftProxy.computerFactory = ComputerBehaviour::new; - PeripheralLookup.get().registerFallback((level, blockPos, blockState, blockEntity, direction) -> peripheralProvider(level, blockPos)); + PeripheralLookup.get().registerFallback((level, blockPos, blockState, blockEntity, direction) -> ComputerBehaviour.peripheralProvider(level, blockPos)); } public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe) { - if (computerFactory == null) - return fallbackFactory.apply(sbe); - return computerFactory.apply(sbe); + if (ComputerCraftProxy.computerFactory == null) + return ComputerCraftProxy.fallbackFactory.apply(sbe); + return ComputerCraftProxy.computerFactory.apply(sbe); } } diff --git a/forge/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/forge/ComputerCraftProxyImpl.java b/forge/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/forge/ComputerCraftProxyImpl.java index 467a071d..fe83abe5 100644 --- a/forge/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/forge/ComputerCraftProxyImpl.java +++ b/forge/src/main/java/dev/ithundxr/createnumismatics/compat/computercraft/forge/ComputerCraftProxyImpl.java @@ -1,25 +1,36 @@ +/* + * 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 . + */ + package dev.ithundxr.createnumismatics.compat.computercraft.forge; import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour; import com.simibubi.create.foundation.blockEntity.SmartBlockEntity; +import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy; import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour; -import java.util.function.Function; - -import static dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy.fallbackFactory; -import static dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour.peripheralProvider; - public class ComputerCraftProxyImpl { - - public static Function computerFactory; - public static void registerWithDependency() { /* Comment if computercraft.implementation is not in the source set */ - computerFactory = ComputerBehaviour::new; + ComputerCraftProxy.computerFactory = ComputerBehaviour::new; } public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe) { - if (computerFactory == null) - return fallbackFactory.apply(sbe); - return computerFactory.apply(sbe); + if (ComputerCraftProxy.computerFactory == null) + return ComputerCraftProxy.fallbackFactory.apply(sbe); + return ComputerCraftProxy.computerFactory.apply(sbe); } } diff --git a/forge/src/main/java/dev/ithundxr/createnumismatics/forge/mixin/self/ComputerBehaviourCapabilities.java b/forge/src/main/java/dev/ithundxr/createnumismatics/forge/mixin/self/ComputerBehaviourCapabilities.java new file mode 100644 index 00000000..48d724f6 --- /dev/null +++ b/forge/src/main/java/dev/ithundxr/createnumismatics/forge/mixin/self/ComputerBehaviourCapabilities.java @@ -0,0 +1,60 @@ +/* + * 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 . + */ + +package dev.ithundxr.createnumismatics.forge.mixin.self; + +import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour; +import com.simibubi.create.foundation.blockEntity.SmartBlockEntity; +import com.tterrag.registrate.util.nullness.NonNullSupplier; +import dan200.computercraft.api.peripheral.IPeripheral; +import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityManager; +import net.minecraftforge.common.capabilities.CapabilityToken; +import net.minecraftforge.common.util.LazyOptional; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(ComputerBehaviour.class) +public class ComputerBehaviourCapabilities extends AbstractComputerBehaviour { + public ComputerBehaviourCapabilities(SmartBlockEntity te) { + super(te); + } + + private static final Capability RAILWAYS$PERIPHERAL_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {}); + LazyOptional railways$peripheral; + @Shadow NonNullSupplier peripheralSupplier; + + @Override + public boolean isPeripheralCap(Capability cap) { + return cap == RAILWAYS$PERIPHERAL_CAPABILITY; + } + + @Override + public LazyOptional getPeripheralCapability() { + if (railways$peripheral == null || !railways$peripheral.isPresent()) + railways$peripheral = LazyOptional.of(() -> peripheralSupplier.get()); + return railways$peripheral.cast(); + } + + @Override + public void removePeripheral() { + if (railways$peripheral != null) + railways$peripheral.invalidate(); + } +} diff --git a/forge/src/main/resources/numismatics.mixins.json b/forge/src/main/resources/numismatics.mixins.json index 5da27406..698ea854 100644 --- a/forge/src/main/resources/numismatics.mixins.json +++ b/forge/src/main/resources/numismatics.mixins.json @@ -11,6 +11,7 @@ "mixins": [ "ServerGamePacketListenerImplMixin", "self.BrassDepositorBlockEntityCapabilities", + "self.ComputerBehaviourCapabilities", "self.VendorBlockEntityCapabilities" ], "injectors": {