Reworked cover system to store full itemstack of the cover (for stuff like filter covers to have a place to store data)

This commit is contained in:
bdew committed 2014-06-01 16:53:51 +03:00
1 parent 5bac374211
commit da3e58b44b
4 files changed
+35 -57

No files matched your search

+16 -13
View File
@@ -14,7 +14,6 @@ import net.minecraft.world.{IBlockAccess, World}
import net.minecraft.entity.player.EntityPlayer
import net.bdew.lib.Misc
import net.bdew.lib.items.ItemUtils
import net.minecraft.item.ItemStack
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 = {
@@ -22,11 +21,11 @@ trait BlockCoverable[T <: TileCoverable] extends HasTE[T] {
val side = Misc.forgeDirection(face)
if (player.getCurrentEquippedItem == null && player.isSneaking) {
for (cover <- te.covers(side)) {
for (cover <- Option(te.covers(side).cval)) {
if (!world.isRemote) {
te.covers(side) := None
te.covers(side) := null
te.onCoversChanged()
ItemUtils.dropItemToPlayer(world, player, new ItemStack(cover))
ItemUtils.dropItemToPlayer(world, player, cover)
}
return true
}
@@ -36,14 +35,15 @@ trait BlockCoverable[T <: TileCoverable] extends HasTE[T] {
activeStack <- Option(player.getCurrentEquippedItem)
activeItem <- Option(activeStack.getItem)
coverItem <- Misc.asInstanceOpt(activeItem, classOf[ItemCover])
if te.isValidCover(side, coverItem) && coverItem.isValidTile(te)
if te.isValidCover(side, activeStack) && coverItem.isValidTile(te, activeStack)
} {
if (!world.isRemote) {
for (cover <- te.covers(side))
ItemUtils.dropItemToPlayer(world, player, new ItemStack(cover))
for (cover <- Option(te.covers(side).cval))
ItemUtils.throwItemAt(world, te.xCoord + side.offsetX, te.yCoord + side.offsetY, te.zCoord + side.offsetZ, cover.copy())
player.inventory.decrStackSize(player.inventory.currentItem, 1)
te.covers(side) := coverItem
te.covers(side) := activeStack.splitStack(1)
if (activeStack.stackSize <= 0) player.inventory.setInventorySlotContents(player.inventory.currentItem, null)
te.onCoversChanged()
}
return true
@@ -53,13 +53,16 @@ trait BlockCoverable[T <: TileCoverable] extends HasTE[T] {
}
override def getBlockTexture(w: IBlockAccess, x: Int, y: Int, z: Int, side: Int) =
getTE(w, x, y, z).covers(Misc.forgeDirection(side)).cval map (_.getCoverIcon) getOrElse
super.getBlockTexture(w, x, y, z, side)
(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 <- covOpt)
ItemUtils.throwItemAt(world, x, y, z, new ItemStack(cover))
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)
}
}
@@ -1,35 +0,0 @@
/*
* 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.Item
import net.bdew.lib.data.base.{DataSlotVal, UpdateKind}
import net.minecraft.nbt.NBTTagCompound
import net.bdew.lib.BdLib
case class DataslotCover(name: String, parent: TileCoverable) extends DataSlotVal[Option[ItemCover]] {
var cval: Option[ItemCover] = None
setUpdate(UpdateKind.SAVE, UpdateKind.WORLD, UpdateKind.RENDER)
def :=(v: ItemCover) = update(Option(v))
override def load(t: NBTTagCompound, kind: UpdateKind.Value) = {
try {
cval = Option(Item.itemsList(t.getInteger(name)).asInstanceOf[ItemCover])
} catch {
case t: Throwable => BdLib.logError("Failed to load cover %s for block at %d, %d, %d: %s", name, parent.xCoord, parent.yCoord, parent.zCoord, t.toString)
t.printStackTrace()
}
}
override def save(t: NBTTagCompound, kind: UpdateKind.Value) =
for (v <- cval) t.setInteger(name, v.itemID)
}
+4 -4
View File
@@ -9,7 +9,7 @@
package net.bdew.lib.covers
import net.minecraft.item.Item
import net.minecraft.item.{ItemStack, Item}
import net.minecraftforge.common.ForgeDirection
import net.minecraft.util.Icon
@@ -17,15 +17,15 @@ trait ItemCover extends Item {
/**
* @return Icon to render, MUST be on terrain spritesheet (0), not item!
*/
def getCoverIcon: Icon
def getCoverIcon(stack: ItemStack): Icon
/**
* Perform tick, called on server only
*/
def tickCover(te: TileCoverable, side: ForgeDirection)
def tickCover(te: TileCoverable, side: ForgeDirection, cover: ItemStack)
/**
* Checks if this cover can be installed on a specific TE
*/
def isValidTile(te: TileCoverable): Boolean
def isValidTile(te: TileCoverable, cover: ItemStack): Boolean
}
+15 -5
View File
@@ -9,16 +9,21 @@
package net.bdew.lib.covers
import net.bdew.lib.data.base.TileDataSlots
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 -> DataslotCover("cv_" + x.toString.toLowerCase, this) }).toMap
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: ItemCover): Boolean
def isValidCover(side: ForgeDirection, cover: ItemStack): Boolean
/**
* Called when new covers are installed
@@ -26,8 +31,13 @@ trait TileCoverable extends TileDataSlots {
def onCoversChanged() {}
def tickCovers() =
for ((dir, covopt) <- covers; cover <- covopt.cval)
cover.tickCover(this, dir)
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)
}