diff --git a/src/net/bdew/lib/Client.scala b/src/net/bdew/lib/Client.scala new file mode 100644 index 0000000..d22e1e9 --- /dev/null +++ b/src/net/bdew/lib/Client.scala @@ -0,0 +1,29 @@ +/* + * 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 + +import org.lwjgl.input.Keyboard +import net.minecraft.client.Minecraft + +/** + * Misc functions and shortcuts for clientside code + */ +object Client { + def minecraft = Minecraft.getMinecraft + def fontRenederer = Minecraft.getMinecraft.fontRenderer + def renderEngine = Minecraft.getMinecraft.renderEngine + def world = Minecraft.getMinecraft.theWorld + def player = Minecraft.getMinecraft.thePlayer + + def shiftDown: Boolean = { + return Keyboard.isKeyDown(42) || Keyboard.isKeyDown(54) + } + +} diff --git a/src/net/bdew/lib/Misc.scala b/src/net/bdew/lib/Misc.scala index 1c34475..6ecdf9c 100644 --- a/src/net/bdew/lib/Misc.scala +++ b/src/net/bdew/lib/Misc.scala @@ -16,6 +16,7 @@ import net.minecraft.item.ItemStack 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 object Misc { def iterNbtCompoundList(parent: NBTTagCompound, name: String): Iterable[NBTTagCompound] = { @@ -55,6 +56,8 @@ 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 haveModVersion(spec: String): Boolean = { val req = VersionParser.parseVersionReference(spec) val modid = req.getLabel diff --git a/src/net/bdew/lib/gui/BaseScreen.scala b/src/net/bdew/lib/gui/BaseScreen.scala index f0757a9..b5c38a1 100644 --- a/src/net/bdew/lib/gui/BaseScreen.scala +++ b/src/net/bdew/lib/gui/BaseScreen.scala @@ -13,6 +13,7 @@ import net.minecraft.client.gui.inventory.GuiContainer import net.minecraft.inventory.Container import scala.collection.mutable import net.minecraft.client.Minecraft +import org.lwjgl.opengl.GL11 abstract class BaseScreen(cont: Container, xSz: Int, ySz: Int) extends GuiContainer(cont) { xSize = xSz @@ -61,6 +62,10 @@ abstract class BaseScreen(cont: Container, xSz: Int, ySz: Int) extends GuiContai protected def drawGuiContainerBackgroundLayer(f: Float, x: Int, y: Int) { if (background != null) widgets.drawTexture(rect, background) - widgets.drawBackground(Point(x, y)) + + GL11.glPushMatrix() + GL11.glTranslatef(rect.x, rect.y, 0) + widgets.drawBackground(Point(x, y) - rect.origin) + GL11.glPopMatrix() } } diff --git a/src/net/bdew/lib/gui/BaseSlot.scala b/src/net/bdew/lib/gui/BaseSlot.scala new file mode 100644 index 0000000..a6ab605 --- /dev/null +++ b/src/net/bdew/lib/gui/BaseSlot.scala @@ -0,0 +1,21 @@ +/* + * 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.gui + +import net.minecraft.inventory.{IInventory, Slot} +import org.lwjgl.opengl.GL11 + +class BaseSlot(inv: IInventory, slot: Int, x: Int, y: Int) extends Slot(inv, slot, x, y) { + override def getBackgroundIconIndex = { + // Workaround for bug in GuiContainer, color isn't reset if previous slot had an item with color. + GL11.glColor4d(1, 1, 1, 1) + super.getBackgroundIconIndex + } +} diff --git a/src/net/bdew/lib/gui/SlotValidating.scala b/src/net/bdew/lib/gui/SlotValidating.scala index 9512e6d..5961337 100644 --- a/src/net/bdew/lib/gui/SlotValidating.scala +++ b/src/net/bdew/lib/gui/SlotValidating.scala @@ -10,9 +10,8 @@ package net.bdew.lib.gui import net.minecraft.inventory.IInventory -import net.minecraft.inventory.Slot import net.minecraft.item.ItemStack -class SlotValidating(inv: IInventory, slot: Int, x: Int, y: Int) extends Slot(inv, slot, x, y) { +class SlotValidating(inv: IInventory, slot: Int, x: Int, y: Int) extends BaseSlot(inv, slot, x, y) { override def isItemValid(stack: ItemStack) = inventory.isItemValidForSlot(getSlotIndex, stack) } \ No newline at end of file diff --git a/src/net/bdew/lib/gui/widgets/Widget.scala b/src/net/bdew/lib/gui/widgets/Widget.scala index 5281c44..fbb5af1 100644 --- a/src/net/bdew/lib/gui/widgets/Widget.scala +++ b/src/net/bdew/lib/gui/widgets/Widget.scala @@ -11,8 +11,6 @@ package net.bdew.lib.gui.widgets import scala.collection.mutable import net.bdew.lib.gui.{WidgetContainer, Point, Rect} -import net.minecraft.util.ResourceLocation -import net.minecraft.client.Minecraft trait BaseWidget { val rect: Rect diff --git a/src/net/bdew/lib/items/IStack.scala b/src/net/bdew/lib/items/IStack.scala new file mode 100644 index 0000000..10f2aaf --- /dev/null +++ b/src/net/bdew/lib/items/IStack.scala @@ -0,0 +1,23 @@ +/* + * 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.items + +import net.minecraft.item.{Item, ItemStack} + +/** + * Allows matching Items in ItemStacks in scala pattern matches + */ +object IStack { + def unapply(x: ItemStack): Option[Item] = + if (x == null || x.getItem == null) + None + else + Some(x.getItem) +} diff --git a/src/net/bdew/lib/items/ItemUtils.scala b/src/net/bdew/lib/items/ItemUtils.scala index c04c3bd..65b0ee0 100644 --- a/src/net/bdew/lib/items/ItemUtils.scala +++ b/src/net/bdew/lib/items/ItemUtils.scala @@ -10,7 +10,7 @@ package net.bdew.lib.items import net.minecraft.entity.item.EntityItem -import net.minecraft.item.ItemStack +import net.minecraft.item.{Item, ItemStack} import net.minecraft.world.World import net.minecraft.entity.player.EntityPlayer import net.minecraft.inventory.IInventory @@ -76,4 +76,9 @@ object ItemUtils { return stack } + + 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 }) } \ No newline at end of file diff --git a/src/net/bdew/lib/items/inventory/ItemInventory.scala b/src/net/bdew/lib/items/inventory/ItemInventory.scala index 285712c..d7992d3 100644 --- a/src/net/bdew/lib/items/inventory/ItemInventory.scala +++ b/src/net/bdew/lib/items/inventory/ItemInventory.scala @@ -10,7 +10,7 @@ package net.bdew.lib.items.inventory import net.minecraft.item.Item -import net.bdew.lib.gui.{BaseScreen, GuiProvider} +import net.bdew.lib.gui.GuiProvider import cpw.mods.fml.common.Loader import cpw.mods.fml.relauncher.{Side, SideOnly} import net.minecraft.entity.player.EntityPlayer @@ -29,7 +29,7 @@ trait ItemInventory extends Item with GuiProvider { val modObj = Loader.instance().activeModContainer().getMod @SideOnly(Side.CLIENT) - def makeGui(inv: InventoryItemAdapter, player: EntityPlayer): BaseScreen + def makeGui(inv: InventoryItemAdapter, player: EntityPlayer): AnyRef def makeContainer(inv: InventoryItemAdapter, player: EntityPlayer): ContainerItemInventory def makeAdaptor(player: EntityPlayer) = new InventoryItemAdapter(player, player.inventory.currentItem, invSize, invTagName) diff --git a/src/net/bdew/lib/recipes/DataClasses.scala b/src/net/bdew/lib/recipes/DataClasses.scala index 0f53293..b647c3d 100644 --- a/src/net/bdew/lib/recipes/DataClasses.scala +++ b/src/net/bdew/lib/recipes/DataClasses.scala @@ -127,13 +127,14 @@ case class StIfHaveMod(mod: String, thn: List[Statement], els: List[Statement]) /** * Conditional block - checks if mod is loaded + * Can contain only delayed statements * Syntax: ifOreDict {string} { {statements} } [else { {statements} }] * Parser: [[net.bdew.lib.recipes.RecipeParser.ifOreDict]] * @param id Ore Dictionary Entry * @param thn Statements to run if present * @param els Statements to run otherwise */ -case class StIfHaveOD(id: String, thn: List[Statement], els: List[Statement]) extends Statement +case class StIfHaveOD(id: String, thn: List[DelayedStatement], els: List[DelayedStatement]) extends DelayedStatement /** * Removes previously defined recieps for a specific result diff --git a/src/net/bdew/lib/recipes/RecipeLoader.scala b/src/net/bdew/lib/recipes/RecipeLoader.scala index 3ef06b6..d65b792 100644 --- a/src/net/bdew/lib/recipes/RecipeLoader.scala +++ b/src/net/bdew/lib/recipes/RecipeLoader.scala @@ -219,15 +219,6 @@ class RecipeLoader { processStatementsSafe(els) } - case StIfHaveOD(od, thn, els) => - if (OreDictionary.getOres(od).size() > 0) { - BdLib.logInfo("ifOreDict: %s found", od) - processStatementsSafe(thn) - } else { - BdLib.logInfo("ifOreDict: %s not found", od) - processStatementsSafe(els) - } - case StClearRecipes(res) => BdLib.logInfo("Clearing recipes that produce %s", res) delayedStatements = delayedStatements.filter({ @@ -307,16 +298,34 @@ class RecipeLoader { BdLib.logInfo("added %d -> %s", inStack, outStack) } + case StIfHaveOD(od, thn, els) => + if (OreDictionary.getOres(od).size() > 0) { + BdLib.logInfo("ifOreDict: %s found".format(od)) + processDelayedStatementsSafe(thn) + } else { + BdLib.logInfo("ifOreDict: %s not found".format(od)) + processDelayedStatementsSafe(els) + } + case x => BdLib.logError("Can't process %s - this is a programing bug!", x) } /** - * Process delayed statements, clear the list afterwards + * Process main delayed statements list, clear the list afterwards */ def processDelayedStatements() { BdLib.logInfo("Processing %d delayed statements", delayedStatements.size) - for (s <- delayedStatements) { + processDelayedStatementsSafe(delayedStatements) + delayedStatements = List.empty + } + + /** + * Process a list of delayed statements and catch all exceptions + * @param list The list to process + */ + def processDelayedStatementsSafe(list: List[DelayedStatement]) { + for (s <- list) { try { processDelayedStatement(s) } catch { @@ -327,7 +336,6 @@ class RecipeLoader { e.printStackTrace() } } - delayedStatements = List.empty } /** diff --git a/src/net/bdew/lib/recipes/RecipeParser.scala b/src/net/bdew/lib/recipes/RecipeParser.scala index 70bd8c6..fe39610 100644 --- a/src/net/bdew/lib/recipes/RecipeParser.scala +++ b/src/net/bdew/lib/recipes/RecipeParser.scala @@ -81,8 +81,8 @@ class RecipeParser extends JavaTokenParsers { case mod ~ thn ~ els => new StIfHaveMod(mod, thn, els.getOrElse(List.empty[Statement])) } - def ifOreDict = ("ifOreDict" ~> str) ~ ("{" ~> statements <~ "}") ~ ("else" ~> "{" ~> statements <~ "}").? ^^ { - case id ~ thn ~ els => new StIfHaveOD(id, thn, els.getOrElse(List.empty[Statement])) + def ifOreDict = ("ifOreDict" ~> str) ~ ("{" ~> delayedStatements <~ "}") ~ ("else" ~> "{" ~> delayedStatements <~ "}").? ^^ { + case id ~ thn ~ els => new StIfHaveOD(id, thn, els.getOrElse(List.empty[DelayedStatement])) } def recipeShaped3x3 = recipeTriplet ~ recipeTriplet ~ ("=>" ~> specWithCount) ~ recipeTriplet ^^ { @@ -107,20 +107,21 @@ class RecipeParser extends JavaTokenParsers { def clearRecipes = "clearRecipes" ~> ":" ~> spec ^^ { case sp => StClearRecipes(sp) } - def statement: Parser[Statement] = ( + def delayedStatement: Parser[DelayedStatement] = ( charSpec | classMacro - | ifMod | ifOreDict | recipeShaped3x3 | recipeShaped2x2 | recipeShaped9 | recipeShapeless | recipeSmelting - | clearRecipes ) + def statement: Parser[Statement] = delayedStatement | ifMod | clearRecipes + def statements = statement.* + def delayedStatements = delayedStatement.* def doParse(r: Reader): List[Statement] = { parseAll(statements, r) match {