diff --git a/.gitignore b/.gitignore index 08c22bc..004dfe6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ -/.gradle -/build -/deps -/eclipse +/bin /out - -*.ipr -*.iml -*.iws +/build +/run +/deps/run +/eclipse +/.gradle +/*.iml +/*.ipr +/.idea diff --git a/build.gradle b/build.gradle index e77fb4c..0cfc9eb 100644 --- a/build.gradle +++ b/build.gradle @@ -34,7 +34,7 @@ archivesBaseName = "bdlib" ext.versuffix = "mc172" minecraft { - version = "1.7.2-10.12.0.1057" + version = "1.7.2-10.12.2.1126" replace("BDLIB_VER", project.version) } diff --git a/src/net/bdew/lib/Misc.scala b/src/net/bdew/lib/Misc.scala index b355970..1fb765f 100644 --- a/src/net/bdew/lib/Misc.scala +++ b/src/net/bdew/lib/Misc.scala @@ -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.minecraft.tileentity.TileEntity +import net.minecraftforge.common.util.ForgeDirection object Misc { def iterNbtCompoundList(parent: NBTTagCompound, name: String): Iterable[NBTTagCompound] = { @@ -57,7 +59,7 @@ object Misc { def filterType[T](from: Iterable[_], cls: Class[T]) = from.filter(cls.isInstance).asInstanceOf[Iterable[T]] - def getBiomeByName(name: String) = BiomeGenBase.getBiomeGenArray.find(_.biomeName == name).getOrElse(null) + def getBiomeByName(name: String) = BiomeGenBase.getBiomeGenArray.find(x => x != null && x.biomeName == name).getOrElse(null) def haveModVersion(spec: String): Boolean = { val req = VersionParser.parseVersionReference(spec) @@ -71,4 +73,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.getWorldObj.getBlockTileEntity(origin.xCoord + dir.offsetX, + origin.yCoord + dir.offsetY, origin.zCoord + dir.offsetZ)) flatMap (asInstanceOpt(_, cls)) } + diff --git a/src/net/bdew/lib/config/BlockManager.scala b/src/net/bdew/lib/config/BlockManager.scala index 1d92152..8947b8f 100644 --- a/src/net/bdew/lib/config/BlockManager.scala +++ b/src/net/bdew/lib/config/BlockManager.scala @@ -13,6 +13,7 @@ import net.minecraft.item.ItemStack import net.minecraft.block.Block import cpw.mods.fml.common.registry.GameRegistry import net.bdew.lib.block.HasTE +import net.bdew.lib.Misc class BlockManager() { @@ -28,7 +29,8 @@ class BlockManager() { GameRegistry.registerCustomItemStack(name, new ItemStack(block)) if (block.isInstanceOf[HasTE[_]]) - GameRegistry.registerTileEntity(block.asInstanceOf[HasTE[_]].TEClass, name) + GameRegistry.registerTileEntity(block.asInstanceOf[HasTE[_]].TEClass, + "%s.%s".format(Misc.getActiveModId, name)) return block } diff --git a/src/net/bdew/lib/covers/BlockCoverable.scala b/src/net/bdew/lib/covers/BlockCoverable.scala new file mode 100644 index 0000000..ef1b8b2 --- /dev/null +++ b/src/net/bdew/lib/covers/BlockCoverable.scala @@ -0,0 +1,68 @@ +/* + * Copyright (c) bdew, 2013 - 2014 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.covers + +import net.bdew.lib.block.HasTE +import net.minecraft.world.{IBlockAccess, World} +import net.minecraft.entity.player.EntityPlayer +import net.bdew.lib.Misc +import net.bdew.lib.items.ItemUtils + +trait BlockCoverable[T <: TileCoverable] extends HasTE[T] { + override def onBlockActivated(world: World, x: Int, y: Int, z: Int, player: EntityPlayer, face: Int, xoffs: Float, yoffs: Float, zoffs: Float): Boolean = { + val te = getTE(world, x, y, z) + val side = Misc.forgeDirection(face) + + if (player.getCurrentEquippedItem == null && player.isSneaking) { + for (cover <- Option(te.covers(side).cval)) { + if (!world.isRemote) { + te.covers(side) := null + te.onCoversChanged() + ItemUtils.dropItemToPlayer(world, player, cover) + } + return true + } + } + + for { + activeStack <- Option(player.getCurrentEquippedItem) + activeItem <- Option(activeStack.getItem) + coverItem <- Misc.asInstanceOpt(activeItem, classOf[ItemCover]) + if te.isValidCover(side, activeStack) && coverItem.isValidTile(te, activeStack) + } { + if (!world.isRemote) { + for (cover <- Option(te.covers(side).cval)) + ItemUtils.throwItemAt(world, te.xCoord + side.offsetX, te.yCoord + side.offsetY, te.zCoord + side.offsetZ, cover.copy()) + + te.covers(side) := activeStack.splitStack(1) + + if (activeStack.stackSize <= 0) player.inventory.setInventorySlotContents(player.inventory.currentItem, null) + te.onCoversChanged() + } + return true + } + + super.onBlockActivated(world, x, y, z, player, face, xoffs, yoffs, zoffs) + } + + override def getBlockTexture(w: IBlockAccess, x: Int, y: Int, z: Int, side: Int) = + (for { + coverStack <- Option(getTE(w, x, y, z).covers(Misc.forgeDirection(side)).cval) + coverItem <- Option(coverStack.getItem) flatMap (Misc.asInstanceOpt(_, classOf[ItemCover])) + } yield coverItem.getCoverIcon(coverStack) + ) getOrElse super.getBlockTexture(w, x, y, z, side) + + override def breakBlock(world: World, x: Int, y: Int, z: Int, blockId: Int, meta: Int) { + if (!world.isRemote) + for ((dir, covOpt) <- getTE(world, x, y, z).covers; cover <- Option(covOpt.cval)) + ItemUtils.throwItemAt(world, x, y, z, cover) + super.breakBlock(world, x, y, z, blockId, meta) + } +} diff --git a/src/net/bdew/lib/covers/ItemCover.scala b/src/net/bdew/lib/covers/ItemCover.scala new file mode 100644 index 0000000..b104467 --- /dev/null +++ b/src/net/bdew/lib/covers/ItemCover.scala @@ -0,0 +1,31 @@ +/* + * Copyright (c) bdew, 2013 - 2014 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.covers + +import net.minecraft.item.{ItemStack, Item} +import net.minecraftforge.common.ForgeDirection +import net.minecraft.util.Icon + +trait ItemCover extends Item { + /** + * @return Icon to render, MUST be on terrain spritesheet (0), not item! + */ + def getCoverIcon(stack: ItemStack): Icon + + /** + * Perform tick, called on server only + */ + def tickCover(te: TileCoverable, side: ForgeDirection, cover: ItemStack) + + /** + * Checks if this cover can be installed on a specific TE + */ + def isValidTile(te: TileCoverable, cover: ItemStack): Boolean +} diff --git a/src/net/bdew/lib/covers/TileCoverable.scala b/src/net/bdew/lib/covers/TileCoverable.scala new file mode 100644 index 0000000..5f05d9f --- /dev/null +++ b/src/net/bdew/lib/covers/TileCoverable.scala @@ -0,0 +1,43 @@ +/* + * Copyright (c) bdew, 2013 - 2014 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.covers + +import net.bdew.lib.data.base.{UpdateKind, TileDataSlots} +import net.minecraftforge.common.ForgeDirection +import net.bdew.lib.data.DataSlotItemStack +import net.bdew.lib.Misc +import net.minecraft.item.ItemStack + +trait TileCoverable extends TileDataSlots { + val covers = (ForgeDirection.VALID_DIRECTIONS map { x => + x -> DataSlotItemStack("cover_" + x.toString.toLowerCase, this).setUpdate(UpdateKind.SAVE, UpdateKind.WORLD, UpdateKind.RENDER) + }).toMap + + /** + * Checks if a specific cover can be installed here + */ + def isValidCover(side: ForgeDirection, cover: ItemStack): Boolean + + /** + * Called when new covers are installed + */ + def onCoversChanged() {} + + def tickCovers() = + for { + (dir, coverSlot) <- covers + coverStack <- Option(coverSlot.cval) + coverItem <- Option(coverStack.getItem) flatMap (Misc.asInstanceOpt(_, classOf[ItemCover])) + } { + coverItem.tickCover(this, dir, coverStack) + } + + serverTick.listen(tickCovers) +} diff --git a/src/net/bdew/lib/items/ItemUtils.scala b/src/net/bdew/lib/items/ItemUtils.scala index 1223784..b9ce65b 100644 --- a/src/net/bdew/lib/items/ItemUtils.scala +++ b/src/net/bdew/lib/items/ItemUtils.scala @@ -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 })