From 5bac374211ba23edb0bdc4c036fb32d6a1ad1635 Mon Sep 17 00:00:00 2001 From: bdew Date: Sun, 1 Jun 2014 15:07:54 +0300 Subject: [PATCH] Added cover upgrades system --- src/net/bdew/lib/covers/BlockCoverable.scala | 65 ++++++++++++++++++++ src/net/bdew/lib/covers/DataslotCover.scala | 35 +++++++++++ src/net/bdew/lib/covers/ItemCover.scala | 31 ++++++++++ src/net/bdew/lib/covers/TileCoverable.scala | 33 ++++++++++ 4 files changed, 164 insertions(+) create mode 100644 src/net/bdew/lib/covers/BlockCoverable.scala create mode 100644 src/net/bdew/lib/covers/DataslotCover.scala create mode 100644 src/net/bdew/lib/covers/ItemCover.scala create mode 100644 src/net/bdew/lib/covers/TileCoverable.scala diff --git a/src/net/bdew/lib/covers/BlockCoverable.scala b/src/net/bdew/lib/covers/BlockCoverable.scala new file mode 100644 index 0000000..581b489 --- /dev/null +++ b/src/net/bdew/lib/covers/BlockCoverable.scala @@ -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) + } +} diff --git a/src/net/bdew/lib/covers/DataslotCover.scala b/src/net/bdew/lib/covers/DataslotCover.scala new file mode 100644 index 0000000..640a63e --- /dev/null +++ b/src/net/bdew/lib/covers/DataslotCover.scala @@ -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) +} diff --git a/src/net/bdew/lib/covers/ItemCover.scala b/src/net/bdew/lib/covers/ItemCover.scala new file mode 100644 index 0000000..033bd97 --- /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.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 +} diff --git a/src/net/bdew/lib/covers/TileCoverable.scala b/src/net/bdew/lib/covers/TileCoverable.scala new file mode 100644 index 0000000..fae050b --- /dev/null +++ b/src/net/bdew/lib/covers/TileCoverable.scala @@ -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) +}