Compare commits

..
11 Commits
13 changed files with 300 additions and 12 deletions

No files matched your search

+1 -1
View File
@@ -14,7 +14,7 @@ buildscript {
apply plugin: 'forge'
apply plugin: 'scala'
version = "1.2.1"
version = "1.2.3"
if (project.hasProperty('buildnum')) {
project.version = project.version + '.' + project.buildnum
+16 -1
View File
@@ -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) =
@@ -26,6 +28,7 @@ object Misc {
def toLocal(s: String) = StatCollector.translateToLocal(s)
def toLocalF(s: String, params: Any*) = StatCollector.translateToLocal(s).format(params: _*)
def hasLocal(s: String) = StatCollector.func_94522_b(s)
def flattenRecipe(pattern: Seq[String], items: Map[Char, AnyRef]) =
pattern ++ items.map {
@@ -54,7 +57,7 @@ object Misc {
def filterType[T](from: Iterable[_], cls: Class[T]) = from.filter(cls.isInstance).asInstanceOf[Iterable[T]]
def getBiomeByName(name: String) = BiomeGenBase.biomeList.find(_.biomeName == name).getOrElse(null)
def getBiomeByName(name: String) = BiomeGenBase.biomeList.find(x => x != null && x.biomeName == name).getOrElse(null)
def haveModVersion(spec: String): Boolean = {
val req = VersionParser.parseVersionReference(spec)
@@ -68,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))
}
+10 -2
View File
@@ -13,8 +13,11 @@ 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(val ids: IdManager) {
class BlockManager(val ids: IdManager, addOldTileNames: Boolean = true) {
def this(ids: IdManager) = this(ids, true)
def regBlockCls[T <: Block](blockCls: Class[T], name: String, addStack: Boolean = true): T = {
val block = blockCls.getConstructor(classOf[Int]).newInstance(ids.getBlockId(name): Integer)
@@ -28,7 +31,12 @@ class BlockManager(val ids: IdManager) {
GameRegistry.registerCustomItemStack(name, new ItemStack(block))
if (block.isInstanceOf[HasTE[_]])
GameRegistry.registerTileEntity(block.asInstanceOf[HasTE[_]].TEClass, name)
if (addOldTileNames)
GameRegistry.registerTileEntityWithAlternatives(block.asInstanceOf[HasTE[_]].TEClass,
"%s.%s".format(Misc.getActiveModId, name), name)
else
GameRegistry.registerTileEntity(block.asInstanceOf[HasTE[_]].TEClass,
"%s.%s".format(Misc.getActiveModId, name))
return block
}
@@ -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)
}
}
+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.{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
}
@@ -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)
}
+5
View File
@@ -29,6 +29,11 @@ case class Color(r: Float, g: Float, b: Float, a: Float = 1) {
object Color {
def fromRGB(r: Int, g: Int, b: Int, a: Int = 255) = Color(r / 255F, g / 255F, b / 255F, a / 255F)
def fromInt(v: Int) = Color(
((v >> 16) & 0xFF) / 255F,
((v >> 8) & 0xFF) / 255F,
(v & 0xFF) / 255F
)
final val white = Color(1, 1, 1)
final val black = Color(0, 0, 0)
final val red = Color(1, 0, 0)
+21 -1
View File
@@ -13,8 +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) {
@@ -77,8 +79,26 @@ 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 })
.map({ case (slot, stack) => slot })
def copyWithRandomSize(template: ItemStack, max: Int, rand: Random) = {
val newSize = rand.nextInt(max)
val newStack = template.copy()
newStack.stackSize =
if (newSize > newStack.getMaxStackSize)
newStack.getMaxStackSize
else if (newSize <= 0)
1
else
newSize
newStack
}
}
@@ -60,10 +60,12 @@ case class ConfigSection(pfx: String = "") extends CfgEntry with Iterable[(Strin
def getIntList(id: String) = getDoubleList(id).map(_.round.toInt).toList
def getFloatList(id: String) = getDoubleList(id).map(_.toFloat).toList
def getColor(id: String) = getFloatList(id) match {
case r :: g :: b :: Nil => Color(r, g, b)
case _ => sys.error("Color definition must be 3 numbers")
}
def getColor(id: String) =
raw(id) match {
case EntryDouble(c) => Color.fromInt(c.toInt)
case EntryNumList(r :: g :: b :: Nil) => Color(r.toFloat, g.toFloat, b.toFloat)
case x => sys.error("Invalid color definition: %s".format(x))
}
}
@@ -13,12 +13,17 @@ import net.bdew.lib.recipes.RecipeParser
trait GenericConfigParser extends RecipeParser {
override def statement = statementCfg | super.statement
def negativeNumber = "-" ~> decimalNumber ^^ { case x => "-" + x }
def signedNumber = decimalNumber | negativeNumber
// Hex variant must come before decimalNumber, otherwise the parser breaks
def signedNumber = (
("0x" ~> "[0-9A-Fa-f]+".r) ^^ { case x => Integer.parseInt(x, 16).toDouble }
| decimalNumber ^^ { case x => x.toDouble }
| ("-" ~> decimalNumber) ^^ { case x => -x.toDouble }
)
def cfgStatementNum = ident ~ "=" ~ signedNumber ^^ { case id ~ eq ~ n => CfgVal(id, EntryDouble(n.toDouble)) }
def cfgStatementStr = ident ~ "=" ~ str ^^ { case id ~ eq ~ s => CfgVal(id, EntryStr(s)) }
def cfgStatementNumList = ident ~ ("=" ~> "{" ~> signedNumber.* <~ "}") ^^ { case id ~ l => CfgVal(id, EntryNumList(l.map(_.toDouble))) }
def cfgStatementNumList = ident ~ ("=" ~> "{" ~> signedNumber.* <~ "}") ^^ { case id ~ l => CfgVal(id, EntryNumList(l)) }
def cfgStatementSub = cfgBlock ^^ { case (id, st) => CfgSub(id, st) }
def cfgStatement = cfgStatementNum | cfgStatementStr | cfgStatementSub | cfgStatementNumList
@@ -0,0 +1,24 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/gendustry
*
* 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/gendustry/master/MMPL-1.0.txt
*/
package net.bdew.lib.recipes.lootlist
import net.bdew.lib.recipes.StackRef
import net.bdew.lib.recipes.gencfg.CfgEntry
/**
* Allos definition of random-weighted loot/drop lists in config sections
* @param list List on (chance percent, stack reference) pairs, resolve to
* real ItemStacks with LootListLoader.resolveLootList
*/
case class EntryLootList(list: List[(Int, StackRef)]) extends CfgEntry
@@ -0,0 +1,42 @@
/*
* 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.recipes.lootlist
import net.bdew.lib.recipes.gencfg.GenericConfigLoader
import net.minecraftforge.oredict.OreDictionary
import net.bdew.lib.recipes.StackRef
import net.minecraft.item.ItemStack
/**
* Loader mixin for loot lists
*/
trait LootListLoader extends GenericConfigLoader {
def resolveLootList(entry: EntryLootList): List[(Int, ItemStack)] = resolveLootList(entry.list)
def resolveLootList(list: List[(Int, StackRef)]) =
(for ((chance, ref) <- list) yield {
try {
val itemStack = getConcreteStack(ref)
if (itemStack == null) {
log.warning("Unable to resolve %s: null returned".format(ref))
None
} else {
if (itemStack.getItemDamage == OreDictionary.WILDCARD_VALUE) {
log.info("meta/damage is unset in %s, defaulting to 0".format(ref))
itemStack.setItemDamage(0)
}
Some((chance, itemStack))
}
} catch {
case e: Throwable =>
log.warning("Unable to resolve %s: %s".format(ref, e.getMessage))
None
}
}).flatten
}
@@ -0,0 +1,25 @@
/*
* 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.recipes.lootlist
import net.bdew.lib.recipes.gencfg.{CfgVal, GenericConfigParser}
/**
* Parser mixin for loot lists
* ident: DropsList(
* {chance}% {stackRef}
* ...
* )
*/
trait LootListParser extends GenericConfigParser {
def dropsEntry = (wholeNumber <~ "%") ~ spec ^^ { case n ~ s => (n.toInt, s) }
def cfgDrops = ident ~ (":" ~> "DropsList" ~> "(" ~> dropsEntry.* <~ ")") ^^ { case id ~ a => CfgVal(id, EntryLootList(a)) }
override def cfgStatement = cfgDrops | super.cfgStatement
}