electrodynamics/src/main/scala/edx/quantum/machine/thermometer/TileThermometer.scala

233 lines
5.8 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.quantum.machine.thermometer
2014-09-28 03:40:01 +02:00
import java.util.ArrayList
import cpw.mods.fml.common.Optional
import cpw.mods.fml.relauncher.{Side, SideOnly}
2015-01-14 12:06:03 +01:00
import edx.core.Reference
2014-12-02 05:26:08 +01:00
import li.cil.oc.api.machine.{Arguments, Callback, Context}
import li.cil.oc.api.network.SimpleComponent
2014-09-28 03:40:01 +02:00
import net.minecraft.block.Block
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.minecraft.world.IBlockAccess
2015-01-26 12:40:32 +01:00
import resonantengine.lib.grid.thermal.GridThermal
2015-01-26 13:28:38 +01:00
import resonantengine.lib.modcontent.block.ResonantTile
2015-01-26 12:40:32 +01:00
import resonantengine.lib.transform.vector.{Vector3, VectorWorld}
import resonantengine.lib.utility.inventory.InventoryUtility
2015-01-26 13:28:38 +01:00
import resonantengine.prefab.block.itemblock.ItemBlockSaved
2014-09-28 03:40:01 +02:00
/**
* Thermometer TileEntity
*/
object TileThermometer
{
2014-12-01 12:59:35 +01:00
final val MAX_THRESHOLD: Int = 5000
private var iconSide: IIcon = null
2014-09-28 03:40:01 +02:00
}
@Optional.Interface(iface = "li.cil.oc.api.network.SimpleComponent", modid = "OpenComputers")
2015-01-21 14:18:00 +01:00
@deprecated
2015-01-26 11:17:24 +01:00
class TileThermometer extends ResonantTile(Material.piston) with SimpleComponent
2014-09-28 03:40:01 +02:00
{
2015-01-21 14:18:00 +01:00
var detectedTemperature: Float = 295
var previousDetectedTemperature: Float = 295
var trackCoordinate: Vector3 = null
private var threshold: Int = 1000
private var isProvidingPower: Boolean = false
2014-09-28 03:40:01 +02:00
2014-12-01 12:59:35 +01:00
//Constructor
providePower = true
normalRender = false
renderStaticBlock = true
itemBlock = classOf[ItemBlockThermometer]
isOpaqueCube = true
2014-09-28 03:40:01 +02:00
2014-12-01 12:59:35 +01:00
override def getIcon(side: Int, meta: Int): IIcon =
{
return if (side == 1 || side == 0) super.getIcon(side, meta) else TileThermometer.iconSide
}
2014-09-28 03:40:01 +02:00
2014-12-01 12:59:35 +01:00
@SideOnly(Side.CLIENT) override def registerIcons(iconRegister: IIconRegister)
{
super.registerIcons(iconRegister)
TileThermometer.iconSide = iconRegister.registerIcon(Reference.prefix + "machine")
}
2014-09-28 03:40:01 +02:00
2014-12-01 12:59:35 +01:00
override def use(player: EntityPlayer, side: Int, hit: Vector3): Boolean =
{
if (player.isSneaking)
2014-09-28 03:40:01 +02:00
{
2015-01-21 14:18:00 +01:00
setThreshold(getThreshold + 100)
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
else
2014-09-28 03:40:01 +02:00
{
2015-01-21 14:18:00 +01:00
setThreshold(getThreshold - 100)
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
return true
}
2014-09-28 03:40:01 +02:00
2014-12-01 12:59:35 +01:00
override def configure(player: EntityPlayer, side: Int, hit: Vector3): Boolean =
{
if (player.isSneaking)
2014-09-28 03:40:01 +02:00
{
2015-01-21 14:18:00 +01:00
setThreshold(getThreshold - 10)
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
else
2014-09-28 03:40:01 +02:00
{
2015-01-21 14:18:00 +01:00
setThreshold(getThreshold + 10)
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
return true
}
2014-09-28 03:40:01 +02:00
2015-01-27 09:15:44 +01:00
def setThreshold(newThreshold: Int)
{
threshold = newThreshold % TileThermometer.MAX_THRESHOLD
if (threshold <= 0)
{
threshold = TileThermometer.MAX_THRESHOLD
}
markUpdate
}
2014-12-01 12:59:35 +01:00
override def getStrongRedstonePower(access: IBlockAccess, side: Int): Int =
{
return if (isProvidingPower) 15 else 0
}
2014-09-28 03:40:01 +02:00
2014-12-01 12:59:35 +01:00
override def getDrops(metadata: Int, fortune: Int): ArrayList[ItemStack] =
{
return new ArrayList[ItemStack]
}
override def onRemove(block: Block, par6: Int)
{
2021-04-05 14:41:30 +02:00
val stack: ItemStack = ItemBlockSaved.getItemStackWithNBT(getBlockType, world, x.toInt, y.toInt, z.toInt)
2014-12-01 12:59:35 +01:00
InventoryUtility.dropItemStack(world, center, stack)
}
2014-09-28 03:40:01 +02:00
2014-12-01 12:59:35 +01:00
override def update
{
super.update
if (!worldObj.isRemote)
2014-09-28 03:40:01 +02:00
{
2014-12-01 12:59:35 +01:00
if (ticks % 10 == 0)
{
if (trackCoordinate != null)
2014-09-28 03:40:01 +02:00
{
2015-01-25 09:58:35 +01:00
detectedTemperature = GridThermal.getTemperature(new VectorWorld(world, trackCoordinate))
2014-09-28 03:40:01 +02:00
}
else
{
2021-04-05 14:41:30 +02:00
detectedTemperature = GridThermal.getTemperature(toVectorWorld)
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
if (detectedTemperature != previousDetectedTemperature || isProvidingPower != this.isOverThreshold)
2014-09-28 03:40:01 +02:00
{
2014-12-01 12:59:35 +01:00
previousDetectedTemperature = detectedTemperature
isProvidingPower = isOverThreshold
notifyChange
//sendPacket(getDescPacket)
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
}
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
}
2014-09-28 03:40:01 +02:00
2015-01-27 09:15:44 +01:00
def isOverThreshold: Boolean =
{
return detectedTemperature >= getThreshold
}
def getThreshold: Int =
{
return threshold
}
2014-12-01 12:59:35 +01:00
def setTrack(track: Vector3)
{
trackCoordinate = track
}
/**
* Reads a tile entity from NBT.
*/
override def readFromNBT(nbt: NBTTagCompound)
{
super.readFromNBT(nbt)
threshold = nbt.getInteger("threshold")
if (nbt.hasKey("trackCoordinate"))
2014-09-28 03:40:01 +02:00
{
2014-12-01 12:59:35 +01:00
trackCoordinate = new Vector3(nbt.getCompoundTag("trackCoordinate"))
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
else
2014-09-28 03:40:01 +02:00
{
2014-12-01 12:59:35 +01:00
trackCoordinate = null
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
}
2014-09-28 03:40:01 +02:00
2014-12-01 12:59:35 +01:00
/**
* Writes a tile entity to NBT.
*/
override def writeToNBT(nbt: NBTTagCompound)
{
super.writeToNBT(nbt)
nbt.setInteger("threshold", threshold)
if (trackCoordinate != null)
2014-09-28 03:40:01 +02:00
{
2014-12-01 12:59:35 +01:00
nbt.setTag("trackCoordinate", this.trackCoordinate.writeNBT(new NBTTagCompound))
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
}
@Callback
@Optional.Method(modid = "OpenComputers")
def getTemperature(context: Context, args: Arguments): Array[Any] =
{
return Array[Any](this.detectedTemperature)
}
@Callback
2014-12-02 05:26:08 +01:00
@Optional.Method(modid = "OpenComputers")
def getWarningTemperature(context: Context, args: Arguments): Array[Any] =
2014-12-01 12:59:35 +01:00
{
2015-01-21 14:18:00 +01:00
return Array[Any](this.getThreshold)
2014-12-01 12:59:35 +01:00
}
@Callback
2014-12-02 05:26:08 +01:00
@Optional.Method(modid = "OpenComputers")
def isAboveWarningTemperature(context: Context, args: Arguments): Array[Any] =
2014-12-01 12:59:35 +01:00
{
return Array[Any](this.isOverThreshold)
}
@Callback
2014-12-02 05:26:08 +01:00
@Optional.Method(modid = "OpenComputers")
def setWarningTemperature(context: Context, args: Arguments): Array[Any] =
2014-12-01 12:59:35 +01:00
{
if (args.count <= 0)
2014-09-28 03:40:01 +02:00
{
2014-12-01 12:59:35 +01:00
throw new IllegalArgumentException("Not enough Arguments. Must provide one argument")
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
if (args.count >= 2)
2014-09-28 03:40:01 +02:00
{
2014-12-01 12:59:35 +01:00
throw new IllegalArgumentException("Too many Arguments. Must provide one argument")
2014-09-28 03:40:01 +02:00
}
2014-12-02 05:26:08 +01:00
if (!args.isInteger(0))
2014-09-28 03:40:01 +02:00
{
2014-12-01 12:59:35 +01:00
throw new IllegalArgumentException("Invalid Argument. Must provide an Integer")
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
this synchronized
2014-09-28 03:40:01 +02:00
{
2014-12-01 12:59:35 +01:00
this.setThreshold(args.checkInteger(0))
2014-09-28 03:40:01 +02:00
}
2014-12-01 12:59:35 +01:00
return Array[Any](this.threshold == args.checkInteger(0))
}
def getComponentName: String =
{
2014-12-02 05:26:08 +01:00
return "Thermometer"
2014-12-01 12:59:35 +01:00
}
2021-04-05 14:41:30 +02:00
}