Added multimeter detection mechanism
This commit is contained in:
parent
ca0fca1fa1
commit
37d9200af2
8 changed files with 348 additions and 18 deletions
Binary file not shown.
After Width: | Height: | Size: 6 KiB |
|
@ -3,7 +3,13 @@
|
|||
*/
|
||||
package resonantinduction.base;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import cpw.mods.fml.common.network.PacketDispatcher;
|
||||
import cpw.mods.fml.common.network.Player;
|
||||
|
||||
/**
|
||||
* @author Calclavia
|
||||
|
@ -12,6 +18,7 @@ import net.minecraft.tileentity.TileEntity;
|
|||
public class TileEntityBase extends TileEntity
|
||||
{
|
||||
protected long ticks = 0;
|
||||
public Set<EntityPlayer> playersUsing = new HashSet<EntityPlayer>();
|
||||
|
||||
public void initiate()
|
||||
{
|
||||
|
@ -28,5 +35,9 @@ public class TileEntityBase extends TileEntity
|
|||
this.initiate();
|
||||
}
|
||||
|
||||
for (EntityPlayer player : this.playersUsing)
|
||||
{
|
||||
PacketDispatcher.sendPacketToPlayer(this.getDescriptionPacket(), (Player) player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,12 +85,12 @@ public class FXElectricBolt extends EntityFX
|
|||
this.segments.add(new BoltSegment(this.start, this.end));
|
||||
this.recalculate();
|
||||
double offsetRatio = this.boltLength * this.complexity;
|
||||
this.split(2, offsetRatio / 8, 0.7f, 0.1f, 20);
|
||||
this.split(2, offsetRatio / 12, 0.5f, 0.1f, 25);
|
||||
this.split(2, offsetRatio / 24, 0.5f, 0.1f, 28);
|
||||
this.split(2, offsetRatio / 32, 0.5f, 0.1f, 30);
|
||||
this.split(2, offsetRatio / 48, 0, 0, 0);
|
||||
this.split(2, offsetRatio / 64, 0, 0, 0);
|
||||
this.split(2, offsetRatio / 10, 0.7f, 0.1f, 20);
|
||||
this.split(2, offsetRatio / 15, 0.5f, 0.1f, 25);
|
||||
this.split(2, offsetRatio / 25, 0.5f, 0.1f, 28);
|
||||
this.split(2, offsetRatio / 38, 0.5f, 0.1f, 30);
|
||||
this.split(2, offsetRatio / 55, 0, 0, 0);
|
||||
this.split(2, offsetRatio / 70, 0, 0, 0);
|
||||
|
||||
this.recalculate();
|
||||
|
||||
|
@ -154,7 +154,7 @@ public class FXElectricBolt extends EntityFX
|
|||
*/
|
||||
for (int i = 1; i < splitAmount; i++)
|
||||
{
|
||||
Vector3 newOffset = segment.difference.getPerpendicular().rotate(this.rand.nextFloat() * 180, segment.difference).scale((this.rand.nextFloat() - 0.5F) * offset);
|
||||
Vector3 newOffset = segment.difference.getPerpendicular().rotate(this.rand.nextFloat() * 360, segment.difference).scale((this.rand.nextFloat() - 0.5F) * offset);
|
||||
Vector3 basePoint = startPoint.clone().translate(subSegment.clone().scale(i));
|
||||
|
||||
newPoints[i] = new BoltPoint(basePoint, newOffset);
|
||||
|
@ -172,7 +172,7 @@ public class FXElectricBolt extends EntityFX
|
|||
|
||||
if ((i != 0) && (this.rand.nextFloat() < splitChance))
|
||||
{
|
||||
Vector3 splitrot = next.difference.xCrossProduct().rotate(this.rand.nextFloat() * 180, next.difference);
|
||||
Vector3 splitrot = next.difference.xCrossProduct().rotate(this.rand.nextFloat() * 360, next.difference);
|
||||
Vector3 diff = next.difference.clone().rotate((this.rand.nextFloat() * 0.66F + 0.33F) * splitAngle, splitrot).scale(splitLength);
|
||||
this.maxSplitID += 1;
|
||||
this.parentIDMap.put(this.maxSplitID, next.splitID);
|
||||
|
|
|
@ -5,9 +5,16 @@ package resonantinduction.multimeter;
|
|||
|
||||
import net.minecraft.block.ITileEntityProvider;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.client.renderer.texture.IconRegister;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.Icon;
|
||||
import net.minecraft.util.MathHelper;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.ForgeDirection;
|
||||
import resonantinduction.ResonantInduction;
|
||||
import resonantinduction.base.BlockBase;
|
||||
|
||||
|
@ -19,18 +26,103 @@ import resonantinduction.base.BlockBase;
|
|||
*/
|
||||
public class BlockMultimeter extends BlockBase implements ITileEntityProvider
|
||||
{
|
||||
private Icon machineIcon;
|
||||
|
||||
public BlockMultimeter(int id)
|
||||
{
|
||||
super("multimeter", id, Material.iron);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int par6, float par7, float par8, float par9)
|
||||
public static int determineOrientation(World par0World, int par1, int par2, int par3, EntityLivingBase par4EntityLivingBase)
|
||||
{
|
||||
entityPlayer.openGui(ResonantInduction.INSTNACE, 0, world, x, y, z);
|
||||
if (MathHelper.abs((float) par4EntityLivingBase.posX - (float) par1) < 2.0F && MathHelper.abs((float) par4EntityLivingBase.posZ - (float) par3) < 2.0F)
|
||||
{
|
||||
double d0 = par4EntityLivingBase.posY + 1.82D - (double) par4EntityLivingBase.yOffset;
|
||||
|
||||
if (d0 - (double) par2 > 2.0D)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((double) par2 - d0 > 0.0D)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int l = MathHelper.floor_double((double) (par4EntityLivingBase.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
|
||||
return l == 0 ? 2 : (l == 1 ? 5 : (l == 2 ? 3 : (l == 3 ? 4 : 0)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase par5EntityLivingBase, ItemStack par6ItemStack)
|
||||
{
|
||||
int l = determineOrientation(world, x, y, z, par5EntityLivingBase);
|
||||
world.setBlockMetadataWithNotify(x, y, z, l, 2);
|
||||
}
|
||||
|
||||
public Icon getIcon(int side, int metadata)
|
||||
{
|
||||
if (side == metadata)
|
||||
{
|
||||
return this.blockIcon;
|
||||
}
|
||||
|
||||
return this.machineIcon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerIcons(IconRegister iconRegister)
|
||||
{
|
||||
super.registerIcons(iconRegister);
|
||||
this.machineIcon = iconRegister.registerIcon(ResonantInduction.PREFIX + "machine");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int side, float par7, float par8, float par9)
|
||||
{
|
||||
if (entityPlayer.isSneaking())
|
||||
{
|
||||
world.setBlockMetadataWithNotify(x, y, z, ForgeDirection.ROTATION_MATRIX[world.getBlockMetadata(x, y, z)][side], 3);
|
||||
}
|
||||
else
|
||||
{
|
||||
entityPlayer.openGui(ResonantInduction.INSTNACE, 0, world, x, y, z);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int isProvidingStrongPower(IBlockAccess blockAccess, int x, int y, int z, int par5)
|
||||
{
|
||||
return this.isProvidingWeakPower(blockAccess, x, y, z, par5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int isProvidingWeakPower(IBlockAccess blockAccess, int x, int y, int z, int par5)
|
||||
{
|
||||
TileEntity tile = blockAccess.getBlockTileEntity(x, y, z);
|
||||
|
||||
if (tile instanceof TileEntityMultimeter)
|
||||
{
|
||||
return ((TileEntityMultimeter) tile).redstoneOn ? 14 : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canProvidePower()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean renderAsNormalBlock()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileEntity createNewTileEntity(World world)
|
||||
{
|
||||
|
|
|
@ -15,9 +15,11 @@ import net.minecraft.inventory.Slot;
|
|||
public class ContainerMultimeter extends Container
|
||||
{
|
||||
private final int yDisplacement = 51;
|
||||
private TileEntityMultimeter tileEntity;
|
||||
|
||||
public ContainerMultimeter(InventoryPlayer inventoryPlayer, TileEntityMultimeter tileEntity)
|
||||
{
|
||||
this.tileEntity = tileEntity;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 3; ++i)
|
||||
|
@ -32,6 +34,15 @@ public class ContainerMultimeter extends Container
|
|||
{
|
||||
this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142 + yDisplacement));
|
||||
}
|
||||
|
||||
this.tileEntity.playersUsing.add(inventoryPlayer.player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContainerClosed(EntityPlayer entityPlayer)
|
||||
{
|
||||
this.tileEntity.playersUsing.remove(entityPlayer);
|
||||
super.onContainerClosed(entityPlayer);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -3,13 +3,17 @@
|
|||
*/
|
||||
package resonantinduction.multimeter;
|
||||
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.client.gui.GuiTextField;
|
||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import resonantinduction.PacketHandler;
|
||||
import resonantinduction.ResonantInduction;
|
||||
import resonantinduction.multimeter.TileEntityMultimeter.DetectMode;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
|
@ -22,11 +26,12 @@ import cpw.mods.fml.relauncher.SideOnly;
|
|||
@SideOnly(Side.CLIENT)
|
||||
public class GuiMultimeter extends GuiContainer
|
||||
{
|
||||
private static final ResourceLocation TEXTURE = new ResourceLocation(ResonantInduction.DOMAIN, ResonantInduction.GUI_DIRECTORY + "gui_base.png");
|
||||
private static final ResourceLocation TEXTURE = new ResourceLocation(ResonantInduction.DOMAIN, ResonantInduction.GUI_DIRECTORY + "gui_multimeter.png");
|
||||
TileEntityMultimeter tileEntity;
|
||||
|
||||
private int containerWidth;
|
||||
private int containerHeight;
|
||||
private GuiTextField textFieldLimit;
|
||||
|
||||
public GuiMultimeter(InventoryPlayer inventoryPlayer, TileEntityMultimeter tileEntity)
|
||||
{
|
||||
|
@ -35,12 +40,50 @@ public class GuiMultimeter extends GuiContainer
|
|||
this.ySize = 217;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initGui()
|
||||
{
|
||||
super.initGui();
|
||||
this.buttonList.add(new GuiButton(0, this.width / 2 + 20, this.height / 2 - 30, 50, 20, "Toggle"));
|
||||
this.textFieldLimit = new GuiTextField(fontRenderer, 35, 82, 65, 12);
|
||||
this.textFieldLimit.setMaxStringLength(8);
|
||||
this.textFieldLimit.setText("" + this.tileEntity.getLimit());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void keyTyped(char par1, int par2)
|
||||
{
|
||||
super.keyTyped(par1, par2);
|
||||
this.textFieldLimit.textboxKeyTyped(par1, par2);
|
||||
|
||||
try
|
||||
{
|
||||
PacketHandler.sendTileEntityPacketToServer(this.tileEntity, (byte) 3, (float) Float.parseFloat(this.textFieldLimit.getText()));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void mouseClicked(int par1, int par2, int par3)
|
||||
{
|
||||
super.mouseClicked(par1, par2, par3);
|
||||
this.textFieldLimit.mouseClicked(par1 - this.containerWidth, par2 - this.containerHeight, par3);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawGuiContainerForegroundLayer(int par1, int par2)
|
||||
{
|
||||
String s = this.tileEntity.getBlockType().getLocalizedName();
|
||||
this.fontRenderer.drawString(s, this.xSize / 2 - this.fontRenderer.getStringWidth(s) / 2, 6, 4210752);
|
||||
this.fontRenderer.drawString("Energy: " + this.tileEntity.getDetectedEnergy(), 9, 15, 4210752);
|
||||
this.fontRenderer.drawString("Energy: " + Math.round(this.tileEntity.getDetectedEnergy()) + " J", 35, 32, 4210752);
|
||||
this.fontRenderer.drawString("Average Energy: " + Math.round(this.tileEntity.getAverageDetectedEnergy()) + " J", 35, 20, 4210752);
|
||||
this.fontRenderer.drawString("Output Redstone If... ", 35, 46, 4210752);
|
||||
this.fontRenderer.drawString(this.tileEntity.getMode().display, 35, 65, 4210752);
|
||||
this.fontRenderer.drawString("KiloJoules", 35, 100, 4210752);
|
||||
|
||||
this.textFieldLimit.drawTextBox();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -52,6 +95,18 @@ public class GuiMultimeter extends GuiContainer
|
|||
this.mc.renderEngine.func_110577_a(TEXTURE);
|
||||
GL11.glColor4f(1, 1, 1, 1);
|
||||
this.drawTexturedModalRect(this.containerWidth, this.containerHeight, 0, 0, this.xSize, this.ySize);
|
||||
|
||||
/*if (this.tileEntity.getMode() != DetectMode.NONE)
|
||||
{
|
||||
int length = (int) (Math.abs(this.tileEntity.getDetectedEnergy() - this.tileEntity.getLimit()) / this.tileEntity.getLimit()) * 110;
|
||||
this.drawTexturedModalRect(this.containerWidth + 13, this.containerHeight + 128 - length, 176, 0, 30, length);
|
||||
}*/
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void actionPerformed(GuiButton button)
|
||||
{
|
||||
PacketHandler.sendTileEntityPacketToServer(this.tileEntity, (byte) 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,21 +3,182 @@
|
|||
*/
|
||||
package resonantinduction.multimeter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.packet.Packet;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.ForgeDirection;
|
||||
import resonantinduction.PacketHandler;
|
||||
import resonantinduction.ResonantInduction;
|
||||
import resonantinduction.base.IPacketReceiver;
|
||||
import resonantinduction.base.TileEntityBase;
|
||||
import resonantinduction.tesla.TileEntityTesla;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
/**
|
||||
* @author Calclavia
|
||||
*
|
||||
*/
|
||||
public class TileEntityMultimeter extends TileEntityBase
|
||||
public class TileEntityMultimeter extends TileEntityBase implements IPacketReceiver
|
||||
{
|
||||
public float getDetectedEnergy()
|
||||
public enum DetectMode
|
||||
{
|
||||
NONE("None"), LESS_THAN("Less Than"), LESS_THAN_EQUAL("Less Than or Equal"),
|
||||
EQUAL("Equal"), GREATER_THAN("Greater Than or Equal"), GREATER_THAN_EQUAL("Greater Than");
|
||||
|
||||
public String display;
|
||||
|
||||
private DetectMode(String display)
|
||||
{
|
||||
this.display = display;
|
||||
}
|
||||
}
|
||||
|
||||
private DetectMode detectMode = DetectMode.NONE;
|
||||
private float energyLimit;
|
||||
private float detectedEnergy;
|
||||
private float detectedAverageEnergy;
|
||||
public boolean redstoneOn;
|
||||
|
||||
@Override
|
||||
public void updateEntity()
|
||||
{
|
||||
super.updateEntity();
|
||||
|
||||
if (this.ticks % 20 == 0)
|
||||
{
|
||||
this.detectedEnergy = this.doGetDetectedEnergy();
|
||||
this.detectedAverageEnergy = (detectedAverageEnergy + this.detectedEnergy) / 2;
|
||||
|
||||
boolean outputRedstone = false;
|
||||
|
||||
switch (detectMode)
|
||||
{
|
||||
default:
|
||||
break;
|
||||
case EQUAL:
|
||||
outputRedstone = this.detectedEnergy == this.energyLimit;
|
||||
break;
|
||||
case GREATER_THAN:
|
||||
outputRedstone = this.detectedEnergy > this.energyLimit;
|
||||
break;
|
||||
case GREATER_THAN_EQUAL:
|
||||
outputRedstone = this.detectedEnergy >= this.energyLimit;
|
||||
break;
|
||||
case LESS_THAN:
|
||||
outputRedstone = this.detectedEnergy < this.energyLimit;
|
||||
break;
|
||||
case LESS_THAN_EQUAL:
|
||||
outputRedstone = this.detectedEnergy <= this.energyLimit;
|
||||
break;
|
||||
}
|
||||
|
||||
if (outputRedstone != this.redstoneOn)
|
||||
{
|
||||
this.redstoneOn = outputRedstone;
|
||||
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, ResonantInduction.blockMultimeter.blockID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet getDescriptionPacket()
|
||||
{
|
||||
return PacketHandler.getTileEntityPacket(this, (byte) 1, (byte) this.detectMode.ordinal(), this.energyLimit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(ByteArrayDataInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (input.readByte())
|
||||
{
|
||||
default:
|
||||
this.detectMode = DetectMode.values()[input.readByte()];
|
||||
this.energyLimit = input.readFloat();
|
||||
break;
|
||||
case 2:
|
||||
this.toggleMode();
|
||||
break;
|
||||
case 3:
|
||||
this.energyLimit = input.readFloat();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList getNetworkedData(ArrayList data)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public float doGetDetectedEnergy()
|
||||
{
|
||||
ForgeDirection direction = ForgeDirection.getOrientation(this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord));
|
||||
ForgeDirection opp = direction.getOpposite();
|
||||
TileEntity tileEntity = this.worldObj.getBlockTileEntity(this.xCoord + opp.offsetX, this.yCoord + opp.offsetY, this.zCoord + opp.offsetZ);
|
||||
|
||||
// TODO: Universal Compatiblity in the future.
|
||||
if (tileEntity instanceof TileEntityTesla)
|
||||
{
|
||||
return ((TileEntityTesla) tileEntity).getEnergyStored();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean canUpdate()
|
||||
public float getDetectedEnergy()
|
||||
{
|
||||
return false;
|
||||
return this.detectedEnergy;
|
||||
}
|
||||
|
||||
public float getAverageDetectedEnergy()
|
||||
{
|
||||
return this.detectedAverageEnergy;
|
||||
}
|
||||
|
||||
public void toggleMode()
|
||||
{
|
||||
this.detectMode = DetectMode.values()[(this.detectMode.ordinal() + 1) % DetectMode.values().length];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a tile entity from NBT.
|
||||
*/
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbt)
|
||||
{
|
||||
super.readFromNBT(nbt);
|
||||
this.detectMode = DetectMode.values()[nbt.getInteger("detectMode")];
|
||||
this.energyLimit = nbt.getFloat("energyLimit");
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a tile entity to NBT.
|
||||
*/
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound nbt)
|
||||
{
|
||||
super.writeToNBT(nbt);
|
||||
nbt.setInteger("detectMode", this.detectMode.ordinal());
|
||||
nbt.setFloat("energyLimit", this.energyLimit);
|
||||
}
|
||||
|
||||
public DetectMode getMode()
|
||||
{
|
||||
return this.detectMode;
|
||||
}
|
||||
|
||||
public float getLimit()
|
||||
{
|
||||
return this.energyLimit;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -339,7 +339,7 @@ public class TileEntityTesla extends TileEntityBase implements ITesla, IPacketRe
|
|||
|
||||
public int getRange()
|
||||
{
|
||||
return Math.min(5 * (this.getHeight() - 1), 50);
|
||||
return Math.min(4 * (this.getHeight() - 1), 50);
|
||||
}
|
||||
|
||||
public void updatePositionStatus()
|
||||
|
|
Loading…
Reference in a new issue