Applied-Energistics-2-tiler.../src/main/java/appeng/fmp/CableBusPart.java

644 lines
15 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.fmp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
2014-12-29 21:59:05 +01:00
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
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;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraftforge.common.util.ForgeDirection;
2014-12-29 21:59:05 +01:00
import codechicken.lib.data.MCDataInput;
import codechicken.lib.data.MCDataOutput;
import codechicken.lib.raytracer.IndexedCuboid6;
import codechicken.lib.vec.Cuboid6;
import codechicken.lib.vec.Vector3;
import codechicken.multipart.IRedstonePart;
import codechicken.multipart.JCuboidPart;
import codechicken.multipart.JNormalOcclusion;
import codechicken.multipart.NormalOcclusionTest;
import codechicken.multipart.NormallyOccludedPart;
import codechicken.multipart.TMultiPart;
import codechicken.multipart.scalatraits.TIInventoryTile;
2014-09-24 02:26:27 +02:00
import appeng.api.implementations.parts.IPartCable;
import appeng.api.networking.IGridNode;
import appeng.api.parts.IFacadeContainer;
import appeng.api.parts.IFacadePart;
import appeng.api.parts.IPart;
import appeng.api.parts.IPartCollisionHelper;
import appeng.api.parts.IPartItem;
import appeng.api.parts.LayerFlags;
import appeng.api.parts.PartItemStack;
import appeng.api.parts.SelectedPart;
import appeng.api.util.AECableType;
import appeng.api.util.AEColor;
import appeng.api.util.DimensionalCoord;
import appeng.client.render.BusRenderHelper;
import appeng.client.render.BusRenderer;
import appeng.core.AEConfig;
import appeng.core.AELog;
import appeng.core.features.AEFeature;
import appeng.helpers.AEMultiTile;
import appeng.parts.BusCollisionHelper;
import appeng.parts.CableBusContainer;
import appeng.parts.PartPlacement;
import appeng.tile.networking.TileCableBus;
import appeng.util.Platform;
/**
* Implementing these might help improve visuals for hollow covers
*
* TSlottedPart,ISidedHollowConnect
*/
2014-10-04 08:08:28 +02:00
public class CableBusPart extends JCuboidPart implements JNormalOcclusion, IRedstonePart, AEMultiTile
2014-09-24 02:26:27 +02:00
{
final static Cuboid6[] sideTests = new Cuboid6[] {
2014-09-24 02:26:27 +02:00
new Cuboid6( 6.0 / 16.0, 0, 6.0 / 16.0, 10.0 / 16.0, 6.0 / 16.0, 10.0 / 16.0 ), // DOWN(0, -1, 0),
2014-09-24 02:26:27 +02:00
new Cuboid6( 6.0 / 16.0, 10.0 / 16.0, 6.0 / 16.0, 10.0 / 16.0, 1.0, 10.0 / 16.0 ), // UP(0, 1, 0),
new Cuboid6( 6.0 / 16.0, 6.0 / 16.0, 0.0, 10.0 / 16.0, 10.0 / 16.0, 6.0 / 16.0 ),// NORTH(0, 0, -1),
new Cuboid6( 6.0 / 16.0, 6.0 / 16.0, 10.0 / 16.0, 10.0 / 16.0, 10.0 / 16.0, 1.0 ),// SOUTH(0, 0, 1),
new Cuboid6( 0.0, 6.0 / 16.0, 6.0 / 16.0, 6.0 / 16.0, 10.0 / 16.0, 10.0 / 16.0 ),// WEST(-1, 0, 0),
new Cuboid6( 10.0 / 16.0, 6.0 / 16.0, 6.0 / 16.0, 1.0, 10.0 / 16.0, 10.0 / 16.0 ),// EAST(1, 0, 0),
};
public static final ThreadLocal<Boolean> disableFacadeOcclusion = new ThreadLocal<Boolean>();
2014-09-24 02:26:27 +02:00
public CableBusContainer cb = new CableBusContainer( this );
@Override
public boolean isInWorld()
{
2014-12-29 15:13:47 +01:00
return this.cb.isInWorld();
2014-09-24 02:26:27 +02:00
}
@Override
public boolean doesTick()
{
return false;
}
@Override
public AECableType getCableConnectionType(ForgeDirection dir)
{
2014-12-29 15:13:47 +01:00
return this.cb.getCableConnectionType( dir );
2014-09-24 02:26:27 +02:00
}
@Override
public boolean recolourBlock(ForgeDirection side, AEColor colour, EntityPlayer who)
{
2014-12-29 15:13:47 +01:00
return this.cb.recolourBlock( side, colour, who );
2014-09-24 02:26:27 +02:00
}
@Override
public AEColor getColor()
{
2014-12-29 15:13:47 +01:00
return this.cb.getColor();
2014-09-24 02:26:27 +02:00
}
@Override
public void save(NBTTagCompound tag)
{
2014-12-29 15:13:47 +01:00
this.cb.writeToNBT( tag );
2014-09-24 02:26:27 +02:00
}
@Override
public void load(NBTTagCompound tag)
{
2014-12-29 15:13:47 +01:00
this.cb.readFromNBT( tag );
2014-09-24 02:26:27 +02:00
}
@Override
public void writeDesc(MCDataOutput packet)
{
ByteBuf stream = Unpooled.buffer();
try
{
2014-12-29 15:13:47 +01:00
this.cb.writeToStream( stream );
2014-09-24 02:26:27 +02:00
packet.writeInt( stream.readableBytes() );
stream.capacity( stream.readableBytes() );
packet.writeByteArray( stream.array() );
}
catch (IOException e)
{
AELog.error( e );
}
}
@Override
public void readDesc(MCDataInput packet)
{
int len = packet.readInt();
byte[] data = packet.readByteArray( len );
2014-09-24 02:26:27 +02:00
try
{
if ( len > 0 )
{
ByteBuf byteBuffer = Unpooled.wrappedBuffer( data );
2014-12-29 15:13:47 +01:00
this.cb.readFromStream( byteBuffer );
2014-09-24 02:26:27 +02:00
}
}
catch (IOException e)
{
AELog.error( e );
}
}
@Override
public Cuboid6 getBounds()
{
AxisAlignedBB b = null;
2014-12-29 15:13:47 +01:00
for (AxisAlignedBB bx : this.cb.getSelectedBoundingBoxesFromPool( false, true, null, true ))
2014-09-24 02:26:27 +02:00
{
if ( b == null )
b = bx;
else
{
double minX = Math.min( b.minX, bx.minX );
double minY = Math.min( b.minY, bx.minY );
double minZ = Math.min( b.minZ, bx.minZ );
double maxX = Math.max( b.maxX, bx.maxX );
double maxY = Math.max( b.maxY, bx.maxY );
double maxZ = Math.max( b.maxZ, bx.maxZ );
b.setBounds( minX, minY, minZ, maxX, maxY, maxZ );
}
}
if ( b == null )
return new Cuboid6( 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 );
return new Cuboid6( b.minX, b.minY, b.minZ, b.maxX, b.maxY, b.maxZ );
}
@Override
public String getType()
{
return PartRegistry.CableBusPart.getName();
}
@Override
public void onPartChanged(TMultiPart part)
{
2014-12-29 15:13:47 +01:00
this.cb.updateConnections();
2014-09-24 02:26:27 +02:00
}
@Override
public ItemStack pickItem(MovingObjectPosition hit)
{
Vec3 v3 = hit.hitVec.addVector( -hit.blockX, -hit.blockY, -hit.blockZ );
2014-12-29 15:13:47 +01:00
SelectedPart sp = this.cb.selectPart( v3 );
2014-09-24 02:26:27 +02:00
if ( sp != null )
{
if ( sp.part != null )
return sp.part.getItemStack( PartItemStack.Break );
if ( sp.facade != null )
return sp.facade.getItemStack();
}
return null;
}
@Override
public Iterable<ItemStack> getDrops()
{
2014-12-29 15:13:47 +01:00
return this.cb.getDrops( new ArrayList() );
2014-09-24 02:26:27 +02:00
}
@Override
public void onEntityCollision(Entity entity)
{
2014-12-29 15:13:47 +01:00
this.cb.onEntityCollision( entity );
2014-09-24 02:26:27 +02:00
}
@Override
public void onWorldJoin()
{
2014-12-29 15:13:47 +01:00
this.canUpdate = true;
this.cb.updateConnections();
this.cb.addToWorld();
2014-09-24 02:26:27 +02:00
}
@Override
public void onWorldSeparate()
{
2014-12-29 15:13:47 +01:00
this.canUpdate = false;
this.cb.removeFromWorld();
2014-09-28 00:50:06 +02:00
}
2014-09-24 02:26:27 +02:00
@Override
public boolean canConnectRedstone(int side)
{
2014-12-29 15:13:47 +01:00
return this.cb.canConnectRedstone( EnumSet.of( ForgeDirection.getOrientation( side ) ) );
2014-09-24 02:26:27 +02:00
}
@Override
public int strongPowerLevel(int side)
{
2014-12-29 15:13:47 +01:00
return this.cb.isProvidingStrongPower( ForgeDirection.getOrientation( side ) );
2014-09-24 02:26:27 +02:00
}
@Override
public int weakPowerLevel(int side)
{
2014-12-29 15:13:47 +01:00
return this.cb.isProvidingWeakPower( ForgeDirection.getOrientation( side ) );
2014-09-24 02:26:27 +02:00
}
@Override
public void onNeighborChanged()
{
2014-12-29 15:13:47 +01:00
this.cb.onNeighborChanged();
2014-09-24 02:26:27 +02:00
}
@Override
public boolean activate(EntityPlayer player, MovingObjectPosition hit, ItemStack item)
{
2014-12-29 15:13:47 +01:00
return this.cb.activate( player, hit.hitVec.addVector( -hit.blockX, -hit.blockY, -hit.blockZ ) );
2014-09-24 02:26:27 +02:00
}
@Override
public void renderDynamic(Vector3 pos, float frame, int pass)
{
if ( pass == 0 || (pass == 1 && AEConfig.instance.isFeatureEnabled( AEFeature.AlphaPass )) )
{
BusRenderHelper.instance.setPass( pass );
2014-12-29 15:13:47 +01:00
this.cb.renderDynamic( pos.x, pos.y, pos.z );
2014-09-24 02:26:27 +02:00
}
}
@Override
public boolean renderStatic(Vector3 pos, int pass)
{
if ( pass == 0 || (pass == 1 && AEConfig.instance.isFeatureEnabled( AEFeature.AlphaPass )) )
{
BusRenderHelper.instance.setPass( pass );
BusRenderer.instance.renderer.renderAllFaces = true;
2014-12-29 15:13:47 +01:00
BusRenderer.instance.renderer.blockAccess = this.world();
2014-09-24 02:26:27 +02:00
BusRenderer.instance.renderer.overrideBlockTexture = null;
2014-12-29 15:13:47 +01:00
this.cb.renderStatic( pos.x, pos.y, pos.z );
2014-09-24 02:26:27 +02:00
return BusRenderHelper.instance.getItemsRendered() > 0;
}
return false;
}
@Override
public int getLightValue()
{
2014-12-29 15:13:47 +01:00
return this.cb.getLightValue();
2014-09-24 02:26:27 +02:00
}
@Override
public boolean canAddPart(ItemStack is, ForgeDirection side)
{
IFacadePart fp = PartPlacement.isFacade( is, side );
if ( fp != null )
{
2014-12-29 15:13:47 +01:00
if ( !(side == null || side == ForgeDirection.UNKNOWN || this.tile() == null) )
2014-09-24 02:26:27 +02:00
{
2014-09-28 22:20:14 +02:00
List<AxisAlignedBB> boxes = new ArrayList<AxisAlignedBB>();
2014-09-24 02:26:27 +02:00
IPartCollisionHelper bch = new BusCollisionHelper( boxes, side, null, true );
fp.getBoxes( bch, null );
for (AxisAlignedBB bb : boxes)
{
disableFacadeOcclusion.set( true );
2014-12-29 15:13:47 +01:00
boolean canAdd = this.tile().canAddPart( new NormallyOccludedPart( new Cuboid6( bb ) ) );
2014-09-24 02:26:27 +02:00
disableFacadeOcclusion.remove();
if ( !canAdd )
{
return false;
}
}
}
return true;
}
if ( is.getItem() instanceof IPartItem )
{
IPartItem bi = (IPartItem) is.getItem();
is = is.copy();
is.stackSize = 1;
IPart bp = bi.createPartFromItemStack( is );
2014-12-29 15:13:47 +01:00
if ( !(side == null || side == ForgeDirection.UNKNOWN || this.tile() == null) )
2014-09-24 02:26:27 +02:00
{
2014-09-28 22:20:14 +02:00
List<AxisAlignedBB> boxes = new ArrayList<AxisAlignedBB>();
2014-09-24 02:26:27 +02:00
IPartCollisionHelper bch = new BusCollisionHelper( boxes, side, null, true );
bp.getBoxes( bch );
for (AxisAlignedBB bb : boxes)
{
2014-12-29 15:13:47 +01:00
if ( !this.tile().canAddPart( new NormallyOccludedPart( new Cuboid6( bb ) ) ) )
2014-09-24 02:26:27 +02:00
{
return false;
}
}
}
}
2014-12-29 15:13:47 +01:00
return this.cb.canAddPart( is, side );
2014-09-24 02:26:27 +02:00
}
@Override
public ForgeDirection addPart(ItemStack is, ForgeDirection side, EntityPlayer owner)
{
2014-12-29 15:13:47 +01:00
return this.cb.addPart( is, side, owner );
2014-09-24 02:26:27 +02:00
}
@Override
public IPart getPart(ForgeDirection side)
{
2014-12-29 15:13:47 +01:00
return this.cb.getPart( side );
2014-09-24 02:26:27 +02:00
}
@Override
public void removePart(ForgeDirection side, boolean suppressUpdate)
{
2014-12-29 15:13:47 +01:00
this.cb.removePart( side, suppressUpdate );
2014-09-24 02:26:27 +02:00
}
boolean canUpdate = false;
@Override
public void markForUpdate()
{
2014-12-29 15:13:47 +01:00
if ( Platform.isServer() && this.canUpdate )
this.sendDescUpdate();
2014-09-24 02:26:27 +02:00
}
@Override
public DimensionalCoord getLocation()
{
2014-10-01 11:39:58 +02:00
return new DimensionalCoord( this.tile() );
2014-09-24 02:26:27 +02:00
}
@Override
public void invalidateConvertedTile()
{
2014-12-29 15:13:47 +01:00
this.cb.setHost( this );
2014-09-24 02:26:27 +02:00
}
public void convertFromTile(TileEntity blockTileEntity)
{
TileCableBus tcb = (TileCableBus) blockTileEntity;
2014-12-29 15:13:47 +01:00
this.cb = tcb.cb;
2014-09-24 02:26:27 +02:00
}
@Override
public boolean occlusionTest(TMultiPart part)
2014-09-24 02:26:27 +02:00
{
return NormalOcclusionTest.apply( this, part );
2014-09-24 02:26:27 +02:00
}
@Override
public Iterable<Cuboid6> getCollisionBoxes()
{
2014-09-28 22:20:14 +02:00
LinkedList<Cuboid6> l = new LinkedList<Cuboid6>();
2014-12-29 15:13:47 +01:00
for (AxisAlignedBB b : this.cb.getSelectedBoundingBoxesFromPool( false, true, null, false ))
2014-09-24 02:26:27 +02:00
{
l.add( new Cuboid6( b.minX, b.minY, b.minZ, b.maxX, b.maxY, b.maxZ ) );
}
return l;
}
@Override
public Iterable<IndexedCuboid6> getSubParts()
{
2014-09-28 22:20:14 +02:00
LinkedList<IndexedCuboid6> l = new LinkedList<IndexedCuboid6>();
2014-12-29 15:13:47 +01:00
for (Cuboid6 c : this.getCollisionBoxes())
2014-09-24 02:26:27 +02:00
{
l.add( new IndexedCuboid6( 0, c ) );
}
return l;
}
@Override
public Iterable<Cuboid6> getOcclusionBoxes()
{
2014-09-28 22:20:14 +02:00
LinkedList<Cuboid6> l = new LinkedList<Cuboid6>();
2014-12-29 15:13:47 +01:00
for (AxisAlignedBB b : this.cb.getSelectedBoundingBoxesFromPool( true, disableFacadeOcclusion.get() == null, null, true ))
2014-09-24 02:26:27 +02:00
{
l.add( new Cuboid6( b.minX, b.minY, b.minZ, b.maxX, b.maxY, b.maxZ ) );
}
return l;
}
@Override
public IGridNode getGridNode(ForgeDirection dir)
{
2014-12-29 15:13:47 +01:00
return this.cb.getGridNode( dir );
2014-09-24 02:26:27 +02:00
}
@Override
public IFacadeContainer getFacadeContainer()
{
2014-12-29 15:13:47 +01:00
return this.cb.getFacadeContainer();
2014-09-24 02:26:27 +02:00
}
@Override
public void clearContainer()
{
2014-12-29 15:13:47 +01:00
this.cb = new CableBusContainer( this );
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isBlocked(ForgeDirection side)
{
2014-12-29 15:13:47 +01:00
if ( side == null || side == ForgeDirection.UNKNOWN || this.tile() == null )
2014-09-24 02:26:27 +02:00
return false;
disableFacadeOcclusion.set( true );
2014-12-29 15:13:47 +01:00
boolean blocked = !this.tile().canAddPart( new NormallyOccludedPart( sideTests[side.ordinal()] ) );
2014-09-24 02:26:27 +02:00
disableFacadeOcclusion.remove();
return blocked;
}
@Override
public SelectedPart selectPart(Vec3 pos)
{
2014-12-29 15:13:47 +01:00
return this.cb.selectPart( pos );
2014-09-24 02:26:27 +02:00
}
@Override
public void partChanged()
{
2014-12-29 15:13:47 +01:00
if ( this.isInWorld() )
this.notifyNeighbors();
2014-09-24 02:26:27 +02:00
}
@Override
public Set<LayerFlags> getLayerFlags()
{
2014-12-29 15:13:47 +01:00
return this.cb.getLayerFlags();
2014-09-24 02:26:27 +02:00
}
@Override
public void markForSave()
{
// mark the chunk for save...
2014-10-01 11:39:58 +02:00
TileEntity te = this.tile();
2014-09-24 02:26:27 +02:00
if ( te != null && te.getWorldObj() != null )
2014-12-29 15:13:47 +01:00
te.getWorldObj().getChunkFromBlockCoords( this.x(), this.z() ).isModified = true;
2014-09-24 02:26:27 +02:00
}
@Override
public boolean hasRedstone(ForgeDirection side)
{
2014-12-29 15:13:47 +01:00
return this.cb.hasRedstone( side );
2014-09-24 02:26:27 +02:00
}
@Override
public void securityBreak()
{
2014-12-29 15:13:47 +01:00
this.cb.securityBreak();
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isEmpty()
{
2014-12-29 15:13:47 +01:00
return this.cb.isEmpty();
2014-09-24 02:26:27 +02:00
}
@Override
public void cleanup()
{
2014-12-29 15:13:47 +01:00
this.tile().remPart( this );
2014-09-24 02:26:27 +02:00
}
@Override
public void notifyNeighbors()
{
2014-12-29 15:13:47 +01:00
if ( this.tile() instanceof TIInventoryTile )
((TIInventoryTile) this.tile()).rebuildSlotMap();
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
if ( this.world() != null && this.world().blockExists( this.x(), this.y(), this.z() ) && !CableBusContainer.isLoading() )
Platform.notifyBlocksOfNeighbors(this.world(), this.x(), this.y(), this.z() );
2014-09-24 02:26:27 +02:00
}
// @Override
public int getHollowSize(int side)
{
2014-12-29 15:13:47 +01:00
IPartCable cable = (IPartCable) this.getPart( ForgeDirection.UNKNOWN );
2014-09-24 02:26:27 +02:00
ForgeDirection dir = ForgeDirection.getOrientation( side );
if ( cable != null && cable.isConnected( dir ) )
{
2014-09-28 22:20:14 +02:00
List<AxisAlignedBB> boxes = new ArrayList<AxisAlignedBB>();
2014-09-24 02:26:27 +02:00
BusCollisionHelper bch = new BusCollisionHelper( boxes, ForgeDirection.EAST, ForgeDirection.UP, ForgeDirection.SOUTH, null, true );
for (ForgeDirection whichSide : ForgeDirection.values())
{
2014-12-29 15:13:47 +01:00
IPart fPart = this.getPart( whichSide );
2014-09-24 02:26:27 +02:00
if ( fPart != null )
fPart.getBoxes( bch );
}
AxisAlignedBB b = null;
AxisAlignedBB pb = Platform.getPrimaryBox( dir, 2 );
for (AxisAlignedBB bb : boxes)
{
if ( bb.intersectsWith( pb ) )
{
if ( b == null )
b = bb;
else
{
b.maxX = Math.max( b.maxX, bb.maxX );
b.maxY = Math.max( b.maxY, bb.maxY );
b.maxZ = Math.max( b.maxZ, bb.maxZ );
b.minX = Math.min( b.minX, bb.minX );
b.minY = Math.min( b.minY, bb.minY );
b.minZ = Math.min( b.minZ, bb.minZ );
}
}
}
if ( b == null )
return 0;
switch (dir)
{
case WEST:
case EAST:
2014-12-29 15:13:47 +01:00
return this.getSize( b.minZ, b.maxZ, b.minY, b.maxY );
2014-09-24 02:26:27 +02:00
case DOWN:
case NORTH:
2014-12-29 15:13:47 +01:00
return this.getSize( b.minX, b.maxX, b.minZ, b.maxZ );
2014-09-24 02:26:27 +02:00
case SOUTH:
case UP:
2014-12-29 15:13:47 +01:00
return this.getSize( b.minX, b.maxX, b.minY, b.maxY );
2014-09-24 02:26:27 +02:00
default:
}
}
return 12;
}
int getSize(double a, double b, double c, double d)
{
double r = Math.abs( a - 0.5 );
r = Math.max( Math.abs( b - 0.5 ), r );
r = Math.max( Math.abs( c - 0.5 ), r );
return (8 * (int) Math.max( Math.abs( d - 0.5 ), r ));
}
// @Override
public int getSlotMask()
{
int mask = 0;
for (ForgeDirection side : ForgeDirection.values())
{
2014-12-29 15:13:47 +01:00
if ( this.getPart( side ) != null )
2014-09-24 02:26:27 +02:00
mask |= 1 << side.ordinal();
2014-12-29 15:13:47 +01:00
else if ( side != ForgeDirection.UNKNOWN && this.getFacadeContainer().getFacade( side ) != null )
2014-09-24 02:26:27 +02:00
mask |= 1 << side.ordinal();
}
return mask;
}
}