Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.97 KB | None | 0 0
  1.  class TileEntityConveyorBeltInput : TileEntity(), ITickable {
  2.  
  3.         val inventory = ItemStackHandler(9)
  4.  
  5.         override fun update() {
  6.             if (!world.isRemote) {
  7.                 val facing = world.getBlockState(pos).properties[BlockHorizontal.FACING] as EnumFacing
  8.                 val block = world.getBlockState(pos.offset(facing.opposite)).block
  9.  
  10.                 if (block == conveyorBelt) {
  11.                     var slotToOutput: ItemStack = ItemStack.EMPTY
  12.                     var slotId = -1
  13.                     for (i in 0..inventory.slots - 1) {
  14.                         if (!inventory.getStackInSlot(i).isEmpty) {
  15.                             slotToOutput = inventory.getStackInSlot(i)
  16.                             slotId = i
  17.                             break
  18.                         }
  19.                     }
  20.                     if (!slotToOutput.isEmpty) {
  21.                         val copy = slotToOutput.copy()
  22.                         copy.count = 1
  23.  
  24.                         val pPL = getPlacePosition(true, facing)
  25.                         val pPR = getPlacePosition(false, facing)
  26.  
  27.                         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))
  28.                         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))
  29.  
  30.                         var worked: Boolean = false
  31.                         var left: Boolean = false
  32.  
  33.                         if (entitiesL.isEmpty()) {
  34.                             left = true
  35.                             worked = true
  36.                         } else if (entitiesR.isEmpty()) {
  37.                             left = false
  38.                             worked = true
  39.                         }
  40.                         if (worked) {
  41.                             val pP = getPlacePosition(left, facing)
  42.                             world.spawnEntity(EntityConveyorBeltItem(copy, world, pP.x, pP.y, pP.z))
  43.                             slotToOutput.count--
  44.                             if (slotToOutput.count == 0) inventory.setStackInSlot(slotId, ItemStack.EMPTY)
  45.                         }
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.  
  51.         private fun getPlacePosition(left: Boolean, facing: EnumFacing): Point3d {
  52.             var pX = pos.offset(facing.opposite).x + 0.0
  53.             var pY = pos.offset(facing.opposite).y + 0.0
  54.             var pZ = pos.offset(facing.opposite).z + 0.0
  55.  
  56.             when (facing) {
  57.                 EnumFacing.NORTH -> {
  58.                     pX += 0.5 + if (left) 0.25 else -0.25; pZ += 0.25f
  59.                 }
  60.                 EnumFacing.SOUTH -> {
  61.                     pX += 0.5 - if (left) 0.25 else -0.25; pZ += 0.75f
  62.                 }
  63.                 EnumFacing.WEST -> pX
  64.                 EnumFacing.EAST -> pX
  65.                 else -> {
  66.  
  67.                 }
  68.             }
  69.             return Point3d(pX, pY, pZ)
  70.         }
  71.  
  72.         override fun hasCapability(capability: Capability<*>, facing: EnumFacing?): Boolean {
  73.             return (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) || super.hasCapability(capability, facing)
  74.         }
  75.  
  76.         override fun <T : Any?> getCapability(capability: Capability<T>, facing: EnumFacing?): T? {
  77.             return if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(inventory) else super.getCapability(capability, facing)
  78.         }
  79.  
  80.         override fun writeToNBT(compound: NBTTagCompound): NBTTagCompound {
  81.             compound.setTag("inventory", inventory.serializeNBT())
  82.             return super.writeToNBT(compound)
  83.         }
  84.  
  85.         override fun readFromNBT(compound: NBTTagCompound) {
  86.             inventory.deserializeNBT(compound.getTag("inventory") as NBTTagCompound)
  87.             super.readFromNBT(compound)
  88.         }
  89.  
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement