Created ComputerBehaviour behaviour

- ComputerBehaviour replaces ComputerControllable and SyncedComputerControllable
This commit is contained in:
caelwarner 2022-10-19 22:39:38 -07:00
parent 19d283b923
commit 56a1210fff
No known key found for this signature in database
GPG Key ID: 514BEF5EADE889FF
16 changed files with 197 additions and 242 deletions

View File

@ -1,6 +1,7 @@
package com.simibubi.create.compat.computercraft; package com.simibubi.create.compat.computercraft;
import com.simibubi.create.foundation.networking.TileEntityDataPacket; import com.simibubi.create.foundation.networking.TileEntityDataPacket;
import com.simibubi.create.foundation.tileEntity.SmartTileEntity;
import com.simibubi.create.foundation.tileEntity.SyncedTileEntity; import com.simibubi.create.foundation.tileEntity.SyncedTileEntity;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -27,8 +28,8 @@ public class AttachedComputerPacket extends TileEntityDataPacket<SyncedTileEntit
@Override @Override
protected void handlePacket(SyncedTileEntity tile) { protected void handlePacket(SyncedTileEntity tile) {
if (tile instanceof SyncedComputerControllable computerControllable) { if (tile instanceof SmartTileEntity smartTile) {
computerControllable.setHasAttachedComputer(hasAttachedComputer); smartTile.getBehaviour(ComputerBehaviour.TYPE).setHasAttachedComputer(hasAttachedComputer);
} }
} }

View File

@ -0,0 +1,62 @@
package com.simibubi.create.compat.computercraft;
import org.jetbrains.annotations.NotNull;
import com.simibubi.create.foundation.tileEntity.SmartTileEntity;
import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour;
import com.simibubi.create.foundation.tileEntity.behaviour.BehaviourType;
import dan200.computercraft.api.peripheral.IPeripheral;
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 net.minecraftforge.common.util.NonNullSupplier;
public class ComputerBehaviour extends TileEntityBehaviour {
public static final BehaviourType<ComputerBehaviour> TYPE = new BehaviourType<>();
protected static final Capability<IPeripheral> PERIPHERAL_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {});
LazyOptional<IPeripheral> peripheral;
NonNullSupplier<IPeripheral> peripheralSupplier;
boolean hasAttachedComputer;
public ComputerBehaviour(SmartTileEntity te, NonNullSupplier<IPeripheral> peripheralSupplier) {
super(te);
this.peripheralSupplier = peripheralSupplier;
this.hasAttachedComputer = false;
}
public static <T> boolean isPeripheralCap(@NotNull Capability<T> cap) {
return cap == PERIPHERAL_CAPABILITY;
}
public <T> LazyOptional<T> getPeripheralCapability() {
if (peripheral == null || !peripheral.isPresent())
peripheral = LazyOptional.of(peripheralSupplier);
return peripheral.cast();
}
public void removePeripheral() {
if (peripheral != null) {
peripheral.invalidate();
}
}
public void setHasAttachedComputer(boolean hasAttachedComputer) {
this.hasAttachedComputer = hasAttachedComputer;
}
public boolean hasAttachedComputer() {
return hasAttachedComputer;
}
@Override
public BehaviourType<?> getType() {
return TYPE;
}
}

View File

@ -1,37 +0,0 @@
package com.simibubi.create.compat.computercraft;
import org.jetbrains.annotations.NotNull;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.util.LazyOptional;
public interface ComputerControllable {
Capability<IPeripheral> PERIPHERAL_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {});
IPeripheral createPeripheral();
void setPeripheral(LazyOptional<IPeripheral> peripheral);
LazyOptional<IPeripheral> getPeripheral();
default <T> LazyOptional<T> getPeripheralCapability(@NotNull Capability<T> cap) {
if (cap == PERIPHERAL_CAPABILITY) {
if (getPeripheral() == null || !getPeripheral().isPresent())
setPeripheral(LazyOptional.of(this::createPeripheral));
return getPeripheral().cast();
}
return LazyOptional.empty();
}
default void removePeripheral() {
if (getPeripheral() != null) {
getPeripheral().invalidate();
}
}
}

View File

@ -1,7 +0,0 @@
package com.simibubi.create.compat.computercraft;
public interface SyncedComputerControllable extends ComputerControllable {
void setHasAttachedComputer(boolean hasAttachedComputer);
}

