Skip to content

Commit

Permalink
Add ComputerCraft integration to Stickers
Browse files Browse the repository at this point in the history
- Add CC isExtended() function to check extension state
- Add CC extend()/retract()/toggle() to change the Sticker extension
  state, returning state change success.
  • Loading branch information
ElementW committed Sep 5, 2024
1 parent abffebd commit 13c0112
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
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;
Expand Down Expand Up @@ -65,6 +67,8 @@ public static NonNullSupplier<SyncedPeripheral<?>> getPeripheralFor(SmartBlockEn
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher;
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;
Expand All @@ -20,21 +22,29 @@
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();
update = false;
}

@Override
public void addBehaviours(List<BlockEntityBehaviour> behaviours) {}
public void addBehaviours(List<BlockEntityBehaviour> behaviours) {
behaviours.add(computerBehaviour = ComputerCraftProxy.behaviour(this));
}

@Override
public void initialize() {
Expand Down Expand Up @@ -85,7 +95,7 @@ public boolean isAttachedToBlock() {
protected void write(CompoundTag tag, boolean clientPacket) {
super.write(tag, clientPacket);
}

@Override
protected void read(CompoundTag compound, boolean clientPacket) {
super.read(compound, clientPacket);
Expand All @@ -98,5 +108,17 @@ public void playSound(boolean attach) {
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();
}

}

0 comments on commit 13c0112

Please sign in to comment.