Added cover upgrades system

This commit is contained in:
bdew committed 2014-06-01 15:07:54 +03:00
1 parent e41e203b7e
commit 5bac374211
4 files changed
+164

No files matched your search

@@ -0,0 +1,65 @@
/*
* 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
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 = {
val te = getTE(world, x, y, z)
val side = Misc.forgeDirection(face)
if (player.getCurrentEquippedItem == null && player.isSneaking) {
for (cover <- te.covers(side)) {
if (!world.isRemote) {
te.covers(side) := None
te.onCoversChanged()
ItemUtils.dropItemToPlayer(world, player, new ItemStack(cover))
}
return true
}
}
for {
activeStack <- Option(player.getCurrentEquippedItem)
activeItem <- Option(activeStack.getItem)
coverItem <- Misc.asInstanceOpt(activeItem, classOf[ItemCover])
if te.isValidCover(side, coverItem) && coverItem.isValidTile(te)
} {
if (!world.isRemote) {
for (cover <- te.covers(side))
ItemUtils.dropItemToPlayer(world, player, new ItemStack(cover))
player.inventory.decrStackSize(player.inventory.currentItem, 1)
te.covers(side) := coverItem
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) =
getTE(w, x, y, z).covers(Misc.forgeDirection(side)).cval map (_.getCoverIcon) 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))
super.breakBlock(world, x, y, z, blockId, meta)
}
}
@@ -0,0 +1,35 @@
/*
* 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)
}
+31
View File
@@ -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.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: Icon
/**
* Perform tick, called on server only
*/
def tickCover(te: TileCoverable, side: ForgeDirection)
/**
* Checks if this cover can be installed on a specific TE
*/
def isValidTile(te: TileCoverable): Boolean
}
@@ -0,0 +1,33 @@
/*
* 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.TileDataSlots
import net.minecraftforge.common.ForgeDirection
trait TileCoverable extends TileDataSlots {
val covers = (ForgeDirection.VALID_DIRECTIONS map { x => x -> DataslotCover("cv_" + x.toString.toLowerCase, this) }).toMap
/**
* Checks if a specific cover can be installed here
*/
def isValidCover(side: ForgeDirection, cover: ItemCover): Boolean
/**
* Called when new covers are installed
*/
def onCoversChanged() {}
def tickCovers() =
for ((dir, covopt) <- covers; cover <- covopt.cval)
cover.tickCover(this, dir)
serverTick.listen(tickCovers)
}