View File

@ -1,19 +1,47 @@
package com.simibubi.create.compat.computercraft.peripherals; package com.simibubi.create.compat.computercraft.peripherals;
import com.simibubi.create.compat.computercraft.ComputerControllable; import java.util.concurrent.atomic.AtomicInteger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import dan200.computercraft.api.peripheral.IPeripheral; import com.simibubi.create.compat.computercraft.AttachedComputerPacket;
import net.minecraft.world.level.block.entity.BlockEntity; import com.simibubi.create.compat.computercraft.ComputerBehaviour;
import com.simibubi.create.foundation.networking.AllPackets;
import com.simibubi.create.foundation.tileEntity.SmartTileEntity;
public abstract class PeripheralBase<T extends BlockEntity & ComputerControllable> implements IPeripheral { import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraftforge.network.PacketDistributor;
public abstract class PeripheralBase<T extends SmartTileEntity> implements IPeripheral {
protected final T tile; protected final T tile;
private final AtomicInteger computers = new AtomicInteger();
public PeripheralBase(T tile) { public PeripheralBase(T tile) {
this.tile = tile; this.tile = tile;
} }
@Override
public void attach(@NotNull IComputerAccess computer) {
computers.incrementAndGet();
updateTile();
}
@Override
public void detach(@NotNull IComputerAccess computer) {
computers.decrementAndGet();
updateTile();
}
private void updateTile() {
boolean hasAttachedComputer = computers.get() > 0;
tile.getBehaviour(ComputerBehaviour.TYPE).setHasAttachedComputer(hasAttachedComputer);
AllPackets.channel.send(PacketDistributor.ALL.noArg(), new AttachedComputerPacket(tile.getBlockPos(), hasAttachedComputer));
}
@Override @Override
public boolean equals(@Nullable IPeripheral other) { public boolean equals(@Nullable IPeripheral other) {
return this == other; return this == other;

View File

@ -1,6 +1,5 @@
package com.simibubi.create.compat.computercraft.peripherals; package com.simibubi.create.compat.computercraft.peripherals;
import com.simibubi.create.compat.computercraft.peripherals.SyncedPeripheralBase;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import com.simibubi.create.content.contraptions.relays.advanced.sequencer.Instruction; import com.simibubi.create.content.contraptions.relays.advanced.sequencer.Instruction;
@ -12,7 +11,7 @@ import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction; import dan200.computercraft.api.lua.LuaFunction;
public class SequencedGearshiftPeripheral extends SyncedPeripheralBase<SequencedGearshiftTileEntity> { public class SequencedGearshiftPeripheral extends PeripheralBase<SequencedGearshiftTileEntity> {
public SequencedGearshiftPeripheral(SequencedGearshiftTileEntity tile) { public SequencedGearshiftPeripheral(SequencedGearshiftTileEntity tile) {
super(tile); super(tile);

View File

@ -1,43 +0,0 @@
package com.simibubi.create.compat.computercraft.peripherals;
import java.util.concurrent.atomic.AtomicInteger;
import com.simibubi.create.compat.computercraft.AttachedComputerPacket;
import com.simibubi.create.compat.computercraft.SyncedComputerControllable;
import com.simibubi.create.compat.computercraft.peripherals.PeripheralBase;
import org.jetbrains.annotations.NotNull;
import com.simibubi.create.foundation.networking.AllPackets;
import dan200.computercraft.api.peripheral.IComputerAccess;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.PacketDistributor;
public abstract class SyncedPeripheralBase<T extends BlockEntity & SyncedComputerControllable> extends PeripheralBase<T> {
private final AtomicInteger computers = new AtomicInteger();
public SyncedPeripheralBase(T tile) {
super(tile);
}
@Override
public void attach(@NotNull IComputerAccess computer) {
computers.incrementAndGet();
updateTile();
}
@Override
public void detach(@NotNull IComputerAccess computer) {
computers.decrementAndGet();
updateTile();
}
private void updateTile() {
boolean hasAttachedComputer = computers.get() > 0;
tile.setHasAttachedComputer(hasAttachedComputer);
AllPackets.channel.send(PacketDistributor.ALL.noArg(), new AttachedComputerPacket(tile.getBlockPos(), hasAttachedComputer));
}
}

View File

@ -5,7 +5,7 @@ import java.util.List;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.simibubi.create.compat.computercraft.ComputerControllable; import com.simibubi.create.compat.computercraft.ComputerBehaviour;
import com.simibubi.create.compat.computercraft.peripherals.SpeedControllerPeripheral; import com.simibubi.create.compat.computercraft.peripherals.SpeedControllerPeripheral;
import com.simibubi.create.content.contraptions.RotationPropagator; import com.simibubi.create.content.contraptions.RotationPropagator;
import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntity;
@ -20,7 +20,6 @@ import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollVal
import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.Lang;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.entity.BlockEntityType;
@ -29,15 +28,14 @@ import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
public class SpeedControllerTileEntity extends KineticTileEntity implements ComputerControllable { public class SpeedControllerTileEntity extends KineticTileEntity {
public static final int DEFAULT_SPEED = 16; public static final int DEFAULT_SPEED = 16;
protected ScrollValueBehaviour targetSpeed; protected ScrollValueBehaviour targetSpeed;
ComputerBehaviour computerBehaviour;
boolean hasBracket; boolean hasBracket;
private LazyOptional<IPeripheral> peripheral;
public SpeedControllerTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) { public SpeedControllerTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state); super(type, pos, state);
hasBracket = false; hasBracket = false;
@ -63,6 +61,7 @@ public class SpeedControllerTileEntity extends KineticTileEntity implements Comp
targetSpeed.withCallback(i -> this.updateTargetRotation()); targetSpeed.withCallback(i -> this.updateTargetRotation());
targetSpeed.withStepFunction(CreativeMotorTileEntity::step); targetSpeed.withStepFunction(CreativeMotorTileEntity::step);
behaviours.add(targetSpeed); behaviours.add(targetSpeed);
behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new SpeedControllerPeripheral(this, targetSpeed)));
registerAwardables(behaviours, AllAdvancements.SPEED_CONTROLLER); registerAwardables(behaviours, AllAdvancements.SPEED_CONTROLLER);
} }
@ -140,33 +139,18 @@ public class SpeedControllerTileEntity extends KineticTileEntity implements Comp
@NotNull @NotNull
@Override @Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
LazyOptional<T> peripheralCap = getPeripheralCapability(cap); if (ComputerBehaviour.isPeripheralCap(cap))
return computerBehaviour.getPeripheralCapability();
return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side); return super.getCapability(cap, side);
} }
@Override @Override
public void invalidateCaps() { public void invalidateCaps() {
super.invalidateCaps(); super.invalidateCaps();
removePeripheral(); computerBehaviour.removePeripheral();
} }
@Override
public IPeripheral createPeripheral() {
return new SpeedControllerPeripheral(this, targetSpeed);
}
@Override
public void setPeripheral(LazyOptional<IPeripheral> peripheral) {
this.peripheral = peripheral;
}
@Override
public LazyOptional<IPeripheral> getPeripheral() {
return this.peripheral;
}
private class ControllerValueBoxTransform extends ValueBoxTransform.Sided { private class ControllerValueBoxTransform extends ValueBoxTransform.Sided {
@Override @Override

View File

@ -35,7 +35,7 @@ public class ConfigureSequencedGearshiftPacket extends TileEntityConfigurationPa
@Override @Override
protected void applySettings(SequencedGearshiftTileEntity te) { protected void applySettings(SequencedGearshiftTileEntity te) {
if (te.hasAttachedComputer) if (te.computerBehaviour.hasAttachedComputer())
return; return;
te.run(-1); te.run(-1);

View File

@ -37,7 +37,7 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen {
super(Lang.translateDirect("gui.sequenced_gearshift.title")); super(Lang.translateDirect("gui.sequenced_gearshift.title"));
this.instructions = te.instructions; this.instructions = te.instructions;
this.pos = te.getBlockPos(); this.pos = te.getBlockPos();
this.hasAttachedComputer = te.hasAttachedComputer; this.hasAttachedComputer = te.computerBehaviour.hasAttachedComputer();
compareTag = Instruction.serializeAll(instructions); compareTag = Instruction.serializeAll(instructions);
} }

View File

@ -1,15 +1,16 @@
package com.simibubi.create.content.contraptions.relays.advanced.sequencer; package com.simibubi.create.content.contraptions.relays.advanced.sequencer;
import java.util.List;
import java.util.Vector; import java.util.Vector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.simibubi.create.compat.computercraft.ComputerBehaviour;
import com.simibubi.create.compat.computercraft.peripherals.SequencedGearshiftPeripheral; import com.simibubi.create.compat.computercraft.peripherals.SequencedGearshiftPeripheral;
import com.simibubi.create.compat.computercraft.SyncedComputerControllable;
import com.simibubi.create.content.contraptions.relays.encased.SplitShaftTileEntity; import com.simibubi.create.content.contraptions.relays.encased.SplitShaftTileEntity;
import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
@ -19,7 +20,7 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implements SyncedComputerControllable { public class SequencedGearshiftTileEntity extends SplitShaftTileEntity {
Vector<Instruction> instructions; Vector<Instruction> instructions;
int currentInstruction; int currentInstruction;
@ -28,8 +29,7 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement
int timer; int timer;
boolean poweredPreviously; boolean poweredPreviously;
LazyOptional<IPeripheral> peripheral; ComputerBehaviour computerBehaviour;
boolean hasAttachedComputer;
public SequencedGearshiftTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) { public SequencedGearshiftTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state); super(type, pos, state);
@ -39,7 +39,12 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement
currentInstructionProgress = 0; currentInstructionProgress = 0;
timer = 0; timer = 0;
poweredPreviously = false; poweredPreviously = false;
hasAttachedComputer = false; }
@Override
public void addBehaviours(List<TileEntityBehaviour> behaviours) {
super.addBehaviours(behaviours);
behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new SequencedGearshiftPeripheral(this)));
} }
@Override @Override
@ -84,7 +89,7 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement
} }
public void onRedstoneUpdate(boolean isPowered, boolean isRunning) { public void onRedstoneUpdate(boolean isPowered, boolean isRunning) {
if (hasAttachedComputer) if (computerBehaviour.hasAttachedComputer())
return; return;
if (!poweredPreviously && isPowered) if (!poweredPreviously && isPowered)
risingFlank(); risingFlank();
@ -173,35 +178,16 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement
@NotNull @NotNull
@Override @Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
LazyOptional<T> peripheralCap = getPeripheralCapability(cap); if (ComputerBehaviour.isPeripheralCap(cap))
return computerBehaviour.getPeripheralCapability();
return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side); return super.getCapability(cap, side);
} }
@Override @Override
public void invalidateCaps() { public void invalidateCaps() {
super.invalidateCaps(); super.invalidateCaps();
removePeripheral(); computerBehaviour.removePeripheral();
}
@Override
public IPeripheral createPeripheral() {
return new SequencedGearshiftPeripheral(this);
}
@Override
public void setPeripheral(LazyOptional<IPeripheral> peripheral) {
this.peripheral = peripheral;
}
@Override
public LazyOptional<IPeripheral> getPeripheral() {
return this.peripheral;
}
@Override
public void setHasAttachedComputer(boolean hasAttachedComputer) {
this.hasAttachedComputer = hasAttachedComputer;
} }
@Override @Override

