Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ dependencies {
modImplementation "net.fabricmc:fabric-language-kotlin:${project.fabric_kotlin_version}+kotlin.${project.kotlin_version}"

include(modImplementation("io.github.cottonmc:LibGui:${project.libgui_version}"))

// Include Cooldown Coordinator 0.3.0 via cursemaven
include(modImplementation("curse.maven:cooldown-coordinator-594072:3757479"))

//implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import net.fabricmc.fabric.api.transfer.v1.item.InventoryStorage
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant
import net.fabricmc.fabric.api.transfer.v1.storage.StorageUtil
import net.gnomecraft.cooldowncoordinator.*
import net.minecraft.block.BlockState
import net.minecraft.block.entity.HopperBlockEntity
import net.minecraft.block.entity.LockableContainerBlockEntity
Expand All @@ -31,12 +32,13 @@ import net.minecraft.world.World
class DuctBlockEntity(
pos : BlockPos, state: BlockState,private val inventory: Inventory = SimpleInventory(1))
: LockableContainerBlockEntity(type,pos,state), Inventory by inventory,
ExtendedScreenHandlerFactory {
ExtendedScreenHandlerFactory, CoordinatedCooldown {

init {
instance = this
}

private var lastTickTime: Long = Long.MAX_VALUE
var transferCooldown: Int = -1

override fun getContainerName(): Text {
Expand All @@ -60,18 +62,31 @@ class DuctBlockEntity(
val outputDir = blockEntity.cachedState[DuctBlock.Props.output]
val outputInv = HopperBlockEntity.getInventoryAt(world, pos?.offset(outputDir))

if(outputInv != null){
val targetEntity = world!!.getBlockEntity(pos!!.offset(outputDir))
val targetWasEmpty: Boolean
val transferSucceeded: Boolean

if (outputInv != null) {
val stackCopy = blockEntity.getStack(0).copy()
val ret = HopperBlockEntity.transfer(blockEntity, outputInv, blockEntity.removeStack(0, 1), outputDir.opposite)
if (ret.isEmpty) {
outputInv.markDirty()
return true
targetWasEmpty = CooldownCoordinator.isInventoryEmpty(outputInv)
transferSucceeded = HopperBlockEntity.transfer(blockEntity, outputInv, blockEntity.removeStack(0, 1), outputDir.opposite).isEmpty
if (!transferSucceeded) {
blockEntity.setStack(0, stackCopy)
}
blockEntity.setStack(0, stackCopy)
} else {
val target = ItemStorage.SIDED.find(world, pos?.offset(outputDir),outputDir) ?: return false
return StorageUtil.move(InventoryStorage.of(blockEntity.inventory, outputDir), target, { iv: ItemVariant? -> true }, 1, null) > 0
val target = ItemStorage.SIDED.find(world, pos?.offset(outputDir),outputDir.opposite) ?: return false
targetWasEmpty = CooldownCoordinator.isStorageEmpty(target)
transferSucceeded = StorageUtil.move(InventoryStorage.of(blockEntity.inventory, outputDir), target, { iv: ItemVariant? -> true }, 1, null) > 0
}

if (transferSucceeded) {
if (targetWasEmpty) {
CooldownCoordinator.notify(targetEntity)
}
targetEntity?.markDirty()
return true
}

return false
}

Expand All @@ -80,6 +95,7 @@ class DuctBlockEntity(
if(world.isClient) return

blockEntity!!.transferCooldown--
blockEntity.lastTickTime = world.time

if (blockEntity.transferCooldown > 0) return

Expand Down Expand Up @@ -112,4 +128,14 @@ class DuctBlockEntity(
val type = FabricBlockEntityTypeBuilder.create(::DuctBlockEntity, Ducts.DUCT_BLOCK).build(null)!!
@JvmStatic lateinit var instance: DuctBlockEntity
}

override fun notifyCooldown() {
val worldTime = this.world?.time ?: Long.MAX_VALUE
if (this.lastTickTime >= worldTime) {
this.transferCooldown = 7
} else {
this.transferCooldown = 8
}
super.markDirty()
}
}