mirror of
https://github.com/GTNewHorizons/bdlib.git
synced 2026-11-12 08:17:37 +00:00
Added Misc.forgeDirection, Misc.asInstanceOpt, Misc.getNeighbourTile and ItemUtils.getAccessibleSlotsFromSide helpers
This commit is contained in:
1 parent
0b87dd83e7
commit
e41e203b7e
2 files changed
+21
-1
No files matched your search
@@ -17,6 +17,8 @@ import cpw.mods.fml.common.versioning.{ArtifactVersion, VersionParser}
|
||||
import cpw.mods.fml.common.Loader
|
||||
import net.minecraftforge.oredict.ShapedOreRecipe
|
||||
import net.minecraft.world.biome.BiomeGenBase
|
||||
import net.minecraftforge.common.ForgeDirection
|
||||
import net.minecraft.tileentity.TileEntity
|
||||
|
||||
object Misc {
|
||||
def iterNbtList[X](list: NBTTagList) =
|
||||
@@ -69,4 +71,16 @@ object Misc {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Because writing this every time is awkward
|
||||
def forgeDirection(i: Int) = ForgeDirection.values()(i)
|
||||
|
||||
// Easy replacement for various "if(foo.isInstanceOf[Bar]) foo.asInstanceOf[Bar]" constructs
|
||||
def asInstanceOpt[T](v: Any, cls: Class[T]) =
|
||||
if (cls.isInstance(v)) Some(v.asInstanceOf[T]) else None
|
||||
|
||||
def getNeighbourTile[T](origin: TileEntity, dir: ForgeDirection, cls: Class[T]) =
|
||||
Option(origin.worldObj.getBlockTileEntity(origin.xCoord + dir.offsetX,
|
||||
origin.yCoord + dir.offsetY, origin.zCoord + dir.offsetZ)) flatMap (asInstanceOpt(_, cls))
|
||||
}
|
||||
|
||||
@@ -13,9 +13,10 @@ import net.minecraft.entity.item.EntityItem
|
||||
import net.minecraft.item.{Item, ItemStack}
|
||||
import net.minecraft.world.World
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import net.minecraft.inventory.IInventory
|
||||
import net.minecraft.inventory.{ISidedInventory, IInventory}
|
||||
import net.bdew.lib.Misc
|
||||
import java.util.Random
|
||||
import net.minecraftforge.common.ForgeDirection
|
||||
|
||||
object ItemUtils {
|
||||
def throwItemAt(world: World, x: Int, y: Int, z: Int, stack: ItemStack) {
|
||||
@@ -78,6 +79,11 @@ object ItemUtils {
|
||||
return stack
|
||||
}
|
||||
|
||||
def getAccessibleSlotsFromSide(inv: IInventory, side: ForgeDirection) =
|
||||
(Misc.asInstanceOpt(inv, classOf[ISidedInventory])
|
||||
map (_.getAccessibleSlotsFromSide(side.ordinal()).toList)
|
||||
getOrElse (0 until inv.getSizeInventory))
|
||||
|
||||
def findItemInInventory(inv: IInventory, item: Item) =
|
||||
Range(0, inv.getSizeInventory).map(x => x -> inv.getStackInSlot(x))
|
||||
.find({ case (slot, stack) => stack != null && stack.getItem == item })
|
||||
|
||||
Reference in new issue
Block a user