Skip to content

Commit

Permalink
Changed some of the methods. It might work.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiewuzhiying committed Sep 24, 2024
1 parent 25854b9 commit b2e1e7c
Show file tree
Hide file tree
Showing 18 changed files with 609 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.xiewuzhiying.vs_addition.compats.create.behaviour.link.DualLinkBehaviour;
import io.github.xiewuzhiying.vs_addition.compats.vs_clockwork.behaviour.flap_bearing.FlapBearingLinkFrequencySlot;
import io.github.xiewuzhiying.vs_addition.compats.vs_clockwork.behaviour.flap_bearing.FlapBearingLinkFrequencySlotNegative;
import io.github.xiewuzhiying.vs_addition.mixinducks.vs_clockwork.flap_bearing.FlapBearingBlockEntityMixinDuck;
import kotlin.jvm.internal.Intrinsics;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand All @@ -31,7 +32,7 @@

@Pseudo
@Mixin(FlapBearingBlockEntity.class)
public abstract class MixinFlapBearingBlockEntity extends KineticBlockEntity {
public abstract class MixinFlapBearingBlockEntity extends KineticBlockEntity implements FlapBearingBlockEntityMixinDuck {

@Shadow(remap = false) private BlockPos redstonePos;
@Shadow(remap = false) private float clientAngleDiff;
Expand All @@ -50,6 +51,10 @@ public abstract class MixinFlapBearingBlockEntity extends KineticBlockEntity {
protected boolean receivedSignalPositiveActive;
@Unique
protected boolean receivedSignalNegativeActive;
@Unique
protected float lockedFlapAngle = 0.0f;
@Unique
protected boolean isLocked = false;


public MixinFlapBearingBlockEntity(BlockEntityType<?> typeIn, BlockPos pos, BlockState state) {
Expand Down Expand Up @@ -131,6 +136,38 @@ public void sendData(CallbackInfo ci) {
}
}

@Inject(
method = "getFlapTarget",
at = @At("HEAD"),
cancellable = true,
remap = false
)
private void getFlapTarget(boolean negativeActivated, boolean positiveActivated, CallbackInfoReturnable<Float> cir) {
if (this.isLocked) {
cir.setReturnValue(this.lockedFlapAngle);
}
}

@Override
public void setLockedFlapAngle(float angle) {
this.lockedFlapAngle = angle;
}

@Override
public float getLockedFlapAngle() {
return this.lockedFlapAngle;
}

@Override
public void setIsLocked(boolean bl) {
this.isLocked = bl;
}

@Override
public boolean getIsLocked() {
return this.isLocked;
}

@Unique
protected void createLink() {
Pair<ValueBoxTransform, ValueBoxTransform> slots_positive =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.xiewuzhiying.vs_addition.mixinducks.vs_clockwork.flap_bearing;

public interface FlapBearingBlockEntityMixinDuck {
void setLockedFlapAngle(float angle);
float getLockedFlapAngle();
void setIsLocked(boolean bl);
boolean getIsLocked();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package io.github.xiewuzhiying.vs_addition.compats.computercraft

import dan200.computercraft.api.peripheral.IPeripheral
import io.github.xiewuzhiying.vs_addition.VSAdditionConfig
import io.github.xiewuzhiying.vs_addition.VSAdditionMod.CBC_ACTIVE
import io.github.xiewuzhiying.vs_addition.VSAdditionMod.CLOCKWORK_ACTIVE
import io.github.xiewuzhiying.vs_addition.VSAdditionMod.EUREKA_ACTIVE
import io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral.*
import io.github.xiewuzhiying.vs_addition.compats.computercraft.PeripheralCommon.PeripheralSupplier
import io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral.CheatFlapBearingPeripheral
import io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral.FlapBearingPeripheral
import io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral.ShipHelmPeripheral
import net.minecraft.core.BlockPos
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.Block
Expand All @@ -14,29 +16,11 @@ import org.valkyrienskies.clockwork.ClockworkBlocks
import org.valkyrienskies.clockwork.content.contraptions.flap.FlapBearingBlockEntity
import org.valkyrienskies.eureka.EurekaBlocks
import org.valkyrienskies.eureka.blockentity.ShipHelmBlockEntity
import rbasamoyai.createbigcannons.cannon_control.cannon_mount.CannonMountBlockEntity
import rbasamoyai.createbigcannons.index.CBCBlocks

object PeripheralCommon {
private val peripheralMap: MutableMap<Block, PeripheralSupplier> = HashMap()

init {
if (CBC_ACTIVE) {
peripheralMap[CBCBlocks.CANNON_MOUNT.get()] =
PeripheralSupplier { be: BlockEntity, _: Level, _: BlockPos ->
if (VSAdditionConfig.SERVER.computercraft.enableCheatCannonMountPeripheral) {
CheatCannonMountPeripheral(
"cbc_cannon_mount",
be as CannonMountBlockEntity
)
} else {
CannonMountPeripheral(
"cbc_cannon_mount",
be as CannonMountBlockEntity
)
}
}
}
if (CLOCKWORK_ACTIVE) {
peripheralMap[ClockworkBlocks.FLAP_BEARING.get()] =
PeripheralSupplier { be: BlockEntity, _: Level, _: BlockPos ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral
import dan200.computercraft.api.lua.LuaFunction
import rbasamoyai.createbigcannons.cannon_control.cannon_mount.CannonMountBlockEntity

class CheatCannonMountPeripheral(peripheralType: String, tileEntity: CannonMountBlockEntity) : CannonMountPeripheral(peripheralType,
open class CheatCannonMountPeripheral(peripheralType: String, tileEntity: CannonMountBlockEntity) : CannonMountPeripheral(peripheralType,
tileEntity
) {
@LuaFunction(mainThread = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral

import dan200.computercraft.api.lua.LuaFunction
import io.github.xiewuzhiying.vs_addition.mixinducks.vs_clockwork.flap_bearing.FlapBearingBlockEntityMixinDuck
import org.valkyrienskies.clockwork.content.contraptions.flap.FlapBearingBlockEntity

class CheatFlapBearingPeripheral(peripheralType: String, tileEntity: FlapBearingBlockEntity) : FlapBearingPeripheral(peripheralType,
tileEntity
) {
@LuaFunction(mainThread = true)
fun setAngle(angle: Double): Any {
override fun setAngle(angle: Double): Boolean {
if (tileEntity.isRunning) {
(this.tileEntity as FlapBearingBlockEntityMixinDuck).lockedFlapAngle = angle.toFloat()
tileEntity.setAngle(angle.toFloat())
(this.tileEntity as FlapBearingBlockEntityMixinDuck).isLocked = true
return true
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral
import dan200.computercraft.api.lua.LuaFunction
import dan200.computercraft.api.peripheral.IPeripheral
import io.github.xiewuzhiying.vs_addition.mixin.vs_clockwork.flap_bearing.FlapBearingBlockEntityAccessor
import io.github.xiewuzhiying.vs_addition.mixinducks.vs_clockwork.flap_bearing.FlapBearingBlockEntityMixinDuck
import org.valkyrienskies.clockwork.content.contraptions.flap.FlapBearingBlockEntity

open class FlapBearingPeripheral(val peripheralType: String, val tileEntity: FlapBearingBlockEntity) : IPeripheral {
Expand Down Expand Up @@ -36,8 +37,27 @@ open class FlapBearingPeripheral(val peripheralType: String, val tileEntity: Fla
return false
}

@LuaFunction(mainThread = true)
open fun setAngle(angle: Double): Boolean {
if (tileEntity.isRunning) {
(this.tileEntity as FlapBearingBlockEntityMixinDuck).lockedFlapAngle = angle.toFloat()
(this.tileEntity as FlapBearingBlockEntityMixinDuck).isLocked = true
return true
}
return false
}

@LuaFunction(mainThread = true)
fun unlock() : Any {
if (tileEntity.isRunning) {
(this.tileEntity as FlapBearingBlockEntityMixinDuck).isLocked = false
return true
}
return false
}

@LuaFunction
fun getAngle(): Any {
return (tileEntity as FlapBearingBlockEntityAccessor).bearingAngle
fun getAngle(): Double {
return (tileEntity as FlapBearingBlockEntityAccessor).bearingAngle.toDouble()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.xiewuzhiying.vs_addition.fabric.mixin.computercraft;

import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.peripheral.generic.methods.InventoryMethods;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.SlottedStorage;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(InventoryMethods.class)
public interface InventoryMethodsAccessor {
@Invoker(value = "moveItem", remap = false)
static int moveItem(SlottedStorage<ItemVariant> from, int fromSlot, SlottedStorage<ItemVariant> to, int toSlot, final int limit) {
throw new AssertionError();
}
@Invoker(value = "toStack", remap = false)
static ItemStack toStack(SingleSlotStorage<ItemVariant> variant) {
throw new AssertionError();
}
@Invoker(value = "extractHandler", remap = false)
static SlottedStorage<ItemVariant> extractHandler(IPeripheral peripheral) {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import net.minecraft.world.level.Level

object FabricPeripheralLookup {
fun peripheralProvider(level: Level, blockPos: BlockPos): IPeripheral? {
return PeripheralCommon.getPeripheralCommon(level, blockPos)
return PeripheralCommon.getPeripheralCommon(level, blockPos) ?: PeripheralFabric.getPeripheralFarbic(level, blockPos)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.xiewuzhiying.vs_addition.fabric.compats.computercraft

import dan200.computercraft.api.peripheral.IPeripheral
import io.github.xiewuzhiying.vs_addition.VSAdditionConfig
import io.github.xiewuzhiying.vs_addition.VSAdditionMod
import io.github.xiewuzhiying.vs_addition.compats.computercraft.PeripheralCommon.PeripheralSupplier
import io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral.CannonMountPeripheral
import io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral.CheatCannonMountPeripheral
import net.minecraft.core.BlockPos
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.entity.BlockEntity
import rbasamoyai.createbigcannons.cannon_control.cannon_mount.CannonMountBlockEntity
import rbasamoyai.createbigcannons.index.CBCBlocks

object PeripheralFabric {
private val peripheralMap: MutableMap<Block, PeripheralSupplier> = HashMap()

init {
if (VSAdditionMod.CBC_ACTIVE) {
peripheralMap[CBCBlocks.CANNON_MOUNT.get()] =
PeripheralSupplier { be: BlockEntity, _: Level, _: BlockPos ->
if (VSAdditionConfig.SERVER.computercraft.enableCheatCannonMountPeripheral) {
CheatCannonMountPeripheral(
"cbc_cannon_mount",
be as CannonMountBlockEntity
)
} else {
CannonMountPeripheral(
"cbc_cannon_mount",
be as CannonMountBlockEntity
)
}
}
}
}

fun getPeripheralFarbic(level: Level, blockPos: BlockPos): IPeripheral? {
val s = level.getBlockState(blockPos)
val be = level.getBlockEntity(blockPos)
val supplier = peripheralMap[s.block]
return be?.let { supplier?.get(it, level, blockPos) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.xiewuzhiying.vs_addition.fabric.compats.computercraft.peripheral

import dan200.computercraft.api.detail.VanillaDetailRegistries
import dan200.computercraft.api.lua.LuaException
import dan200.computercraft.api.lua.LuaFunction
import dan200.computercraft.api.peripheral.IComputerAccess
import dan200.computercraft.core.util.ArgumentHelpers
import dan200.computercraft.shared.peripheral.generic.methods.InventoryMethods.StorageWrapper
import io.github.xiewuzhiying.vs_addition.compats.computercraft.peripheral.CannonMountPeripheral
import io.github.xiewuzhiying.vs_addition.fabric.mixin.computercraft.InventoryMethodsAccessor
import rbasamoyai.createbigcannons.cannon_control.cannon_mount.CannonMountBlockEntity
import java.util.*

class CannonMountPeripheralWithInventory(peripheralType: String, tileEntity: CannonMountBlockEntity, val inventory: StorageWrapper) : CannonMountPeripheral(peripheralType,
tileEntity
){
@LuaFunction(mainThread = true)
fun size(): Int {
return inventory.storage.slots.size
}

@LuaFunction(mainThread = true)
fun list(): Map<Int, Map<String, *>> {
val result: MutableMap<Int, Map<String, *>> = HashMap()
val slots = inventory.storage.slots
val size = slots.size
for (i in 0 until size) {
val stack = InventoryMethodsAccessor.toStack(slots[i])
if (!stack.isEmpty) result[i + 1] = VanillaDetailRegistries.ITEM_STACK.getBasicDetails(stack)
}

return result
}

@LuaFunction(mainThread = true)
@Throws(LuaException::class)
fun getItemDetail(slot: Int): Map<String, *>? {
ArgumentHelpers.assertBetween(slot, 1, inventory.storage.slotCount, "Slot out of range (%s)")

val stack = InventoryMethodsAccessor.toStack(inventory.storage.getSlot(slot - 1))
return if (stack.isEmpty) null else VanillaDetailRegistries.ITEM_STACK.getDetails(stack)
}

@LuaFunction(mainThread = true)
@Throws(LuaException::class)
fun getItemLimit(slot: Int): Long {
ArgumentHelpers.assertBetween(slot, 1, inventory.storage.slotCount, "Slot out of range (%s)")
return inventory.storage.getSlot(slot - 1).capacity
}

@LuaFunction(mainThread = true)
@Throws(LuaException::class)
fun pushItems(
computer: IComputerAccess,
toName: String, fromSlot: Int, limit: Optional<Int>, toSlot: Optional<Int>
): Int {
// Find location to transfer to
val location = computer.getAvailablePeripheral(toName)
?: throw LuaException("Target '$toName' does not exist")

val to = InventoryMethodsAccessor.extractHandler(location)
?: throw LuaException("Target '$toName' is not an inventory")

val fromStorage = inventory.storage

// Validate slots
val actualLimit = limit.orElse(Int.MAX_VALUE)
ArgumentHelpers.assertBetween(fromSlot, 1, fromStorage.slotCount, "From slot out of range (%s)")
if (toSlot.isPresent) ArgumentHelpers.assertBetween(toSlot.get(), 1, to.slots.size, "To slot out of range (%s)")

if (actualLimit <= 0) return 0
return InventoryMethodsAccessor.moveItem(fromStorage, fromSlot - 1, to, toSlot.orElse(0) - 1, actualLimit)
}

@LuaFunction(mainThread = true)
@Throws(LuaException::class)
fun pullItems(
computer: IComputerAccess,
fromName: String, fromSlot: Int, limit: Optional<Int>, toSlot: Optional<Int>
): Int {
// Find location to transfer to
val location = computer.getAvailablePeripheral(fromName)
?: throw LuaException("Source '$fromName' does not exist")

val toStorage = inventory.storage

val from = InventoryMethodsAccessor.extractHandler(location)
?: throw LuaException("Source '$fromName' is not an inventory")

// Validate slots
val actualLimit = limit.orElse(Int.MAX_VALUE)
ArgumentHelpers.assertBetween(fromSlot, 1, from.slots.size, "From slot out of range (%s)")
if (toSlot.isPresent) ArgumentHelpers.assertBetween(
toSlot.get(),
1,
toStorage.slotCount,
"To slot out of range (%s)"
)

if (actualLimit <= 0) return 0
return InventoryMethodsAccessor.moveItem(from, fromSlot - 1, toStorage, toSlot.orElse(0) - 1, actualLimit)
}
}
Loading

0 comments on commit b2e1e7c

Please sign in to comment.