Applied-Energistics-2-tiler.../src/main/java/appeng/tile/networking/TileCableBus.java

426 lines
10 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 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* 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.tile.networking;
2014-09-24 02:26:27 +02:00
import java.io.IOException;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
2014-09-24 02:26:27 +02:00
2015-12-24 02:07:03 +01:00
import io.netty.buffer.ByteBuf;
2014-09-24 02:26:27 +02:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
2014-09-24 02:26:27 +02:00
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.networking.IGridNode;
import appeng.api.parts.IFacadeContainer;
import appeng.api.parts.IPart;
import appeng.api.parts.LayerFlags;
import appeng.api.parts.SelectedPart;
import appeng.api.util.AECableType;
import appeng.api.util.AEColor;
2015-06-16 02:44:59 +02:00
import appeng.api.util.AEPartLocation;
2014-09-24 02:26:27 +02:00
import appeng.api.util.DimensionalCoord;
import appeng.block.networking.BlockCableBus;
2014-09-24 02:26:27 +02:00
import appeng.helpers.AEMultiTile;
import appeng.helpers.ICustomCollision;
import appeng.hooks.TickHandler;
import appeng.integration.IntegrationRegistry;
2014-09-24 02:26:27 +02:00
import appeng.integration.IntegrationType;
import appeng.integration.abstraction.IImmibisMicroblocks;
import appeng.parts.CableBusContainer;
import appeng.tile.AEBaseTile;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
import appeng.util.Platform;
2014-09-24 02:26:27 +02:00
public class TileCableBus extends AEBaseTile implements AEMultiTile, ICustomCollision
{
private CableBusContainer cb = new CableBusContainer( this );
2014-09-24 02:26:27 +02:00
private int oldLV = -1; // on re-calculate light when it changes
@TileEvent( TileEventType.WORLD_NBT_READ )
2015-09-30 14:24:40 +02:00
public void readFromNBT_TileCableBus( final NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
this.getCableBus().readFromNBT( data );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.WORLD_NBT_WRITE )
2015-09-30 14:24:40 +02:00
public void writeToNBT_TileCableBus( final NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
this.getCableBus().writeToNBT( data );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.NETWORK_READ )
2015-09-30 14:24:40 +02:00
public boolean readFromStream_TileCableBus( final ByteBuf data ) throws IOException
2014-09-24 02:26:27 +02:00
{
final boolean ret = this.getCableBus().readFromStream( data );
2014-09-24 02:26:27 +02:00
final int newLV = this.getCableBus().getLightValue();
if( newLV != this.oldLV )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.oldLV = newLV;
2015-09-30 14:26:54 +02:00
this.worldObj.checkLight( this.pos );
2014-09-24 02:26:27 +02:00
}
this.updateTileSetting();
2014-09-24 02:26:27 +02:00
return ret;
}
/**
* Changes this tile to the TESR version if any of the parts require dynamic rendering.
*/
protected void updateTileSetting()
{
if( this.getCableBus().isRequiresDynamicRender() )
{
try
{
final TileCableBus tcb = (TileCableBus) BlockCableBus.getTesrTile().newInstance();
tcb.copyFrom( this );
this.getWorld().setTileEntity( pos, tcb );
}
catch( final Throwable ignored )
{
}
}
}
2015-09-30 14:24:40 +02:00
protected void copyFrom( final TileCableBus oldTile )
2014-09-24 02:26:27 +02:00
{
final CableBusContainer tmpCB = this.getCableBus();
this.setCableBus( oldTile.getCableBus() );
2014-12-29 15:13:47 +01:00
this.oldLV = oldTile.oldLV;
oldTile.setCableBus( tmpCB );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.NETWORK_WRITE )
2015-09-30 14:24:40 +02:00
public void writeToStream_TileCableBus( final ByteBuf data ) throws IOException
{
this.getCableBus().writeToStream( data );
}
2014-09-24 02:26:27 +02:00
@Override
public double getMaxRenderDistanceSquared()
2014-09-24 02:26:27 +02:00
{
return 900.0;
2014-09-24 02:26:27 +02:00
}
@Override
public void invalidate()
2014-09-24 02:26:27 +02:00
{
super.invalidate();
this.getCableBus().removeFromWorld();
2014-09-24 02:26:27 +02:00
}
@Override
public void validate()
{
super.validate();
2015-01-01 22:13:10 +01:00
TickHandler.INSTANCE.addInit( this );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public IGridNode getGridNode( final AEPartLocation dir )
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().getGridNode( dir );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public AECableType getCableConnectionType( final AEPartLocation side )
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().getCableConnectionType( side );
2014-09-24 02:26:27 +02:00
}
@Override
public float getCableConnectionLength( AECableType cable )
{
return this.getCableBus().getCableConnectionLength( cable );
}
2014-09-24 02:26:27 +02:00
@Override
public void onChunkUnload()
2014-09-24 02:26:27 +02:00
{
super.onChunkUnload();
this.getCableBus().removeFromWorld();
2014-09-24 02:26:27 +02:00
}
@Override
public void markForUpdate()
2014-09-24 02:26:27 +02:00
{
if( this.worldObj == null )
2015-04-29 02:30:53 +02:00
{
return;
2015-04-29 02:30:53 +02:00
}
final int newLV = this.getCableBus().getLightValue();
if( newLV != this.oldLV )
{
this.oldLV = newLV;
this.worldObj.checkLight( this.pos );
// worldObj.updateAllLightTypes( xCoord, yCoord, zCoord );
}
super.markForUpdate();
2014-09-24 02:26:27 +02:00
}
@Override
public boolean canBeRotated()
2014-09-24 02:26:27 +02:00
{
return false;
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void getDrops( final World w, final BlockPos pos, final List drops )
2014-09-24 02:26:27 +02:00
{
this.getCableBus().getDrops( drops );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void getNoDrops( final World w, final BlockPos pos, final List<ItemStack> drops )
2014-09-24 02:26:27 +02:00
{
this.getCableBus().getNoDrops( drops );
2014-09-24 02:26:27 +02:00
}
@Override
public void onReady()
2014-09-24 02:26:27 +02:00
{
super.onReady();
if( this.getCableBus().isEmpty() )
{
2015-09-30 14:26:54 +02:00
if( this.worldObj.getTileEntity( this.pos ) == this )
2015-04-29 02:30:53 +02:00
{
2015-09-30 14:26:54 +02:00
this.worldObj.destroyBlock( this.pos, true );
2015-04-29 02:30:53 +02:00
}
}
else
2015-04-29 02:30:53 +02:00
{
this.getCableBus().addToWorld();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
@Override
public boolean requiresTESR()
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().isRequiresDynamicRender();
2014-09-24 02:26:27 +02:00
}
@Override
public IFacadeContainer getFacadeContainer()
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().getFacadeContainer();
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean canAddPart( final ItemStack is, final AEPartLocation side )
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().canAddPart( is, side );
2014-09-24 02:26:27 +02:00
}
@Override
public AEPartLocation addPart( final ItemStack is, final AEPartLocation side, final EntityPlayer player, final EnumHand hand )
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().addPart( is, side, player, hand );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public IPart getPart( final AEPartLocation side )
2015-06-16 02:44:59 +02:00
{
return this.cb.getPart( side );
}
@Override
2015-09-30 14:24:40 +02:00
public IPart getPart( final EnumFacing side )
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().getPart( side );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void removePart( final AEPartLocation side, final boolean suppressUpdate )
2014-09-24 02:26:27 +02:00
{
this.getCableBus().removePart( side, suppressUpdate );
2014-09-24 02:26:27 +02:00
}
@Override
public DimensionalCoord getLocation()
2014-09-24 02:26:27 +02:00
{
return new DimensionalCoord( this );
2014-09-24 02:26:27 +02:00
}
@Override
public AEColor getColor()
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().getColor();
2014-09-24 02:26:27 +02:00
}
@Override
public void clearContainer()
{
this.setCableBus( new CableBusContainer( this ) );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isBlocked( final EnumFacing side )
2014-09-24 02:26:27 +02:00
{
// TODO 1.10.2-R - Stuff.
return false;
2015-06-16 02:44:59 +02:00
}
2015-06-16 02:44:59 +02:00
@Override
2015-09-30 14:24:40 +02:00
public Iterable<AxisAlignedBB> getSelectedBoundingBoxesFromPool( final World w, final BlockPos pos, final Entity e, final boolean visual )
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().getSelectedBoundingBoxesFromPool( false, true, e, visual );
2014-09-24 02:26:27 +02:00
}
@Override
public SelectedPart selectPart( final Vec3d pos )
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().selectPart( pos );
2014-09-24 02:26:27 +02:00
}
@Override
public void markForSave()
2014-09-24 02:26:27 +02:00
{
super.markDirty();
2014-09-24 02:26:27 +02:00
}
@Override
public void partChanged()
2014-09-24 02:26:27 +02:00
{
this.notifyNeighbors();
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean hasRedstone( final AEPartLocation side )
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().hasRedstone( side );
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isEmpty()
{
return this.getCableBus().isEmpty();
2014-09-24 02:26:27 +02:00
}
@Override
public Set<LayerFlags> getLayerFlags()
{
return this.getCableBus().getLayerFlags();
2014-09-24 02:26:27 +02:00
}
@Override
public void cleanup()
{
if( IntegrationRegistry.INSTANCE.isEnabled( IntegrationType.ImmibisMicroblocks ) )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final IImmibisMicroblocks imb = (IImmibisMicroblocks) IntegrationRegistry.INSTANCE.getInstance( IntegrationType.ImmibisMicroblocks );
if( imb != null && imb.leaveParts( this ) )
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
}
2015-09-30 14:26:54 +02:00
this.getWorld().setBlockToAir( this.pos );
2015-06-16 02:44:59 +02:00
}
2015-06-16 02:44:59 +02:00
@Override
public void addCollidingBlockToList( final World w, final BlockPos pos, final AxisAlignedBB bb, final List<AxisAlignedBB> out, final Entity e )
2015-06-16 02:44:59 +02:00
{
2015-09-30 14:24:40 +02:00
for( final AxisAlignedBB bx : this.getSelectedBoundingBoxesFromPool( w, pos, e, false ) )
2015-04-29 02:30:53 +02:00
{
out.add( new AxisAlignedBB( bx.minX, bx.minY, bx.minZ, bx.maxX, bx.maxY, bx.maxZ ) );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
@Override
public void notifyNeighbors()
{
2015-09-30 14:26:54 +02:00
if( this.worldObj != null && this.worldObj.isBlockLoaded( this.pos ) && !CableBusContainer.isLoading() )
2015-04-29 02:30:53 +02:00
{
2015-09-30 14:26:54 +02:00
Platform.notifyBlocksOfNeighbors( this.worldObj, this.pos );
2015-04-29 02:30:53 +02:00
}
}
2014-09-24 02:26:27 +02:00
@Override
public boolean isInWorld()
{
return this.getCableBus().isInWorld();
}
2014-09-24 02:26:27 +02:00
@Override
public boolean recolourBlock( final EnumFacing side, final AEColor colour, final EntityPlayer who )
2014-09-24 02:26:27 +02:00
{
return this.getCableBus().recolourBlock( side, colour, who );
2014-09-24 02:26:27 +02:00
}
public CableBusContainer getCableBus()
{
return this.cb;
}
private void setCableBus( final CableBusContainer cb )
{
this.cb = cb;
}
@Override
public boolean hasCapability( Capability<?> capabilityClass, @Nullable EnumFacing fromSide )
{
// Note that null will be translated to INTERNAL here
AEPartLocation partLocation = AEPartLocation.fromFacing( fromSide );
IPart part = getPart( partLocation );
boolean result = part != null && part.hasCapability( capabilityClass );
return result || super.hasCapability( capabilityClass, fromSide );
}
@Override
public <T> T getCapability( Capability<T> capabilityClass, @Nullable EnumFacing fromSide )
{
// Note that null will be translated to INTERNAL here
AEPartLocation partLocation = AEPartLocation.fromFacing( fromSide );
IPart part = getPart( partLocation );
T result = part == null ? null : part.getCapability( capabilityClass );
if( result != null )
{
return result;
}
return super.getCapability( capabilityClass, fromSide );
}
2014-09-24 02:26:27 +02:00
}