Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TileEntityConveyorBeltInput : TileEntity(), ITickable {
- val inventory = ItemStackHandler(9)
- override fun update() {
- if (!world.isRemote) {
- val facing = world.getBlockState(pos).properties[BlockHorizontal.FACING] as EnumFacing
- val block = world.getBlockState(pos.offset(facing.opposite)).block
- if (block == conveyorBelt) {
- var slotToOutput: ItemStack = ItemStack.EMPTY
- var slotId = -1
- for (i in 0..inventory.slots - 1) {
- if (!inventory.getStackInSlot(i).isEmpty) {
- slotToOutput = inventory.getStackInSlot(i)
- slotId = i
- break
- }
- }
- if (!slotToOutput.isEmpty) {
- val copy = slotToOutput.copy()
- copy.count = 1
- val pPL = getPlacePosition(true, facing)
- val pPR = getPlacePosition(false, facing)
- val entitiesL = world.getEntitiesWithinAABB(EntityConveyorBeltItem::class.java, AxisAlignedBB(pPL.x, pPL.y, pPL.z, pPL.x + 0.1, pPL.y + 0.2, pPL.z + 0.1))
- val entitiesR = world.getEntitiesWithinAABB(EntityConveyorBeltItem::class.java, AxisAlignedBB(pPR.x, pPR.y, pPR.z, pPR.x + 0.2, pPR.y + 0.05, pPR.z + 0.2))
- var worked: Boolean = false
- var left: Boolean = false
- if (entitiesL.isEmpty()) {
- left = true
- worked = true
- } else if (entitiesR.isEmpty()) {
- left = false
- worked = true
- }
- if (worked) {
- val pP = getPlacePosition(left, facing)
- world.spawnEntity(EntityConveyorBeltItem(copy, world, pP.x, pP.y, pP.z))
- slotToOutput.count--
- if (slotToOutput.count == 0) inventory.setStackInSlot(slotId, ItemStack.EMPTY)
- }
- }
- }
- }
- }
- private fun getPlacePosition(left: Boolean, facing: EnumFacing): Point3d {
- var pX = pos.offset(facing.opposite).x + 0.0
- var pY = pos.offset(facing.opposite).y + 0.0
- var pZ = pos.offset(facing.opposite).z + 0.0
- when (facing) {
- EnumFacing.NORTH -> {
- pX += 0.5 + if (left) 0.25 else -0.25; pZ += 0.25f
- }
- EnumFacing.SOUTH -> {
- pX += 0.5 - if (left) 0.25 else -0.25; pZ += 0.75f
- }
- EnumFacing.WEST -> pX
- EnumFacing.EAST -> pX
- else -> {
- }
- }
- return Point3d(pX, pY, pZ)
- }
- override fun hasCapability(capability: Capability<*>, facing: EnumFacing?): Boolean {
- return (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) || super.hasCapability(capability, facing)
- }
- override fun <T : Any?> getCapability(capability: Capability<T>, facing: EnumFacing?): T? {
- return if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(inventory) else super.getCapability(capability, facing)
- }
- override fun writeToNBT(compound: NBTTagCompound): NBTTagCompound {
- compound.setTag("inventory", inventory.serializeNBT())
- return super.writeToNBT(compound)
- }
- override fun readFromNBT(compound: NBTTagCompound) {
- inventory.deserializeNBT(compound.getTag("inventory") as NBTTagCompound)
- super.readFromNBT(compound)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement