Got multimeter working with wires

This commit is contained in:
Calclavia 2013-08-24 20:00:33 +08:00
parent 4eede3ad02
commit b19963a050
4 changed files with 87 additions and 7 deletions

View file

@ -8,6 +8,7 @@ import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.oredict.ShapedOreRecipe;
import org.modstats.ModstatInfo;
@ -23,6 +24,7 @@ import resonantinduction.entangler.ItemLinker;
import resonantinduction.entangler.ItemQuantumEntangler;
import resonantinduction.multimeter.BlockMultimeter;
import resonantinduction.multimeter.ItemBlockMultimeter;
import resonantinduction.multimeter.MultimeterEventHandler;
import resonantinduction.multimeter.TileEntityMultimeter;
import resonantinduction.tesla.BlockTesla;
import resonantinduction.tesla.TileEntityTesla;
@ -144,7 +146,7 @@ public class ResonantInduction
LOGGER.setParent(FMLLog.getLogger());
NetworkRegistry.instance().registerGuiHandler(this, ResonantInduction.proxy);
Modstats.instance().getReporter().registerMod(this);
MinecraftForge.EVENT_BUS.register(new MultimeterEventHandler());
CONFIGURATION.load();
// Config

View file

@ -602,7 +602,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
@Override
public float getProvide(ForgeDirection direction)
{
return Math.max(this.getEnergyStored(), this.transferThreshold);
return Math.min(this.getEnergyStored(), this.transferThreshold);
}
@Override

View file

@ -0,0 +1,42 @@
package resonantinduction.multimeter;
import java.util.HashMap;
import net.minecraft.world.World;
import net.minecraftforge.event.ForgeSubscribe;
import universalelectricity.core.electricity.ElectricalEvent.ElectricityProductionEvent;
import universalelectricity.core.grid.IElectricityNetwork;
/**
* @author Calclavia
*
*/
public class MultimeterEventHandler
{
private static final HashMap<IElectricityNetwork, Float> networkEnergyCache = new HashMap<IElectricityNetwork, Float>();
private static long lastCheckTime = 0;
public static HashMap<IElectricityNetwork, Float> getCache(World worldObj)
{
HashMap<IElectricityNetwork, Float> returnCache = (HashMap<IElectricityNetwork, Float>) networkEnergyCache.clone();
if (Math.abs(worldObj.getWorldTime() - lastCheckTime) >= 40)
{
lastCheckTime = worldObj.getWorldTime();
networkEnergyCache.clear();
}
return returnCache;
}
@ForgeSubscribe
public void event(ElectricityProductionEvent evt)
{
IElectricityNetwork network = evt.network;
if (evt.electricityPack.getWatts() != 0)
{
networkEnergyCache.put(network, evt.electricityPack.getWatts());
}
}
}

View file

@ -15,7 +15,11 @@ import net.minecraftforge.common.ForgeDirection;
import resonantinduction.PacketHandler;
import resonantinduction.ResonantInduction;
import resonantinduction.base.IPacketReceiver;
import universalelectricity.core.block.IConductor;
import universalelectricity.core.block.IConnector;
import universalelectricity.core.block.IElectricalStorage;
import universalelectricity.core.grid.IElectricityNetwork;
import universalelectricity.prefab.tile.IRotatable;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import universalelectricity.prefab.tile.TileEntityElectrical;
@ -30,7 +34,7 @@ import cpw.mods.fml.common.network.Player;
* @author Calclavia
*
*/
public class TileEntityMultimeter extends TileEntityAdvanced implements IPacketReceiver
public class TileEntityMultimeter extends TileEntityAdvanced implements IPacketReceiver, IConnector, IRotatable
{
public Set<EntityPlayer> playersUsing = new HashSet<EntityPlayer>();
@ -64,9 +68,7 @@ public class TileEntityMultimeter extends TileEntityAdvanced implements IPacketR
if (this.ticks % 20 == 0)
{
float prevDetectedEnergy = this.detectedEnergy;
this.detectedEnergy = this.doGetDetectedEnergy();
this.detectedAverageEnergy = (detectedAverageEnergy + this.detectedEnergy) / 2;
this.peakDetection = Math.max(peakDetection, this.detectedEnergy);
this.updateDetection(this.doGetDetectedEnergy());
boolean outputRedstone = false;
@ -153,7 +155,7 @@ public class TileEntityMultimeter extends TileEntityAdvanced implements IPacketR
public float doGetDetectedEnergy()
{
ForgeDirection direction = ForgeDirection.getOrientation(this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord));
ForgeDirection direction = this.getDirection();
TileEntity tileEntity = this.worldObj.getBlockTileEntity(this.xCoord + direction.offsetX, this.yCoord + direction.offsetY, this.zCoord + direction.offsetZ);
return getDetectedEnergy(tileEntity);
}
@ -168,10 +170,26 @@ public class TileEntityMultimeter extends TileEntityAdvanced implements IPacketR
{
return ((IElectricalStorage) tileEntity).getEnergyStored();
}
else if (tileEntity instanceof IConductor)
{
IElectricityNetwork network = ((IConductor) tileEntity).getNetwork();
if (MultimeterEventHandler.getCache(tileEntity.worldObj).containsKey(network) && MultimeterEventHandler.getCache(tileEntity.worldObj).get(network) instanceof Float)
{
return MultimeterEventHandler.getCache(tileEntity.worldObj).get(network);
}
}
return 0;
}
public void updateDetection(float detected)
{
this.detectedEnergy = detected;
this.detectedAverageEnergy = (detectedAverageEnergy + this.detectedEnergy) / 2;
this.peakDetection = Math.max(peakDetection, this.detectedEnergy);
}
public float getDetectedEnergy()
{
return this.detectedEnergy;
@ -227,4 +245,22 @@ public class TileEntityMultimeter extends TileEntityAdvanced implements IPacketR
return this.peakDetection;
}
@Override
public boolean canConnect(ForgeDirection direction)
{
return direction == this.getDirection();
}
@Override
public ForgeDirection getDirection()
{
return ForgeDirection.getOrientation(this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord));
}
@Override
public void setDirection(ForgeDirection direction)
{
this.worldObj.setBlockMetadataWithNotify(this.xCoord, this.yCoord, this.zCoord, direction.ordinal(), 3);
}
}