Add ComputerCraft integration to Stickers

- Add CC isExtended() function to check extension state
- Add CC extend()/retract()/toggle() to change the Sticker extension
  state, returning state change success.
This commit is contained in:
Céleste Wouters 2024-09-05 11:51:06 +02:00
parent 6bdd9c1fe0
commit 86fa50ca73
Failed to generate hash of commit
3 changed files with 89 additions and 1 deletions

View file

@ -13,9 +13,11 @@ import com.simibubi.create.compat.computercraft.implementation.peripherals.Signa
import com.simibubi.create.compat.computercraft.implementation.peripherals.SpeedControllerPeripheral;
import com.simibubi.create.compat.computercraft.implementation.peripherals.SpeedGaugePeripheral;
import com.simibubi.create.compat.computercraft.implementation.peripherals.StationPeripheral;
import com.simibubi.create.compat.computercraft.implementation.peripherals.StickerPeripheral;
import com.simibubi.create.compat.computercraft.implementation.peripherals.StressGaugePeripheral;
import com.simibubi.create.compat.computercraft.implementation.peripherals.SyncedPeripheral;
import com.simibubi.create.compat.computercraft.implementation.peripherals.TrackObserverPeripheral;
import com.simibubi.create.content.contraptions.chassis.StickerBlockEntity;
import com.simibubi.create.content.kinetics.gauge.SpeedGaugeBlockEntity;
import com.simibubi.create.content.kinetics.gauge.StressGaugeBlockEntity;
import com.simibubi.create.content.kinetics.motor.CreativeMotorBlockEntity;
@ -66,6 +68,8 @@ public class ComputerBehaviour extends AbstractComputerBehaviour {
return () -> new SpeedGaugePeripheral(sgbe);
if (be instanceof StressGaugeBlockEntity sgbe)
return () -> new StressGaugePeripheral(sgbe);
if (be instanceof StickerBlockEntity sbe)
return () -> new StickerPeripheral(sbe);
if (be instanceof StationBlockEntity sbe)
return () -> new StationPeripheral(sbe);
if (be instanceof TrackObserverBlockEntity tobe)

View file

@ -0,0 +1,62 @@
package com.simibubi.create.compat.computercraft.implementation.peripherals;
import org.jetbrains.annotations.NotNull;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.content.contraptions.chassis.StickerBlock;
import com.simibubi.create.content.contraptions.chassis.StickerBlockEntity;
import dan200.computercraft.api.lua.LuaFunction;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
public class StickerPeripheral extends SyncedPeripheral<StickerBlockEntity> {
public StickerPeripheral(StickerBlockEntity blockEntity) {
super(blockEntity);
}
@LuaFunction
public boolean isExtended() {
return blockEntity.isBlockStateExtended();
}
@LuaFunction(mainThread = true)
public boolean extend() {
BlockState state = blockEntity.getBlockState();
if (!AllBlocks.STICKER.has(state) || state.getValue(StickerBlock.EXTENDED))
return false;
blockEntity.getLevel().setBlock(
blockEntity.getBlockPos(), state.setValue(StickerBlock.EXTENDED, true), Block.UPDATE_CLIENTS);
return true;
}
@LuaFunction(mainThread = true)
public boolean retract() {
BlockState state = blockEntity.getBlockState();
if (!AllBlocks.STICKER.has(state) || !state.getValue(StickerBlock.EXTENDED))
return false;
blockEntity.getLevel().setBlock(
blockEntity.getBlockPos(), state.setValue(StickerBlock.EXTENDED, false), Block.UPDATE_CLIENTS);
return true;
}
@LuaFunction(mainThread = true)
public boolean toggle() {
BlockState state = blockEntity.getBlockState();
if (!AllBlocks.STICKER.has(state))
return false;
boolean extended = state.getValue(StickerBlock.EXTENDED);
blockEntity.getLevel().setBlock(
blockEntity.getBlockPos(), state.setValue(StickerBlock.EXTENDED, !extended), Block.UPDATE_CLIENTS);
return true;
}
@NotNull
@Override
public String getType() {
return "Create_Sticker";
}
}

View file

@ -4,6 +4,8 @@ import java.util.List;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllSoundEvents;
import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.compat.computercraft.ComputerCraftProxy;
import com.simibubi.create.content.contraptions.glue.SuperGlueEntity;
import com.simibubi.create.content.contraptions.glue.SuperGlueItem;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
@ -21,13 +23,19 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.DistExecutor;
import org.jetbrains.annotations.NotNull;
public class StickerBlockEntity extends SmartBlockEntity {
LerpedFloat piston;
boolean update;
public AbstractComputerBehaviour computerBehaviour;
public StickerBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
piston = LerpedFloat.linear();
@ -35,7 +43,9 @@ public class StickerBlockEntity extends SmartBlockEntity {
}
@Override
public void addBehaviours(List<BlockEntityBehaviour> behaviours) {}
public void addBehaviours(List<BlockEntityBehaviour> behaviours) {
behaviours.add(computerBehaviour = ComputerCraftProxy.behaviour(this));
}
@Override
public void initialize() {
@ -99,5 +109,17 @@ public class StickerBlockEntity extends SmartBlockEntity {
AllSoundEvents.SLIME_ADDED.play(level, Minecraft.getInstance().player, worldPosition, 0.35f, attach ? 0.75f : 0.2f);
}
@Override
public <T> @NotNull LazyOptional<T> getCapability(@NotNull Capability<T> cap, Direction side) {
if (computerBehaviour.isPeripheralCap(cap))
return computerBehaviour.getPeripheralCapability();
return super.getCapability(cap, side);
}
@Override
public void invalidateCaps() {
super.invalidateCaps();
computerBehaviour.removePeripheral();
}
}