Applied-Energistics-2-tiler.../src/main/java/appeng/parts/p2p/PartP2PRedstone.java

199 lines
4.5 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.parts.p2p;
2014-09-24 02:26:27 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockRedstoneWire;
2015-06-16 02:44:59 +02:00
import net.minecraft.block.state.IBlockState;
2014-09-24 02:26:27 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
2014-09-24 02:26:27 +02:00
import net.minecraft.world.World;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.networking.events.MENetworkBootingStatusChange;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.me.GridAccessException;
import appeng.util.Platform;
2014-09-24 02:26:27 +02:00
public class PartP2PRedstone extends PartP2PTunnel<PartP2PRedstone>
{
private int power;
private boolean recursive = false;
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
public PartP2PRedstone( final ItemStack is )
2014-09-24 02:26:27 +02:00
{
super( is );
2014-09-24 02:26:27 +02:00
}
@MENetworkEventSubscribe
2015-09-30 14:24:40 +02:00
public void changeStateA( final MENetworkBootingStatusChange bs )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.setNetworkReady();
2014-09-24 02:26:27 +02:00
}
private void setNetworkReady()
2014-09-24 02:26:27 +02:00
{
if( this.isOutput() )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final PartP2PRedstone in = this.getInput();
if( in != null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.putInput( in.power );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
private void putInput( final Object o )
2014-09-24 02:26:27 +02:00
{
if( this.recursive )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.recursive = true;
if( this.isOutput() && this.getProxy().isActive() )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final int newPower = (Integer) o;
if( this.power != newPower )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.power = newPower;
this.notifyNeighbors();
2014-09-24 02:26:27 +02:00
}
}
2014-12-29 15:13:47 +01:00
this.recursive = false;
2014-09-24 02:26:27 +02:00
}
private void notifyNeighbors()
2014-09-24 02:26:27 +02:00
{
final World worldObj = this.getTile().getWorld();
2014-09-24 02:26:27 +02:00
Platform.notifyBlocksOfNeighbors( worldObj, this.getTile().getPos() );
2014-09-24 02:26:27 +02:00
// and this cause sometimes it can go thought walls.
for( final EnumFacing face : EnumFacing.VALUES )
2015-12-24 02:11:17 +01:00
{
Platform.notifyBlocksOfNeighbors( worldObj, this.getTile().getPos().offset( face ) );
2015-12-24 02:11:17 +01:00
}
2014-09-24 02:26:27 +02:00
}
@MENetworkEventSubscribe
2015-09-30 14:24:40 +02:00
public void changeStateB( final MENetworkChannelsChanged bs )
{
this.setNetworkReady();
}
@MENetworkEventSubscribe
2015-09-30 14:24:40 +02:00
public void changeStateC( final MENetworkPowerStatusChange bs )
{
this.setNetworkReady();
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public void readFromNBT( final NBTTagCompound tag )
2014-09-24 02:26:27 +02:00
{
super.readFromNBT( tag );
2014-12-29 15:13:47 +01:00
this.power = tag.getInteger( "power" );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void writeToNBT( final NBTTagCompound tag )
2014-09-24 02:26:27 +02:00
{
super.writeToNBT( tag );
tag.setInteger( "power", this.power );
}
@Override
public void onTunnelNetworkChange()
{
this.setNetworkReady();
2014-09-24 02:26:27 +02:00
}
public float getPowerDrainPerTick()
{
return 0.5f;
2014-09-28 00:50:06 +02:00
}
2014-09-24 02:26:27 +02:00
@Override
public void onNeighborChanged()
{
if( !this.isOutput() )
2014-09-24 02:26:27 +02:00
{
final BlockPos target = this.getTile().getPos().offset( this.getSide().getFacing() );
2014-09-24 02:26:27 +02:00
final IBlockState state = this.getTile().getWorld().getBlockState( target );
2015-09-30 14:24:40 +02:00
final Block b = state.getBlock();
if( b != null && !this.isOutput() )
2014-09-24 02:26:27 +02:00
{
EnumFacing srcSide = this.getSide().getFacing();
if( b instanceof BlockRedstoneWire )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
srcSide = EnumFacing.UP;
2015-04-29 02:30:53 +02:00
}
this.power = b.isProvidingStrongPower( this.getTile().getWorld(), target, state, srcSide );
this.power = Math.max( this.power, b.isProvidingWeakPower( this.getTile().getWorld(), target, state, srcSide ) );
2014-12-29 15:13:47 +01:00
this.sendToOutput( this.power );
2014-09-24 02:26:27 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.sendToOutput( 0 );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
@Override
public boolean canConnectRedstone()
{
return true;
}
@Override
public int isProvidingStrongPower()
{
return this.isOutput() ? this.power : 0;
}
@Override
public int isProvidingWeakPower()
{
return this.isOutput() ? this.power : 0;
}
2015-09-30 14:24:40 +02:00
private void sendToOutput( final int power )
2014-09-24 02:26:27 +02:00
{
try
{
2015-09-30 14:24:40 +02:00
for( final PartP2PRedstone rs : this.getOutputs() )
2014-09-24 02:26:27 +02:00
{
rs.putInput( power );
}
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
2014-09-24 02:26:27 +02:00
{
// :P
}
}
}