View File

@ -2,33 +2,23 @@ package com.simibubi.create.content.contraptions.relays.gauge;
import java.util.List; import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.simibubi.create.compat.computercraft.ComputerControllable;
import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation; import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation;
import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.Lang;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
public abstract class GaugeTileEntity extends KineticTileEntity implements IHaveGoggleInformation, ComputerControllable { public abstract class GaugeTileEntity extends KineticTileEntity implements IHaveGoggleInformation {
public float dialTarget; public float dialTarget;
public float dialState; public float dialState;
public float prevDialState; public float prevDialState;
public int color; public int color;
private LazyOptional<IPeripheral> peripheral;
public GaugeTileEntity(BlockEntityType<?> typeIn, BlockPos pos, BlockState state) { public GaugeTileEntity(BlockEntityType<?> typeIn, BlockPos pos, BlockState state) {
super(typeIn, pos, state); super(typeIn, pos, state);
} }
@ -63,28 +53,4 @@ public abstract class GaugeTileEntity extends KineticTileEntity implements IHave
return true; return true;
} }
@NotNull
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
LazyOptional<T> peripheralCap = getPeripheralCapability(cap);
return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side);
}
@Override
public void invalidateCaps() {
super.invalidateCaps();
removePeripheral();
}
@Override
public void setPeripheral(LazyOptional<IPeripheral> peripheral) {
this.peripheral = peripheral;
}
@Override
public LazyOptional<IPeripheral> getPeripheral() {
return this.peripheral;
}
} }

