Converted detector to scala
This commit is contained in:
parent
89e494116d
commit
cc31f1f0ff
2 changed files with 159 additions and 181 deletions
|
@ -1,181 +0,0 @@
|
|||
package resonantinduction.mechanical.machine;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.Packet;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.AxisAlignedBB;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import resonant.content.spatial.block.SpatialBlock;
|
||||
import resonant.engine.ResonantEngine;
|
||||
import resonant.lib.network.discriminator.PacketTile;
|
||||
import resonant.lib.network.discriminator.PacketType;
|
||||
import resonant.lib.network.handle.IPacketIDReceiver;
|
||||
import resonantinduction.archaic.blocks.TileFilterable;
|
||||
import resonantinduction.core.Reference;
|
||||
import resonantinduction.mechanical.Mechanical;
|
||||
|
||||
public class TileDetector extends TileFilterable implements IPacketIDReceiver
|
||||
{
|
||||
private boolean powering = false;
|
||||
|
||||
public TileDetector()
|
||||
{
|
||||
super();
|
||||
setTextureName(Reference.prefix() + "material_metal_side");
|
||||
this.canProvidePower(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update()
|
||||
{
|
||||
super.update();
|
||||
|
||||
if (!this.worldObj.isRemote && this.ticks() % 10 == 0)
|
||||
{
|
||||
int metadata = this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord);
|
||||
AxisAlignedBB testArea = AxisAlignedBB.getBoundingBox(this.xCoord, this.yCoord, this.zCoord, this.xCoord + 1, this.yCoord + 1, this.zCoord + 1);
|
||||
ForgeDirection dir = ForgeDirection.getOrientation(metadata);
|
||||
testArea.offset(dir.offsetX, dir.offsetY, dir.offsetZ);
|
||||
|
||||
ArrayList<Entity> entities = (ArrayList<Entity>) this.worldObj.getEntitiesWithinAABB(EntityItem.class, testArea);
|
||||
boolean powerCheck = false;
|
||||
|
||||
if (entities.size() > 0)
|
||||
{
|
||||
if (getFilter() != null)
|
||||
{
|
||||
for (int i = 0; i < entities.size(); i++)
|
||||
{
|
||||
EntityItem e = (EntityItem) entities.get(i);
|
||||
ItemStack itemStack = e.getEntityItem();
|
||||
|
||||
powerCheck = this.isFiltering(itemStack);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
powerCheck = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
powerCheck = false;
|
||||
}
|
||||
|
||||
if (powerCheck != this.powering)
|
||||
{
|
||||
this.powering = powerCheck;
|
||||
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, Mechanical.blockDetector);
|
||||
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord + 1, this.zCoord, Mechanical.blockDetector);
|
||||
for (int x = this.xCoord - 1; x <= this.xCoord + 1; x++)
|
||||
{
|
||||
for (int z = this.zCoord - 1; z <= this.zCoord + 1; z++)
|
||||
{
|
||||
this.worldObj.notifyBlocksOfNeighborChange(x, this.yCoord + 1, z, Mechanical.blockDetector);
|
||||
}
|
||||
}
|
||||
|
||||
ResonantEngine.instance.packetHandler.sendToAllAround(new PacketTile(this, 0, this.isInverted()), this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate()
|
||||
{
|
||||
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, Mechanical.blockDetector);
|
||||
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord + 1, this.zCoord, Mechanical.blockDetector);
|
||||
super.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound tag)
|
||||
{
|
||||
super.readFromNBT(tag);
|
||||
|
||||
this.powering = tag.getBoolean("powering");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound tag)
|
||||
{
|
||||
super.writeToNBT(tag);
|
||||
|
||||
tag.setBoolean("powering", this.powering);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet getDescriptionPacket()
|
||||
{
|
||||
return ResonantEngine.instance.packetHandler.toMCPacket(new PacketTile(this, 0, this.isInverted()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean read(ByteBuf data, int id, EntityPlayer player, PacketType type)
|
||||
{
|
||||
if(id == 0)
|
||||
this.setInverted(data.readBoolean());
|
||||
return true;
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@Override
|
||||
public void registerIcons(IIconRegister iconReg)
|
||||
{
|
||||
SpatialBlock.icon().put("detector_front_green", iconReg.registerIcon(Reference.prefix() + "detector_front_green"));
|
||||
SpatialBlock.icon().put("detector_front_red", iconReg.registerIcon(Reference.prefix() + "detector_front_red"));
|
||||
SpatialBlock.icon().put("detector_side_green", iconReg.registerIcon(Reference.prefix() + "detector_side_green"));
|
||||
SpatialBlock.icon().put("detector_side_red", iconReg.registerIcon(Reference.prefix() + "detector_side_red"));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SideOnly(Side.CLIENT)
|
||||
public IIcon getIcon(int side, int metadata)
|
||||
{
|
||||
if (side == ForgeDirection.SOUTH.ordinal())
|
||||
{
|
||||
return SpatialBlock.icon().get("detector_front_green");
|
||||
}
|
||||
|
||||
return SpatialBlock.icon().get("detector_side_green");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SideOnly(Side.CLIENT)
|
||||
public IIcon getIcon(IBlockAccess iBlockAccess, int side)
|
||||
{
|
||||
boolean isInverted = false;
|
||||
boolean isFront = false;
|
||||
TileEntity tileEntity = iBlockAccess.getTileEntity(x(), y(), z());
|
||||
|
||||
if (tileEntity instanceof TileDetector)
|
||||
{
|
||||
isFront = side == ((TileDetector) tileEntity).getDirection().ordinal();
|
||||
isInverted = ((TileDetector) tileEntity).isInverted();
|
||||
}
|
||||
|
||||
return isInverted ? (isFront ? SpatialBlock.icon().get("detector_front_red") : SpatialBlock.icon().get("detector_side_red")) : (isFront ? SpatialBlock.icon().get("detector_front_green") : SpatialBlock.icon().get("detector_side_green"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStrongRedstonePower(IBlockAccess access, int side)
|
||||
{
|
||||
if(side != getDirection().getOpposite().ordinal())
|
||||
{
|
||||
return powering ? 15 : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,159 @@
|
|||
package resonantinduction.mechanical.machine
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
import cpw.mods.fml.relauncher.{Side, SideOnly}
|
||||
import io.netty.buffer.ByteBuf
|
||||
import net.minecraft.client.renderer.texture.IIconRegister
|
||||
import net.minecraft.entity.Entity
|
||||
import net.minecraft.entity.item.EntityItem
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraft.network.Packet
|
||||
import net.minecraft.tileentity.TileEntity
|
||||
import net.minecraft.util.{AxisAlignedBB, IIcon}
|
||||
import net.minecraft.world.IBlockAccess
|
||||
import net.minecraftforge.common.util.ForgeDirection
|
||||
import resonant.content.spatial.block.SpatialBlock
|
||||
import resonant.engine.ResonantEngine
|
||||
import resonant.lib.network.discriminator.{PacketTile, PacketType}
|
||||
import resonant.lib.network.handle.IPacketIDReceiver
|
||||
import resonantinduction.archaic.blocks.TileFilterable
|
||||
import resonantinduction.core.Reference
|
||||
import resonantinduction.mechanical.Mechanical
|
||||
|
||||
class TileDetector extends TileFilterable with IPacketIDReceiver
|
||||
{
|
||||
setTextureName(Reference.prefix + "material_metal_side")
|
||||
this.canProvidePower(true)
|
||||
|
||||
override def update
|
||||
{
|
||||
super.update
|
||||
if (!this.worldObj.isRemote && this.ticks % 10 == 0)
|
||||
{
|
||||
val metadata: Int = this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord)
|
||||
val testArea: AxisAlignedBB = AxisAlignedBB.getBoundingBox(this.xCoord, this.yCoord, this.zCoord, this.xCoord + 1, this.yCoord + 1, this.zCoord + 1)
|
||||
val dir: ForgeDirection = ForgeDirection.getOrientation(metadata)
|
||||
testArea.offset(dir.offsetX, dir.offsetY, dir.offsetZ)
|
||||
val entities: ArrayList[Entity] = this.worldObj.getEntitiesWithinAABB(classOf[EntityItem], testArea).asInstanceOf[ArrayList[Entity]]
|
||||
var powerCheck: Boolean = false
|
||||
if (entities.size > 0)
|
||||
{
|
||||
if (getFilter != null)
|
||||
{
|
||||
{
|
||||
var i: Int = 0
|
||||
while (i < entities.size)
|
||||
{
|
||||
{
|
||||
val e: EntityItem = entities.get(i).asInstanceOf[EntityItem]
|
||||
val itemStack: ItemStack = e.getEntityItem
|
||||
powerCheck = this.isFiltering(itemStack)
|
||||
}
|
||||
({
|
||||
i += 1;
|
||||
i - 1
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
powerCheck = true
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
powerCheck = false
|
||||
}
|
||||
if (powerCheck != this.powering)
|
||||
{
|
||||
this.powering = powerCheck
|
||||
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, Mechanical.blockDetector)
|
||||
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord + 1, this.zCoord, Mechanical.blockDetector)
|
||||
|
||||
for(x <- (this.xCoord - 1) - (this.xCoord + 1))
|
||||
{
|
||||
for(z <- (this.zCoord - 1) to (this.zCoord + 1))
|
||||
{
|
||||
this.worldObj.notifyBlocksOfNeighborChange(x, this.yCoord + 1, z, Mechanical.blockDetector)
|
||||
}
|
||||
}
|
||||
ResonantEngine.instance.packetHandler.sendToAllAround(new PacketTile(this, 0, this.isInverted), this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override def invalidate
|
||||
{
|
||||
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, Mechanical.blockDetector)
|
||||
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord + 1, this.zCoord, Mechanical.blockDetector)
|
||||
super.invalidate
|
||||
}
|
||||
|
||||
override def readFromNBT(tag: NBTTagCompound)
|
||||
{
|
||||
super.readFromNBT(tag)
|
||||
this.powering = tag.getBoolean("powering")
|
||||
}
|
||||
|
||||
override def writeToNBT(tag: NBTTagCompound)
|
||||
{
|
||||
super.writeToNBT(tag)
|
||||
tag.setBoolean("powering", this.powering)
|
||||
}
|
||||
|
||||
override def getDescriptionPacket: Packet =
|
||||
{
|
||||
return ResonantEngine.instance.packetHandler.toMCPacket(new PacketTile(this, 0, this.isInverted))
|
||||
}
|
||||
|
||||
override def read(data: ByteBuf, id: Int, player: EntityPlayer, `type`: PacketType): Boolean =
|
||||
{
|
||||
if (id == 0) this.setInverted(data.readBoolean)
|
||||
return true
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT) override def registerIcons(iconReg: IIconRegister)
|
||||
{
|
||||
SpatialBlock.icon.put("detector_front_green", iconReg.registerIcon(Reference.prefix + "detector_front_green"))
|
||||
SpatialBlock.icon.put("detector_front_red", iconReg.registerIcon(Reference.prefix + "detector_front_red"))
|
||||
SpatialBlock.icon.put("detector_side_green", iconReg.registerIcon(Reference.prefix + "detector_side_green"))
|
||||
SpatialBlock.icon.put("detector_side_red", iconReg.registerIcon(Reference.prefix + "detector_side_red"))
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT) override def getIcon(side: Int, metadata: Int): IIcon =
|
||||
{
|
||||
if (side == ForgeDirection.SOUTH.ordinal)
|
||||
{
|
||||
return SpatialBlock.icon.get("detector_front_green")
|
||||
}
|
||||
return SpatialBlock.icon.get("detector_side_green")
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT) override def getIcon(iBlockAccess: IBlockAccess, side: Int): IIcon =
|
||||
{
|
||||
var isInverted: Boolean = false
|
||||
var isFront: Boolean = false
|
||||
val tileEntity: TileEntity = iBlockAccess.getTileEntity(x, y, z)
|
||||
if (tileEntity.isInstanceOf[TileDetector])
|
||||
{
|
||||
isFront = side == (tileEntity.asInstanceOf[TileDetector]).getDirection.ordinal
|
||||
isInverted = (tileEntity.asInstanceOf[TileDetector]).isInverted
|
||||
}
|
||||
return if (isInverted) (if (isFront) SpatialBlock.icon.get("detector_front_red") else SpatialBlock.icon.get("detector_side_red")) else (if (isFront) SpatialBlock.icon.get("detector_front_green") else SpatialBlock.icon.get("detector_side_green"))
|
||||
}
|
||||
|
||||
override def getStrongRedstonePower(access: IBlockAccess, side: Int): Int =
|
||||
{
|
||||
if (side != getDirection.getOpposite.ordinal)
|
||||
{
|
||||
return if (powering) 15 else 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
private var powering: Boolean = false
|
||||
}
|
Loading…
Reference in a new issue