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

235 lines
5.2 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-07-15 09:25:03 +02:00
package appeng.parts.p2p;
2015-06-16 02:44:59 +02:00
import java.io.IOException;
import java.util.List;
2015-06-16 02:44:59 +02:00
2015-12-24 02:07:03 +01:00
import io.netty.buffer.ByteBuf;
2014-07-15 09:25:03 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
2014-07-15 09:25:03 +02:00
import net.minecraft.world.World;
2015-12-24 02:07:03 +01:00
2014-07-15 09:25:03 +02:00
import appeng.api.networking.IGridNode;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.core.settings.TickRates;
import appeng.items.parts.PartModels;
2014-07-15 09:25:03 +02:00
import appeng.me.GridAccessException;
2014-07-15 09:25:03 +02:00
public class PartP2PLight extends PartP2PTunnel<PartP2PLight> implements IGridTickable
{
private static final P2PModels MODELS = new P2PModels( "part/p2p/p2p_tunnel_light" );
@PartModels
public static List<ResourceLocation> getModels()
{
return MODELS.getModels();
}
private int lastValue = 0;
private float opacity = -1;
2014-07-15 09:25:03 +02:00
2015-09-30 14:24:40 +02:00
public PartP2PLight( final ItemStack is )
2014-07-15 09:25:03 +02:00
{
super( is );
2014-07-15 09:25:03 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void chanRender( final MENetworkChannelsChanged c )
2014-07-15 09:25:03 +02:00
{
2014-12-29 15:13:47 +01:00
this.onTunnelNetworkChange();
2014-07-15 09:25:03 +02:00
super.chanRender( c );
}
@Override
2015-09-30 14:24:40 +02:00
public void powerRender( final MENetworkPowerStatusChange c )
2014-07-15 09:25:03 +02:00
{
2014-12-29 15:13:47 +01:00
this.onTunnelNetworkChange();
2014-07-15 09:25:03 +02:00
super.powerRender( c );
}
@Override
2015-09-30 14:24:40 +02:00
public void writeToStream( final ByteBuf data ) throws IOException
2014-07-15 09:25:03 +02:00
{
super.writeToStream( data );
data.writeInt( this.isOutput() ? this.lastValue : 0 );
2014-07-15 09:25:03 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean readFromStream( final ByteBuf data ) throws IOException
2014-07-15 09:25:03 +02:00
{
super.readFromStream( data );
this.lastValue = data.readInt();
this.setOutput( this.lastValue > 0 );
return false;
}
private boolean doWork()
{
if( this.isOutput() )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final TileEntity te = this.getTile();
final World w = te.getWorld();
final int newLevel = w.getLightFromNeighbors( te.getPos().offset( this.getSide().getFacing() ) );
if( this.lastValue != newLevel && this.getProxy().isActive() )
{
this.lastValue = newLevel;
try
{
2015-09-30 14:24:40 +02:00
for( final PartP2PLight out : this.getOutputs() )
2015-04-29 02:30:53 +02:00
{
out.setLightLevel( this.lastValue );
2015-04-29 02:30:53 +02:00
}
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
{
// :P
}
return true;
}
return false;
2014-07-15 09:25:03 +02:00
}
@Override
public void onNeighborChanged()
{
2014-12-29 15:13:47 +01:00
this.opacity = -1;
2014-12-29 15:13:47 +01:00
this.doWork();
if( this.isOutput() )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.getHost().markForUpdate();
2015-04-29 02:30:53 +02:00
}
2014-07-15 09:25:03 +02:00
}
@Override
public int getLightLevel()
2014-07-15 09:25:03 +02:00
{
if( this.isOutput() && this.isPowered() )
2015-04-29 02:30:53 +02:00
{
return this.blockLight( this.lastValue );
2015-04-29 02:30:53 +02:00
}
2014-07-15 09:25:03 +02:00
return 0;
2014-07-15 09:25:03 +02:00
}
private void setLightLevel( final int out )
2014-07-15 09:25:03 +02:00
{
this.lastValue = out;
this.getHost().markForUpdate();
2014-07-15 09:25:03 +02:00
}
2015-09-30 14:24:40 +02:00
private int blockLight( final int emit )
2014-07-15 09:25:03 +02:00
{
if( this.opacity < 0 )
{
2015-09-30 14:24:40 +02:00
final TileEntity te = this.getTile();
this.opacity = 255 - te.getWorld().getBlockLightOpacity( te.getPos().offset( this.getSide().getFacing() ) );
}
return (int) ( emit * ( this.opacity / 255.0f ) );
2014-07-15 09:25:03 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void readFromNBT( final NBTTagCompound tag )
2014-07-15 09:25:03 +02:00
{
super.readFromNBT( tag );
if( tag.hasKey( "opacity" ) )
2015-04-29 02:30:53 +02:00
{
this.opacity = tag.getFloat( "opacity" );
2015-04-29 02:30:53 +02:00
}
this.lastValue = tag.getInteger( "lastValue" );
2014-07-15 09:25:03 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void writeToNBT( final NBTTagCompound tag )
2014-07-15 09:25:03 +02:00
{
super.writeToNBT( tag );
tag.setFloat( "opacity", this.opacity );
tag.setInteger( "lastValue", this.lastValue );
}
2014-07-15 09:25:03 +02:00
@Override
public void onTunnelConfigChange()
{
this.onTunnelNetworkChange();
}
2014-07-15 09:25:03 +02:00
@Override
public void onTunnelNetworkChange()
{
if( this.isOutput() )
2014-07-15 09:25:03 +02:00
{
2015-09-30 14:24:40 +02:00
final PartP2PLight src = this.getInput();
if( src != null && src.getProxy().isActive() )
2015-04-29 02:30:53 +02:00
{
this.setLightLevel( src.lastValue );
2015-04-29 02:30:53 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
this.getHost().markForUpdate();
2015-04-29 02:30:53 +02:00
}
2014-07-15 09:25:03 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
this.doWork();
2015-04-29 02:30:53 +02:00
}
2014-07-15 09:25:03 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public TickingRequest getTickingRequest( final IGridNode node )
2014-07-16 05:34:39 +02:00
{
return new TickingRequest( TickRates.LightTunnel.getMin(), TickRates.LightTunnel.getMax(), false, false );
2014-07-16 05:34:39 +02:00
}
2014-07-15 09:25:03 +02:00
@Override
2015-09-30 14:24:40 +02:00
public TickRateModulation tickingRequest( final IGridNode node, final int ticksSinceLastCall )
2014-07-15 09:25:03 +02:00
{
2014-12-29 15:13:47 +01:00
return this.doWork() ? TickRateModulation.FASTER : TickRateModulation.SLOWER;
2014-07-15 09:25:03 +02:00
}
public float getPowerDrainPerTick()
{
return 0.5f;
}
@Override
public List<ResourceLocation> getStaticModels()
{
return MODELS.getModel( isPowered(), isActive() );
}
2014-07-15 09:25:03 +02:00
}