View File

@ -2,26 +2,41 @@ package com.simibubi.create.content.contraptions.relays.gauge;
import java.util.List; import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.simibubi.create.compat.computercraft.ComputerBehaviour;
import com.simibubi.create.compat.computercraft.peripherals.SpeedGaugePeripheral; import com.simibubi.create.compat.computercraft.peripherals.SpeedGaugePeripheral;
import com.simibubi.create.content.contraptions.base.IRotate.SpeedLevel; import com.simibubi.create.content.contraptions.base.IRotate.SpeedLevel;
import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.config.AllConfigs;
import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour;
import com.simibubi.create.foundation.utility.Color; import com.simibubi.create.foundation.utility.Color;
import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.Lang;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
public class SpeedGaugeTileEntity extends GaugeTileEntity { public class SpeedGaugeTileEntity extends GaugeTileEntity {
ComputerBehaviour computerBehaviour;
public SpeedGaugeTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) { public SpeedGaugeTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state); super(type, pos, state);
} }
@Override
public void addBehaviours(List<TileEntityBehaviour> behaviours) {
super.addBehaviours(behaviours);
behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new SpeedGaugePeripheral(this)));
}
@Override @Override
public void onSpeedChanged(float prevSpeed) { public void onSpeedChanged(float prevSpeed) {
super.onSpeedChanged(prevSpeed); super.onSpeedChanged(prevSpeed);
@ -65,9 +80,19 @@ public class SpeedGaugeTileEntity extends GaugeTileEntity {
return true; return true;
} }
@NotNull
@Override @Override
public IPeripheral createPeripheral() { public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
return new SpeedGaugePeripheral(this); if (ComputerBehaviour.isPeripheralCap(cap))
return computerBehaviour.getPeripheralCapability();
return super.getCapability(cap, side);
}
@Override
public void invalidateCaps() {
super.invalidateCaps();
computerBehaviour.removePeripheral();
} }
} }

