Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ComputerCraft integrations to more devices #6849

Open
wants to merge 7 commits into
base: mc1.18/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
ElementW committed Sep 5, 2024
commit 13c01123394c49be8540601c985aa6b1173840d9
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();
}

}