Skip to content

Commit

Permalink
Fix CC compat on forge 2: Electric Boogaloo
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Jun 5, 2024
1 parent 97b7dcc commit 463405f
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.function.Function;

public class ComputerCraftProxy {

public static void register() {
fallbackFactory = FallbackComputerBehaviour::new;
Mods.COMPUTERCRAFT.executeIfInstalled(() -> ComputerCraftProxy::registerWithDependency);
Expand All @@ -39,10 +38,10 @@ static void registerWithDependency() {
}

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
@@ -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 <https://www.gnu.org/licenses/>.
*/

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;
Expand All @@ -13,6 +31,7 @@
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);
Expand All @@ -21,25 +40,23 @@ 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<IPeripheral> 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());
}

@Override
public <T> T getPeripheral() {
//noinspection unchecked
return (T) peripheral;
return (T) peripheralSupplier.get();
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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

import com.simibubi.create.compat.computercraft.implementation.peripherals.SyncedPeripheral;
Expand All @@ -13,7 +31,6 @@
import static dev.ithundxr.createnumismatics.content.backend.Coin.getCoinsFromSpurAmount;

public class BrassDepositorPeripheral extends SyncedPeripheral<BrassDepositorBlockEntity> {

public BrassDepositorPeripheral(BrassDepositorBlockEntity blockEntity) {
super(blockEntity);
}
Expand All @@ -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<Map.Entry<Coin, Integer>> coins = getCoinsFromSpurAmount(spurAmount);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
/*
* 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 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;
Expand All @@ -18,20 +31,18 @@
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<SmartBlockEntity, ? extends AbstractComputerBehaviour> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<SmartBlockEntity, ? extends AbstractComputerBehaviour> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<IPeripheral> RAILWAYS$PERIPHERAL_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {});
LazyOptional<IPeripheral> railways$peripheral;
@Shadow NonNullSupplier<IPeripheral> peripheralSupplier;

@Override
public <T> boolean isPeripheralCap(Capability<T> cap) {
return cap == RAILWAYS$PERIPHERAL_CAPABILITY;
}

@Override
public <T> LazyOptional<T> 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();
}
}
1 change: 1 addition & 0 deletions forge/src/main/resources/numismatics.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"mixins": [
"ServerGamePacketListenerImplMixin",
"self.BrassDepositorBlockEntityCapabilities",
"self.ComputerBehaviourCapabilities",
"self.VendorBlockEntityCapabilities"
],
"injectors": {
Expand Down

0 comments on commit 463405f

Please sign in to comment.