View File

@ -2,6 +2,10 @@ package com.simibubi.create.content.contraptions.relays.gauge;
import java.util.List; import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.simibubi.create.compat.computercraft.ComputerBehaviour;
import com.simibubi.create.compat.computercraft.peripherals.StressGaugePeripheral; import com.simibubi.create.compat.computercraft.peripherals.StressGaugePeripheral;
import com.simibubi.create.content.contraptions.base.IRotate.StressImpact; import com.simibubi.create.content.contraptions.base.IRotate.StressImpact;
import com.simibubi.create.foundation.advancement.AllAdvancements; import com.simibubi.create.foundation.advancement.AllAdvancements;
@ -12,17 +16,20 @@ import com.simibubi.create.foundation.utility.Color;
import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.Lang;
import com.simibubi.create.foundation.utility.LangBuilder; import com.simibubi.create.foundation.utility.LangBuilder;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
public class StressGaugeTileEntity extends GaugeTileEntity { public class StressGaugeTileEntity extends GaugeTileEntity {
ComputerBehaviour computerBehaviour;
static BlockPos lastSent; static BlockPos lastSent;
public StressGaugeTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) { public StressGaugeTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
@ -32,6 +39,7 @@ public class StressGaugeTileEntity extends GaugeTileEntity {
@Override @Override
public void addBehaviours(List<TileEntityBehaviour> behaviours) { public void addBehaviours(List<TileEntityBehaviour> behaviours) {
super.addBehaviours(behaviours); super.addBehaviours(behaviours);
behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new StressGaugePeripheral(this)));
registerAwardables(behaviours, AllAdvancements.STRESSOMETER, AllAdvancements.STRESSOMETER_MAXED); registerAwardables(behaviours, AllAdvancements.STRESSOMETER, AllAdvancements.STRESSOMETER_MAXED);
} }
@ -143,9 +151,19 @@ public class StressGaugeTileEntity extends GaugeTileEntity {
award(AllAdvancements.STRESSOMETER_MAXED); award(AllAdvancements.STRESSOMETER_MAXED);
} }
@NotNull
@Override @Override
public IPeripheral createPeripheral() { public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
return new StressGaugePeripheral(this); if (ComputerBehaviour.isPeripheralCap(cap))
return computerBehaviour.getPeripheralCapability();
return super.getCapability(cap, side);
}
@Override
public void invalidateCaps() {
super.invalidateCaps();
computerBehaviour.removePeripheral();
} }
} }

