From 1fa1cf3d2e3c263df21b84ac7c8c145e5c379ca6 Mon Sep 17 00:00:00 2001 From: Calclavia Date: Fri, 2 Jan 2015 15:01:39 +0800 Subject: [PATCH] Added workbench --- .../archaic/ArchaicContent.scala | 3 +- .../archaic/process/TileCastingMold.scala | 4 +- .../archaic/process/TileMillstone.scala | 212 +++++++++--------- .../archaic/process/TileWorkbench.scala | 89 ++++++++ 4 files changed, 197 insertions(+), 111 deletions(-) create mode 100644 src/main/scala/resonantinduction/archaic/process/TileWorkbench.scala diff --git a/src/main/scala/resonantinduction/archaic/ArchaicContent.scala b/src/main/scala/resonantinduction/archaic/ArchaicContent.scala index b30e474b2..68ff6b9e7 100644 --- a/src/main/scala/resonantinduction/archaic/ArchaicContent.scala +++ b/src/main/scala/resonantinduction/archaic/ArchaicContent.scala @@ -12,7 +12,7 @@ import resonantinduction.archaic.firebox.{TileFirebox, TileHotPlate} import resonantinduction.archaic.fluid.grate.TileGrate import resonantinduction.archaic.fluid.gutter.TileGutter import resonantinduction.archaic.fluid.tank.TileTank -import resonantinduction.archaic.process.{TileCastingMold, TileMillstone} +import resonantinduction.archaic.process.{TileWorkbench, TileCastingMold, TileMillstone} import resonantinduction.core.{RICreativeTab, Reference} import resonantinduction.mechanical.mech.gear.ItemHandCrank ; @@ -36,6 +36,7 @@ object ArchaicContent extends ContentHolder var blockGrate: Block = new TileGrate var blockGutter: Block = new TileGutter var blockTank: Block = new TileTank + var blockWorkbench: Block = new TileWorkbench //Constructor manager.setTab(RICreativeTab) diff --git a/src/main/scala/resonantinduction/archaic/process/TileCastingMold.scala b/src/main/scala/resonantinduction/archaic/process/TileCastingMold.scala index 233dfca58..56f373e19 100644 --- a/src/main/scala/resonantinduction/archaic/process/TileCastingMold.scala +++ b/src/main/scala/resonantinduction/archaic/process/TileCastingMold.scala @@ -35,8 +35,8 @@ class TileCastingMold extends TileInventory(Material.rock) with IFluidHandler wi //Constructor setTextureName(Reference.prefix + "material_metal_side") - normalRender(false) - isOpaqueCube(false) + normalRender = false + isOpaqueCube = false override def canUpdate: Boolean = { diff --git a/src/main/scala/resonantinduction/archaic/process/TileMillstone.scala b/src/main/scala/resonantinduction/archaic/process/TileMillstone.scala index 4038390cb..94a10f5d8 100644 --- a/src/main/scala/resonantinduction/archaic/process/TileMillstone.scala +++ b/src/main/scala/resonantinduction/archaic/process/TileMillstone.scala @@ -1,151 +1,147 @@ package resonantinduction.archaic.process -import cpw.mods.fml.common.network.ByteBufUtils import cpw.mods.fml.relauncher.{Side, SideOnly} import io.netty.buffer.ByteBuf import net.minecraft.block.material.Material import net.minecraft.client.renderer.texture.IIconRegister import net.minecraft.entity.player.EntityPlayer import net.minecraft.item.ItemStack -import net.minecraft.nbt.NBTTagCompound import net.minecraft.util.IIcon import net.minecraftforge.common.util.ForgeDirection import resonant.api.recipe.{MachineRecipes, RecipeResource} import resonant.lib.factory.resources.RecipeType -import resonant.lib.network.discriminator.{PacketTile, PacketType} -import resonant.lib.network.handle.IPacketReceiver +import resonant.lib.network.discriminator.PacketType +import resonant.lib.network.handle.{TPacketReceiver, TPacketSender} import resonant.lib.prefab.tile.TileInventory import resonant.lib.prefab.tile.spatial.SpatialBlock +import resonant.lib.transform.vector.Vector3 import resonant.lib.utility.inventory.InventoryUtility +import resonant.lib.wrapper.ByteBufWrapper._ import resonantinduction.core.Reference import resonantinduction.mechanical.mech.gear.ItemHandCrank -import resonant.lib.transform.vector.Vector3 -class TileMillstone extends TileInventory(Material.rock) with IPacketReceiver +class TileMillstone extends TileInventory(Material.rock) with TPacketSender with TPacketReceiver { + private var grindCount: Int = 0 - private var grindCount: Int = 0 + //Constructor + maxSlots = 1 + setTextureName(Reference.prefix + "millstone_side") - //Constructor - setTextureName(Reference.prefix + "millstone_side") + override def onInventoryChanged + { + grindCount = 0 + worldObj.markBlockForUpdate(xCoord, yCoord, zCoord) + } - override def onInventoryChanged + def doGrind(spawnPos: Vector3) + { + val outputs = MachineRecipes.INSTANCE.getOutput(RecipeType.GRINDER.name, getStackInSlot(0)) + + if (outputs.length > 0) { - grindCount = 0 - worldObj.markBlockForUpdate(xCoord, yCoord, zCoord) - } - - def doGrind(spawnPos: Vector3) - { - val outputs: Array[RecipeResource] = MachineRecipes.INSTANCE.getOutput(RecipeType.GRINDER.name, getStackInSlot(0)) - if (outputs.length > 0) + grindCount += 1 + if (grindCount > 20) + { + for (res <- outputs) { - grindCount += 1; - if ( grindCount > 20) - { - for (res <- outputs) - { - InventoryUtility.dropItemStack(worldObj, spawnPos, res.getItemStack.copy) - } - decrStackSize(0, 1) - onInventoryChanged - } + InventoryUtility.dropItemStack(worldObj, spawnPos, res.getItemStack.copy) } + decrStackSize(0, 1) + onInventoryChanged + } } + } - override def canUpdate: Boolean = + override def canUpdate: Boolean = + { + return false + } + + override def isItemValidForSlot(i: Int, itemStack: ItemStack): Boolean = + { + return MachineRecipes.INSTANCE.getOutput(RecipeType.GRINDER.name, itemStack).length > 0 + } + + override def canStore(stack: ItemStack, slot: Int, side: ForgeDirection): Boolean = + { + return true + } + + @SideOnly(Side.CLIENT) + override def registerIcons(iconReg: IIconRegister) + { + SpatialBlock.icon.put("millstone_side", iconReg.registerIcon(Reference.prefix + "millstone_side")) + SpatialBlock.icon.put("millstone_top", iconReg.registerIcon(Reference.prefix + "millstone_top")) + } + + @SideOnly(Side.CLIENT) + override def getIcon(side: Int, meta: Int): IIcon = + { + if (side == 0 || side == 1) { - return false + return SpatialBlock.icon.get("millstone_top") } + return SpatialBlock.icon.get("millstone_side") + } - override def isItemValidForSlot(i: Int, itemStack: ItemStack): Boolean = + override def click(player: EntityPlayer) + { + if (!world.isRemote) { - return MachineRecipes.INSTANCE.getOutput(RecipeType.GRINDER.name, itemStack).length > 0 + val output: ItemStack = getStackInSlot(0) + if (output != null) + { + InventoryUtility.dropItemStack(world, new Vector3(player), output, 0) + setInventorySlotContents(0, null) + } + onInventoryChanged } + } - override def canStore(stack: ItemStack, slot: Int, side: ForgeDirection): Boolean = + override def use(player: EntityPlayer, hitSide: Int, hit: Vector3): Boolean = + { + val current: ItemStack = player.inventory.getCurrentItem + val output: ItemStack = getStackInSlot(0) + if (current != null && current.getItem.isInstanceOf[ItemHandCrank]) { + if (output != null) + { + doGrind(new Vector3(player)) + player.addExhaustion(0.3f) return true + } } - - /** - * Packets - */ - override def getDescPacket: PacketTile = + if (output != null) { - val nbt: NBTTagCompound = new NBTTagCompound - this.writeToNBT(nbt) - return new PacketTile(this, nbt) + InventoryUtility.dropItemStack(world, new Vector3(player), output, 0) + setInventorySlotContents(0, null) } - - def read(data: ByteBuf, player: EntityPlayer, `type`: PacketType) + else if (current != null && isItemValidForSlot(0, current)) { - try - { - this.readFromNBT(ByteBufUtils.readTag(data)) - } - catch - { - case e: Exception => - { - e.printStackTrace - } - } + setInventorySlotContents(0, current) + player.inventory.setInventorySlotContents(player.inventory.currentItem, null) } + world.markBlockForUpdate(xi, yi, zi) + return false + } - @SideOnly(Side.CLIENT) override def registerIcons(iconReg: IIconRegister) - { - SpatialBlock.icon.put("millstone_side", iconReg.registerIcon(Reference.prefix + "millstone_side")) - SpatialBlock.icon.put("millstone_top", iconReg.registerIcon(Reference.prefix + "millstone_top")) - } + /** + * Packets + */ + /** + * Override this method + * Be sure to super this method or manually write the ID into the packet when sending + */ + override def write(buf: ByteBuf, id: Int) + { + super.write(buf, id) + buf <<<< writeToNBT + } - @SideOnly(Side.CLIENT) override def getIcon(side: Int, meta: Int): IIcon = - { - if (side == 0 || side == 1) - { - return SpatialBlock.icon.get("millstone_top") - } - return SpatialBlock.icon.get("millstone_side") - } - - override def click(player: EntityPlayer) - { - if (!world.isRemote) - { - val output: ItemStack = getStackInSlot(0) - if (output != null) - { - InventoryUtility.dropItemStack(world, new Vector3(player), output, 0) - setInventorySlotContents(0, null) - } - onInventoryChanged - } - } - - override def use(player: EntityPlayer, hitSide: Int, hit: Vector3): Boolean = - { - val current: ItemStack = player.inventory.getCurrentItem - val output: ItemStack = getStackInSlot(0) - if (current != null && current.getItem.isInstanceOf[ItemHandCrank]) - { - if (output != null) - { - doGrind(new Vector3(player)) - player.addExhaustion(0.3f) - return true - } - } - if (output != null) - { - InventoryUtility.dropItemStack(world, new Vector3(player), output, 0) - setInventorySlotContents(0, null) - } - else if (current != null && isItemValidForSlot(0, current)) - { - setInventorySlotContents(0, current) - player.inventory.setInventorySlotContents(player.inventory.currentItem, null) - } - world.markBlockForUpdate(xi, yi, zi) - return false - } + override def read(buf: ByteBuf, id: Int, packetType: PacketType) + { + super.read(buf, id, packetType) + buf >>>> readFromNBT + } } \ No newline at end of file diff --git a/src/main/scala/resonantinduction/archaic/process/TileWorkbench.scala b/src/main/scala/resonantinduction/archaic/process/TileWorkbench.scala new file mode 100644 index 000000000..47ebeeb07 --- /dev/null +++ b/src/main/scala/resonantinduction/archaic/process/TileWorkbench.scala @@ -0,0 +1,89 @@ +package resonantinduction.archaic.process + +import net.minecraft.block.material.Material +import net.minecraft.entity.player.EntityPlayer +import net.minecraft.item.{Item, ItemStack} +import net.minecraft.util.ResourceLocation +import net.minecraftforge.client.model.AdvancedModelLoader +import net.minecraftforge.oredict.OreDictionary +import resonant.api.recipe.{MachineRecipes, RecipeResource} +import resonant.lib.factory.resources.RecipeType +import resonant.lib.network.handle.{TPacketReceiver, TPacketSender} +import resonant.lib.prefab.tile.TileInventory +import resonant.lib.render.RenderUtility +import resonant.lib.transform.vector.Vector3 +import resonant.lib.utility.inventory.InventoryUtility +import resonantinduction.archaic.engineering.ItemHammer +import resonantinduction.core.{Reference, ResonantInduction} + +/** + * + * @author Calclavia + */ +object TileWorkbench +{ + val model = AdvancedModelLoader.loadModel(new ResourceLocation(Reference.domain, Reference.modelPath + "workbench.obj")) +} + +class TileWorkbench extends TileInventory(Material.rock) with TPacketSender with TPacketReceiver +{ + //Constructor + maxSlots = 1 + setTextureName(Reference.prefix + "material_metal_side") + normalRender = false + isOpaqueCube = false + + override def use(player: EntityPlayer, hitSide: Int, hit: Vector3): Boolean = + { + //The player is holding a hammer. Attempt to crush the item on the bench + if (player.getCurrentEquippedItem != null) + { + if (player.getCurrentEquippedItem.getItem.isInstanceOf[ItemHammer]) + { + val inputStack = getStackInSlot(0) + + if (inputStack != null) + { + val oreName: String = OreDictionary.getOreName(OreDictionary.getOreID(inputStack)) + if (oreName != null && !(oreName == "Unknown")) + { + val outputs: Array[RecipeResource] = MachineRecipes.INSTANCE.getOutput(RecipeType.CRUSHER.name, oreName) + if (outputs != null && outputs.length > 0) + { + if (!world.isRemote && world.rand.nextFloat < 0.2) + { + for (resource <- outputs) + { + val outputStack: ItemStack = resource.getItemStack.copy + if (outputStack != null) + { + InventoryUtility.dropItemStack(world, new Vector3(player), outputStack, 0) + inputStack.stackSize -= 1 + setInventorySlotContents(0, if (inputStack.stackSize <= 0) null else inputStack) + } + } + } + ResonantInduction.proxy.renderBlockParticle(world, new Vector3(x + 0.5, y + 0.5, z + 0.5), new Vector3((Math.random - 0.5f) * 3, (Math.random - 0.5f) * 3, (Math.random - 0.5f) * 3), Item.getIdFromItem(inputStack.getItem), 1) + world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, Reference.prefix + "hammer", 0.5f, 0.8f + (0.2f * world.rand.nextFloat)) + player.addExhaustion(0.1f) + player.getCurrentEquippedItem.damageItem(1, player) + return true + } + } + } + } + else + { + return interactCurrentItem(this, 0, player) + } + } + + return false + } + + override def renderDynamic(pos: Vector3, frame: Float, pass: Int) + { + RenderUtility.bind(Reference.domain, Reference.modelPath + "workbench.png") + TileWorkbench.model.renderAll() + } +}