Skip to content

Commit

Permalink
Fix CC compat on forge
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Jun 5, 2024
1 parent 423fa94 commit 97b7dcc
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
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;

import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
Expand All @@ -21,7 +39,6 @@ static void registerWithDependency() {
}

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

@ExpectPlatform
public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,10 @@ public void setChanged() {

AbstractComputerBehaviour computerBehaviour;



public VendorBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}


@Override
public void addBehaviours(List<BlockEntityBehaviour> behaviours) {
price = new SliderStylePriceBehaviour(this, this::addCoin, this::getCoinCount);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 dev.ithundxr.createnumismatics.content.depositor.AbstractDepositorBlockEntity;
import dev.ithundxr.createnumismatics.content.depositor.BrassDepositorBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(BrassDepositorBlockEntity.class)
public abstract class BrassDepositorBlockEntityCapabilities extends AbstractDepositorBlockEntity implements ICapabilityProvider {
public BrassDepositorBlockEntityCapabilities(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}

@Shadow AbstractComputerBehaviour computerBehaviour;

@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction direction) {
if (computerBehaviour.isPeripheralCap(capability))
return computerBehaviour.getPeripheralCapability();

return super.getCapability(capability, direction);
}

@Override
public void invalidateCaps() {
super.invalidateCaps();
computerBehaviour.removePeripheral();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package dev.ithundxr.createnumismatics.forge.mixin.self;

import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import dev.ithundxr.createnumismatics.content.vendor.VendorBlockEntity;
import net.minecraft.core.BlockPos;
Expand All @@ -32,6 +33,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

@Mixin(VendorBlockEntity.class)
Expand All @@ -40,6 +42,8 @@ public VendorBlockEntityCapabilities(BlockEntityType<?> type, BlockPos pos, Bloc
super(type, pos, state);
}

@Shadow AbstractComputerBehaviour computerBehaviour;

// This is actually just down + all the 5 other sides
@Unique private static final Direction[] numismatics$DIRECTIONS = {Direction.DOWN, Direction.NORTH};
@Unique LazyOptional<? extends IItemHandler>[] numismatics$handlers = SidedInvWrapper.create((WorldlyContainer) this, numismatics$DIRECTIONS);
Expand All @@ -50,6 +54,10 @@ public VendorBlockEntityCapabilities(BlockEntityType<?> type, BlockPos pos, Bloc
// If down return the down handler otherwise return the one for all other sides
return facing == Direction.DOWN ? numismatics$handlers[0].cast() : numismatics$handlers[1].cast();
}

if (computerBehaviour.isPeripheralCap(capability))
return computerBehaviour.getPeripheralCapability();

return super.getCapability(capability, facing);
}

Expand All @@ -62,6 +70,7 @@ public void reviveCaps() {
@Override
public void invalidateCaps() {
super.invalidateCaps();
computerBehaviour.removePeripheral();

for (LazyOptional<? extends IItemHandler> createNumismatics$handler : numismatics$handlers) {
createNumismatics$handler.invalidate();
Expand Down
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 @@ -10,6 +10,7 @@
],
"mixins": [
"ServerGamePacketListenerImplMixin",
"self.BrassDepositorBlockEntityCapabilities",
"self.VendorBlockEntityCapabilities"
],
"injectors": {
Expand Down

0 comments on commit 97b7dcc

Please sign in to comment.