View File

@ -5,7 +5,7 @@ import java.util.List;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.simibubi.create.compat.computercraft.ComputerControllable; import com.simibubi.create.compat.computercraft.ComputerBehaviour;
import com.simibubi.create.compat.computercraft.peripherals.DisplayLinkPeripheral; import com.simibubi.create.compat.computercraft.peripherals.DisplayLinkPeripheral;
import com.simibubi.create.content.logistics.block.display.source.DisplaySource; import com.simibubi.create.content.logistics.block.display.source.DisplaySource;
import com.simibubi.create.content.logistics.block.display.target.DisplayTarget; import com.simibubi.create.content.logistics.block.display.target.DisplayTarget;
@ -16,7 +16,6 @@ import com.simibubi.create.foundation.utility.NBTHelper;
import com.simibubi.create.foundation.utility.animation.LerpedFloat; import com.simibubi.create.foundation.utility.animation.LerpedFloat;
import com.simibubi.create.foundation.utility.animation.LerpedFloat.Chaser; import com.simibubi.create.foundation.utility.animation.LerpedFloat.Chaser;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
@ -27,7 +26,7 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerControllable { public class DisplayLinkTileEntity extends SmartTileEntity {
protected BlockPos targetOffset; protected BlockPos targetOffset;
@ -42,7 +41,7 @@ public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerCo
public int refreshTicks; public int refreshTicks;
private LazyOptional<IPeripheral> peripheral; ComputerBehaviour computerBehaviour;
public DisplayLinkTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) { public DisplayLinkTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state); super(type, pos, state);
@ -54,6 +53,13 @@ public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerCo
glow.chase(0, 0.5f, Chaser.EXP); glow.chase(0, 0.5f, Chaser.EXP);
} }
@Override
public void addBehaviours(List<TileEntityBehaviour> behaviours) {
behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new DisplayLinkPeripheral(this)));
registerAwardables(behaviours, AllAdvancements.DISPLAY_LINK, AllAdvancements.DISPLAY_BOARD);
}
@Override @Override
public void tick() { public void tick() {
super.tick(); super.tick();
@ -128,11 +134,6 @@ public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerCo
award(AllAdvancements.DISPLAY_LINK); award(AllAdvancements.DISPLAY_LINK);
} }
@Override
public void addBehaviours(List<TileEntityBehaviour> behaviours) {
registerAwardables(behaviours, AllAdvancements.DISPLAY_LINK, AllAdvancements.DISPLAY_BOARD);
}
@Override @Override
public void writeSafe(CompoundTag tag) { public void writeSafe(CompoundTag tag) {
super.writeSafe(tag); super.writeSafe(tag);
@ -186,30 +187,16 @@ public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerCo
@NotNull @NotNull
@Override @Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
LazyOptional<T> peripheralCap = getPeripheralCapability(cap); if (ComputerBehaviour.isPeripheralCap(cap))
return computerBehaviour.getPeripheralCapability();
return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side); return super.getCapability(cap, side);
} }
@Override @Override
public void invalidateCaps() { public void invalidateCaps() {
super.invalidateCaps(); super.invalidateCaps();
removePeripheral(); computerBehaviour.removePeripheral();
}
@Override
public IPeripheral createPeripheral() {
return new DisplayLinkPeripheral(this);
}
@Override
public void setPeripheral(LazyOptional<IPeripheral> peripheral) {
this.peripheral = peripheral;
}
@Override
public LazyOptional<IPeripheral> getPeripheral() {
return this.peripheral;
} }
public void target(BlockPos targetPosition) { public void target(BlockPos targetPosition) {

View File

@ -18,7 +18,7 @@ import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllItems; import com.simibubi.create.AllItems;
import com.simibubi.create.AllSoundEvents; import com.simibubi.create.AllSoundEvents;
import com.simibubi.create.Create; import com.simibubi.create.Create;
import com.simibubi.create.compat.computercraft.ComputerControllable; import com.simibubi.create.compat.computercraft.ComputerBehaviour;
import com.simibubi.create.compat.computercraft.peripherals.StationPeripheral; import com.simibubi.create.compat.computercraft.peripherals.StationPeripheral;
import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException;
import com.simibubi.create.content.contraptions.components.structureMovement.ITransformableTE; import com.simibubi.create.content.contraptions.components.structureMovement.ITransformableTE;
@ -55,7 +55,6 @@ import com.simibubi.create.foundation.utility.WorldAttached;
import com.simibubi.create.foundation.utility.animation.LerpedFloat; import com.simibubi.create.foundation.utility.animation.LerpedFloat;
import com.simibubi.create.foundation.utility.animation.LerpedFloat.Chaser; import com.simibubi.create.foundation.utility.animation.LerpedFloat.Chaser;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos; import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
@ -82,7 +81,7 @@ import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.network.PacketDistributor; import net.minecraftforge.network.PacketDistributor;
public class StationTileEntity extends SmartTileEntity implements ITransformableTE, ComputerControllable { public class StationTileEntity extends SmartTileEntity implements ITransformableTE {
public TrackTargetingBehaviour<GlobalStation> edgePoint; public TrackTargetingBehaviour<GlobalStation> edgePoint;
public LerpedFloat flag; public LerpedFloat flag;
@ -90,6 +89,7 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
protected int failedCarriageIndex; protected int failedCarriageIndex;
protected AssemblyException lastException; protected AssemblyException lastException;
protected DepotBehaviour depotBehaviour; protected DepotBehaviour depotBehaviour;
ComputerBehaviour computerBehaviour;
// for display // for display
UUID imminentTrain; UUID imminentTrain;
@ -103,7 +103,6 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
boolean flagFlipped; boolean flagFlipped;
public Component lastDisassembledTrainName; public Component lastDisassembledTrainName;
private LazyOptional<IPeripheral> peripheral;
public StationTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) { public StationTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state); super(type, pos, state);
@ -122,6 +121,7 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
depotBehaviour.addSubBehaviours(behaviours); depotBehaviour.addSubBehaviours(behaviours);
registerAwardables(behaviours, AllAdvancements.CONTRAPTION_ACTORS, AllAdvancements.TRAIN, registerAwardables(behaviours, AllAdvancements.CONTRAPTION_ACTORS, AllAdvancements.TRAIN,
AllAdvancements.LONG_TRAIN, AllAdvancements.CONDUCTOR); AllAdvancements.LONG_TRAIN, AllAdvancements.CONDUCTOR);
behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new StationPeripheral(this)));
} }
@Override @Override
@ -706,18 +706,19 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
@Override @Override
public <T> @NotNull LazyOptional<T> getCapability(@NotNull Capability<T> cap, Direction side) { public <T> @NotNull LazyOptional<T> getCapability(@NotNull Capability<T> cap, Direction side) {
LazyOptional<T> peripheralCap = getPeripheralCapability(cap);
if (isItemHandlerCap(cap)) if (isItemHandlerCap(cap))
return depotBehaviour.getItemCapability(cap, side); return depotBehaviour.getItemCapability(cap, side);
return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side); if (ComputerBehaviour.isPeripheralCap(cap))
return computerBehaviour.getPeripheralCapability();
return super.getCapability(cap, side);
} }
@Override @Override
public void invalidateCaps() { public void invalidateCaps() {
super.invalidateCaps(); super.invalidateCaps();
removePeripheral(); computerBehaviour.removePeripheral();
} }
private void applyAutoSchedule() { private void applyAutoSchedule() {
@ -781,19 +782,4 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
edgePoint.transform(transform); edgePoint.transform(transform);
} }
@Override
public IPeripheral createPeripheral() {
return new StationPeripheral(this);
}
@Override
public void setPeripheral(LazyOptional<IPeripheral> peripheral) {
this.peripheral = peripheral;
}
@Override
public LazyOptional<IPeripheral> getPeripheral() {
return peripheral;
}
} }