From 2a89bdf52ac734f303476fee2d0d603909ddfe8e Mon Sep 17 00:00:00 2001 From: thatsIch Date: Sun, 29 Mar 2015 09:33:06 +0200 Subject: [PATCH] Fixes #1118 Does not crash with invalid ItemStacks anymore --- src/main/java/appeng/block/AEBaseBlock.java | 276 ++--- .../java/appeng/client/gui/AEBaseGui.java | 1002 ++++++++--------- .../client/render/AppEngRenderItem.java | 19 +- .../appeng/client/render/BaseBlockRender.java | 3 + .../render/blocks/RenderBlockInscriber.java | 1 + .../render/blocks/RenderBlockInterface.java | 3 +- .../client/render/blocks/RenderMEChest.java | 2 + .../render/blocks/RenderSpatialPylon.java | 69 +- .../java/appeng/tile/storage/TileIOPort.java | 4 +- .../java/appeng/util/item/AEItemStack.java | 766 ++++++------- 10 files changed, 1077 insertions(+), 1068 deletions(-) diff --git a/src/main/java/appeng/block/AEBaseBlock.java b/src/main/java/appeng/block/AEBaseBlock.java index dfa6f35c1..40def1d3c 100644 --- a/src/main/java/appeng/block/AEBaseBlock.java +++ b/src/main/java/appeng/block/AEBaseBlock.java @@ -22,9 +22,7 @@ package appeng.block; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; - -import com.google.common.base.Optional; -import com.google.common.collect.Lists; +import javax.annotation.Nullable; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; @@ -56,6 +54,9 @@ import cpw.mods.fml.relauncher.ReflectionHelper; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import com.google.common.base.Optional; +import com.google.common.collect.Lists; + import appeng.api.implementations.items.IMemoryCard; import appeng.api.implementations.items.MemoryCardMessages; import appeng.api.implementations.tiles.IColorableTile; @@ -97,11 +98,13 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature @SideOnly( Side.CLIENT ) BlockRenderInfo renderInfo; private IFeatureHandler handler; + + @Nullable private Class tileEntityType = null; protected AEBaseBlock( Class c, Material mat ) { - this( c, mat, Optional. absent() ); + this( c, mat, Optional.absent() ); this.setLightOpacity( 255 ); this.setLightLevel( 0 ); this.setHardness( 2.2F ); @@ -113,11 +116,11 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature { super( mat ); - if ( mat == AEGlassMaterial.INSTANCE || mat == Material.glass ) + if( mat == AEGlassMaterial.INSTANCE || mat == Material.glass ) this.setStepSound( Block.soundTypeGlass ); - else if ( mat == Material.rock ) + else if( mat == Material.rock ) this.setStepSound( Block.soundTypeStone ); - else if ( mat == Material.wood ) + else if( mat == Material.wood ) this.setStepSound( Block.soundTypeWood ); else this.setStepSound( Block.soundTypeMetal ); @@ -126,6 +129,12 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature this.featureSubName = subName; } + // update Block value. + private void setTileProvider( boolean b ) + { + ReflectionHelper.setPrivateValue( Block.class, this, b, "isTileProvider" ); + } + @Override public String toString() { @@ -139,6 +148,28 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature info.updateIcons( i, i, i, i, i, i ); } + @SideOnly( Side.CLIENT ) + public BlockRenderInfo getRendererInstance() + { + if( this.renderInfo != null ) + return this.renderInfo; + + try + { + return this.renderInfo = new BlockRenderInfo( this.getRenderer().newInstance() ); + } + catch( Throwable t ) + { + throw new RuntimeException( t ); + } + } + + @SideOnly( Side.CLIENT ) + protected Class getRenderer() + { + return BaseBlockRender.class; + } + public IIcon unmappedGetIcon( IBlockAccess w, int x, int y, int z, int s ) { return super.getIcon( w, x, y, z, s ); @@ -146,16 +177,17 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature protected void setTileEntity( Class c ) { + this.tileEntityType = c; + AEBaseTile.registerTileItem( c, new ItemStackSrc( this, 0 ) ); - GameRegistry.registerTileEntity( this.tileEntityType = c, this.featureFullName ); + GameRegistry.registerTileEntity( this.tileEntityType, this.featureFullName ); this.isInventory = IInventory.class.isAssignableFrom( c ); this.setTileProvider( this.hasBlockTileEntity() ); } - // update Block value. - private void setTileProvider( boolean b ) + public boolean hasBlockTileEntity() { - ReflectionHelper.setPrivateValue( Block.class, this, b, "isTileProvider" ); + return this.tileEntityType != null; } protected void setFeature( EnumSet f ) @@ -203,7 +235,7 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature @SideOnly( Side.CLIENT ) public IIcon getIcon( int direction, int metadata ) { - if ( this.renderIcon != null ) + if( this.renderIcon != null ) return this.renderIcon; return this.getRendererInstance().getTexture( ForgeDirection.getOrientation( direction ) ); @@ -216,20 +248,20 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature { ICustomCollision collisionHandler = null; - if ( this instanceof ICustomCollision ) - collisionHandler = ( ICustomCollision ) this; + if( this instanceof ICustomCollision ) + collisionHandler = (ICustomCollision) this; else { AEBaseTile te = this.getTileEntity( w, x, y, z ); - if ( te instanceof ICustomCollision ) - collisionHandler = ( ICustomCollision ) te; + if( te instanceof ICustomCollision ) + collisionHandler = (ICustomCollision) te; } - if ( collisionHandler != null && bb != null ) + if( collisionHandler != null && bb != null ) { List tmp = new ArrayList(); collisionHandler.addCollidingBlockToList( w, x, y, z, bb, tmp, e ); - for ( AxisAlignedBB b : tmp ) + for( AxisAlignedBB b : tmp ) { b.minX += x; b.minY += y; @@ -237,7 +269,7 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature b.maxX += x; b.maxY += y; b.maxZ += z; - if ( bb.intersectsWith( b ) ) + if( bb.intersectsWith( b ) ) out.add( b ); } } @@ -252,18 +284,18 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature ICustomCollision collisionHandler = null; AxisAlignedBB b = null; - if ( this instanceof ICustomCollision ) - collisionHandler = ( ICustomCollision ) this; + if( this instanceof ICustomCollision ) + collisionHandler = (ICustomCollision) this; else { AEBaseTile te = this.getTileEntity( w, x, y, z ); - if ( te instanceof ICustomCollision ) - collisionHandler = ( ICustomCollision ) te; + if( te instanceof ICustomCollision ) + collisionHandler = (ICustomCollision) te; } - if ( collisionHandler != null ) + if( collisionHandler != null ) { - if ( Platform.isClient() ) + if( Platform.isClient() ) { EntityPlayer player = Minecraft.getMinecraft().thePlayer; LookDirection ld = Platform.getPlayerRay( player, Platform.getEyeOffset( player ) ); @@ -273,22 +305,22 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature double lastDist = 0; - for ( AxisAlignedBB bb : bbs ) + for( AxisAlignedBB bb : bbs ) { - this.setBlockBounds( ( float ) bb.minX, ( float ) bb.minY, ( float ) bb.minZ, ( float ) bb.maxX, ( float ) bb.maxY, ( float ) bb.maxZ ); + this.setBlockBounds( (float) bb.minX, (float) bb.minY, (float) bb.minZ, (float) bb.maxX, (float) bb.maxY, (float) bb.maxZ ); MovingObjectPosition r = super.collisionRayTrace( w, x, y, z, ld.a, ld.b ); this.setBlockBounds( 0, 0, 0, 1, 1, 1 ); - if ( r != null ) + if( r != null ) { double xLen = ( ld.a.xCoord - r.hitVec.xCoord ); double yLen = ( ld.a.yCoord - r.hitVec.yCoord ); double zLen = ( ld.a.zCoord - r.hitVec.zCoord ); double thisDist = xLen * xLen + yLen * yLen + zLen * zLen; - if ( br == null || lastDist > thisDist ) + if( br == null || lastDist > thisDist ) { lastDist = thisDist; br = bb; @@ -296,16 +328,16 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature } } - if ( br != null ) + if( br != null ) { br.setBounds( br.minX + x, br.minY + y, br.minZ + z, br.maxX + x, br.maxY + y, br.maxZ + z ); return br; } } - for ( AxisAlignedBB bx : collisionHandler.getSelectedBoundingBoxesFromPool( w, x, y, z, null, false ) ) + for( AxisAlignedBB bx : collisionHandler.getSelectedBoundingBoxesFromPool( w, x, y, z, null, false ) ) { - if ( b == null ) + if( b == null ) b = bx; else { @@ -338,38 +370,38 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature { ICustomCollision collisionHandler = null; - if ( this instanceof ICustomCollision ) - collisionHandler = ( ICustomCollision ) this; + if( this instanceof ICustomCollision ) + collisionHandler = (ICustomCollision) this; else { AEBaseTile te = this.getTileEntity( w, x, y, z ); - if ( te instanceof ICustomCollision ) - collisionHandler = ( ICustomCollision ) te; + if( te instanceof ICustomCollision ) + collisionHandler = (ICustomCollision) te; } - if ( collisionHandler != null ) + if( collisionHandler != null ) { Iterable bbs = collisionHandler.getSelectedBoundingBoxesFromPool( w, x, y, z, null, true ); MovingObjectPosition br = null; double lastDist = 0; - for ( AxisAlignedBB bb : bbs ) + for( AxisAlignedBB bb : bbs ) { - this.setBlockBounds( ( float ) bb.minX, ( float ) bb.minY, ( float ) bb.minZ, ( float ) bb.maxX, ( float ) bb.maxY, ( float ) bb.maxZ ); + this.setBlockBounds( (float) bb.minX, (float) bb.minY, (float) bb.minZ, (float) bb.maxX, (float) bb.maxY, (float) bb.maxZ ); MovingObjectPosition r = super.collisionRayTrace( w, x, y, z, a, b ); this.setBlockBounds( 0, 0, 0, 1, 1, 1 ); - if ( r != null ) + if( r != null ) { double xLen = ( a.xCoord - r.hitVec.xCoord ); double yLen = ( a.yCoord - r.hitVec.yCoord ); double zLen = ( a.zCoord - r.hitVec.zCoord ); double thisDist = xLen * xLen + yLen * yLen + zLen * zLen; - if ( br == null || lastDist > thisDist ) + if( br == null || lastDist > thisDist ) { lastDist = thisDist; br = r; @@ -377,7 +409,7 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature } } - if ( br != null ) + if( br != null ) { return br; } @@ -391,37 +423,37 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature @Override final public boolean onBlockActivated( World w, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ ) { - if ( player != null ) + if( player != null ) { ItemStack is = player.inventory.getCurrentItem(); - if ( is != null ) + if( is != null ) { - if ( Platform.isWrench( player, is, x, y, z ) && player.isSneaking() ) + if( Platform.isWrench( player, is, x, y, z ) && player.isSneaking() ) { Block id = w.getBlock( x, y, z ); - if ( id != null ) + if( id != null ) { AEBaseTile tile = this.getTileEntity( w, x, y, z ); ItemStack[] drops = Platform.getBlockDrops( w, x, y, z ); - if ( tile == null ) + if( tile == null ) return false; - if ( tile instanceof TileCableBus || tile instanceof TileSkyChest ) + if( tile instanceof TileCableBus || tile instanceof TileSkyChest ) return false; ItemStack op = new ItemStack( this ); - for ( ItemStack ol : drops ) + for( ItemStack ol : drops ) { - if ( Platform.isSameItemType( ol, op ) ) + if( Platform.isSameItemType( ol, op ) ) { NBTTagCompound tag = tile.downloadSettings( SettingsFrom.DISMANTLE_ITEM ); - if ( tag != null ) + if( tag != null ) ol.setTagCompound( tag ); } } - if ( id.removedByPlayer( w, player, x, y, z, false ) ) + if( id.removedByPlayer( w, player, x, y, z, false ) ) { List l = Lists.newArrayList( drops ); Platform.spawnDrops( w, x, y, z, l ); @@ -431,17 +463,17 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature return false; } - if ( is.getItem() instanceof IMemoryCard && !( this instanceof BlockCableBus ) ) + if( is.getItem() instanceof IMemoryCard && !( this instanceof BlockCableBus ) ) { - IMemoryCard memoryCard = ( IMemoryCard ) is.getItem(); - if ( player.isSneaking() ) + IMemoryCard memoryCard = (IMemoryCard) is.getItem(); + if( player.isSneaking() ) { AEBaseTile t = this.getTileEntity( w, x, y, z ); - if ( t != null ) + if( t != null ) { String name = this.getUnlocalizedName(); NBTTagCompound data = t.downloadSettings( SettingsFrom.MEMORY_CARD ); - if ( data != null ) + if( data != null ) { memoryCard.setMemoryCardContents( is, name, data ); memoryCard.notifyUser( player, MemoryCardMessages.SETTINGS_SAVED ); @@ -453,7 +485,7 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature { String name = memoryCard.getSettingsName( is ); NBTTagCompound data = memoryCard.getData( is ); - if ( this.getUnlocalizedName().equals( name ) ) + if( this.getUnlocalizedName().equals( name ) ) { AEBaseTile t = this.getTileEntity( w, x, y, z ); t.uploadSettings( SettingsFrom.MEMORY_CARD, data ); @@ -478,11 +510,11 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature @Override public void onBlockPlacedBy( World w, int x, int y, int z, EntityLivingBase player, ItemStack is ) { - if ( is.hasDisplayName() ) + if( is.hasDisplayName() ) { TileEntity te = this.getTileEntity( w, x, y, z ); - if ( te instanceof AEBaseTile ) - ( ( AEBaseTile ) w.getTileEntity( x, y, z ) ).setName( is.getDisplayName() ); + if( te instanceof AEBaseTile ) + ( (AEBaseTile) w.getTileEntity( x, y, z ) ).setName( is.getDisplayName() ); } } @@ -504,8 +536,8 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature public int getComparatorInputOverride( World w, int x, int y, int z, int s ) { TileEntity te = this.getTileEntity( w, x, y, z ); - if ( te instanceof IInventory ) - return Container.calcRedstoneFromInventory( ( IInventory ) te ); + if( te instanceof IInventory ) + return Container.calcRedstoneFromInventory( (IInventory) te ); return 0; } @@ -544,18 +576,18 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature { IOrientable rotatable = null; - if ( this.hasBlockTileEntity() ) + if( this.hasBlockTileEntity() ) { - rotatable = ( AEBaseTile ) this.getTileEntity( w, x, y, z ); + rotatable = (AEBaseTile) this.getTileEntity( w, x, y, z ); } - else if ( this instanceof IOrientableBlock ) + else if( this instanceof IOrientableBlock ) { - rotatable = ( ( IOrientableBlock ) this ).getOrientable( w, x, y, z ); + rotatable = ( (IOrientableBlock) this ).getOrientable( w, x, y, z ); } - if ( rotatable != null && rotatable.canBeRotated() ) + if( rotatable != null && rotatable.canBeRotated() ) { - if ( this.hasCustomRotation() ) + if( this.hasCustomRotation() ) { this.customRotateBlock( rotatable, axis ); return true; @@ -565,12 +597,12 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature ForgeDirection forward = rotatable.getForward(); ForgeDirection up = rotatable.getUp(); - for ( int rs = 0; rs < 4; rs++ ) + for( int rs = 0; rs < 4; rs++ ) { forward = Platform.rotateAround( forward, axis ); up = Platform.rotateAround( up, axis ); - if ( this.isValidOrientation( w, x, y, z, forward, up ) ) + if( this.isValidOrientation( w, x, y, z, forward, up ) ) { rotatable.setOrientation( forward, up ); return true; @@ -600,10 +632,10 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature @Override final public ForgeDirection[] getValidRotations( World w, int x, int y, int z ) { - if ( this.hasBlockTileEntity() ) + if( this.hasBlockTileEntity() ) { AEBaseTile obj = this.getTileEntity( w, x, y, z ); - if ( obj != null && obj.canBeRotated() ) + if( obj != null && obj.canBeRotated() ) { return ForgeDirection.VALID_DIRECTIONS; } @@ -617,13 +649,13 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature { TileEntity te = this.getTileEntity( world, x, y, z ); - if ( te instanceof IColorableTile ) + if( te instanceof IColorableTile ) { - IColorableTile ct = ( IColorableTile ) te; + IColorableTile ct = (IColorableTile) te; AEColor c = ct.getColor(); AEColor newColor = AEColor.values()[colour]; - if ( c != newColor ) + if( c != newColor ) { ct.recolourBlock( side, newColor, null ); return true; @@ -634,42 +666,25 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature return super.recolourBlock( world, x, y, z, side, colour ); } - @SideOnly( Side.CLIENT ) - public BlockRenderInfo getRendererInstance() - { - if ( this.renderInfo != null ) - return this.renderInfo; - - try - { - return this.renderInfo = new BlockRenderInfo( this.getRenderer().newInstance() ); - } - catch ( Throwable t ) - { - throw new RuntimeException( t ); - } - } - @SideOnly( Side.CLIENT ) private FlippableIcon optionalIcon( IIconRegister ir, String Name, IIcon substitute ) { // if the input is an flippable IIcon find the original. - while ( substitute instanceof FlippableIcon ) - substitute = ( ( FlippableIcon ) substitute ).getOriginal(); + while( substitute instanceof FlippableIcon ) + substitute = ( (FlippableIcon) substitute ).getOriginal(); - if ( substitute != null ) + if( substitute != null ) { try { ResourceLocation resLoc = new ResourceLocation( Name ); - resLoc = new ResourceLocation( resLoc.getResourceDomain(), String.format( "%s/%s%s", "textures/blocks", - resLoc.getResourcePath(), ".png" ) ); + resLoc = new ResourceLocation( resLoc.getResourceDomain(), String.format( "%s/%s%s", "textures/blocks", resLoc.getResourcePath(), ".png" ) ); IResource res = Minecraft.getMinecraft().getResourceManager().getResource( resLoc ); - if ( res != null ) + if( res != null ) return new FlippableIcon( ir.registerIcon( Name ) ); } - catch ( Throwable e ) + catch( Throwable e ) { return new FlippableIcon( substitute ); } @@ -678,12 +693,6 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature return new FlippableIcon( ir.registerIcon( Name ) ); } - @SideOnly( Side.CLIENT ) - protected Class getRenderer() - { - return BaseBlockRender.class; - } - @SideOnly( Side.CLIENT ) public void getCheckedSubBlocks( Item item, CreativeTabs tabs, List itemStacks ) { @@ -694,16 +703,16 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature { IOrientable ori = null; - if ( this.hasBlockTileEntity() ) + if( this.hasBlockTileEntity() ) { - ori = ( AEBaseTile ) this.getTileEntity( w, x, y, z ); + ori = (AEBaseTile) this.getTileEntity( w, x, y, z ); } - else if ( this instanceof IOrientableBlock ) + else if( this instanceof IOrientableBlock ) { - ori = ( ( IOrientableBlock ) this ).getOrientable( w, x, y, z ); + ori = ( (IOrientableBlock) this ).getOrientable( w, x, y, z ); } - if ( ori != null && ori.canBeRotated() ) + if( ori != null && ori.canBeRotated() ) { return this.mapRotation( ori, ForgeDirection.getOrientation( s ) ).ordinal(); } @@ -711,19 +720,15 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature return s; } - public boolean hasBlockTileEntity() - { - return this.tileEntityType != null; - } - + @Nullable public T getTileEntity( IBlockAccess w, int x, int y, int z ) { - if ( !this.hasBlockTileEntity() ) + if( !this.hasBlockTileEntity() ) return null; TileEntity te = w.getTileEntity( x, y, z ); - if ( this.tileEntityType.isInstance( te ) ) - return ( T ) te; + if( this.tileEntityType.isInstance( te ) ) + return (T) te; return null; } @@ -741,30 +746,30 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature ForgeDirection up = ori.getUp(); ForgeDirection west = ForgeDirection.UNKNOWN; - if ( forward == null || up == null ) + if( forward == null || up == null ) return dir; int west_x = forward.offsetY * up.offsetZ - forward.offsetZ * up.offsetY; int west_y = forward.offsetZ * up.offsetX - forward.offsetX * up.offsetZ; int west_z = forward.offsetX * up.offsetY - forward.offsetY * up.offsetX; - for ( ForgeDirection dx : ForgeDirection.VALID_DIRECTIONS ) - if ( dx.offsetX == west_x && dx.offsetY == west_y && dx.offsetZ == west_z ) + for( ForgeDirection dx : ForgeDirection.VALID_DIRECTIONS ) + if( dx.offsetX == west_x && dx.offsetY == west_y && dx.offsetZ == west_z ) west = dx; - if ( dir == forward ) + if( dir == forward ) return ForgeDirection.SOUTH; - if ( dir == forward.getOpposite() ) + if( dir == forward.getOpposite() ) return ForgeDirection.NORTH; - if ( dir == up ) + if( dir == up ) return ForgeDirection.UP; - if ( dir == up.getOpposite() ) + if( dir == up.getOpposite() ) return ForgeDirection.DOWN; - if ( dir == west ) + if( dir == west ) return ForgeDirection.WEST; - if ( dir == west.getOpposite() ) + if( dir == west.getOpposite() ) return ForgeDirection.EAST; return ForgeDirection.UNKNOWN; @@ -784,17 +789,24 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature @Override final public TileEntity createNewTileEntity( World var1, int var2 ) { - if ( this.hasBlockTileEntity() ) + if( this.hasBlockTileEntity() ) { try { return this.tileEntityType.newInstance(); } - catch ( Throwable e ) + catch( InstantiationException e ) { + e.printStackTrace(); + throw new RuntimeException( e ); + } + catch( IllegalAccessException e ) + { + e.printStackTrace(); throw new RuntimeException( e ); } } + return null; } @@ -802,10 +814,10 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature public void breakBlock( World w, int x, int y, int z, Block a, int b ) { AEBaseTile te = this.getTileEntity( w, x, y, z ); - if ( te != null ) + if( te != null ) { ArrayList drops = new ArrayList(); - if ( te.dropItems() ) + if( te.dropItems() ) te.getDrops( w, x, y, z, drops ); else te.getNoDrops( w, x, y, z, drops ); @@ -815,7 +827,7 @@ public class AEBaseBlock extends BlockContainer implements IAEFeature } super.breakBlock( w, x, y, z, a, b ); - if ( te != null ) + if( te != null ) w.setTileEntity( x, y, z, null ); } diff --git a/src/main/java/appeng/client/gui/AEBaseGui.java b/src/main/java/appeng/client/gui/AEBaseGui.java index 417018998..4c57ebdb6 100644 --- a/src/main/java/appeng/client/gui/AEBaseGui.java +++ b/src/main/java/appeng/client/gui/AEBaseGui.java @@ -30,9 +30,6 @@ import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; -import com.google.common.base.Joiner; -import com.google.common.base.Stopwatch; - import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; @@ -52,6 +49,9 @@ import net.minecraft.util.ResourceLocation; import cpw.mods.fml.common.ObfuscationReflectionHelper; +import com.google.common.base.Joiner; +import com.google.common.base.Stopwatch; + import appeng.api.storage.data.IAEItemStack; import appeng.client.gui.widgets.GuiScrollbar; import appeng.client.gui.widgets.ITooltip; @@ -81,29 +81,44 @@ import appeng.integration.IntegrationType; import appeng.integration.abstraction.INEI; import appeng.util.Platform; + public abstract class AEBaseGui extends GuiContainer { - + public static boolean switchingGuis; protected final List meSlots = new LinkedList(); + // drag y + final Set drag_click = new HashSet(); + final AppEngRenderItem aeRenderItem = new AppEngRenderItem(); protected GuiScrollbar myScrollBar = null; - static public boolean switchingGuis; + boolean disableShiftClick = false; + Stopwatch dbl_clickTimer = Stopwatch.createStarted(); + ItemStack dbl_whichItem; + Slot bl_clicked; + boolean useNEI = false; private boolean subGui; - public AEBaseGui(Container container) + public AEBaseGui( Container container ) { super( container ); this.subGui = switchingGuis; switchingGuis = false; } - protected int getQty(GuiButton btn) + protected static String join( Collection toolTip, String delimiter ) + { + final Joiner joiner = Joiner.on( delimiter ); + + return joiner.join( toolTip ); + } + + protected int getQty( GuiButton btn ) { try { DecimalFormat df = new DecimalFormat( "+#;-#" ); return df.parse( btn.displayString ).intValue(); } - catch (ParseException e) + catch( ParseException e ) { return 0; } @@ -121,11 +136,11 @@ public abstract class AEBaseGui extends GuiContainer final List slots = this.getInventorySlots(); Iterator i = slots.iterator(); - while (i.hasNext()) - if ( i.next() instanceof SlotME ) + while( i.hasNext() ) + if( i.next() instanceof SlotME ) i.remove(); - for (InternalSlotME me : this.meSlots) + for( InternalSlotME me : this.meSlots ) slots.add( new SlotME( me ) ); } @@ -136,378 +151,31 @@ public abstract class AEBaseGui extends GuiContainer } @Override - public void handleMouseInput() - { - super.handleMouseInput(); - - int i = Mouse.getEventDWheel(); - if ( i != 0 && isShiftKeyDown() ) - { - int x = Mouse.getEventX() * this.width / this.mc.displayWidth; - int y = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1; - this.mouseWheelEvent( x, y, i / Math.abs( i ) ); - } - else if ( i != 0 && this.myScrollBar != null ) - this.myScrollBar.wheel( i ); - } - - protected void mouseWheelEvent(int x, int y, int wheel) - { - Slot slot = this.getSlot( x, y ); - if ( slot instanceof SlotME ) - { - IAEItemStack item = ((SlotME) slot).getAEStack(); - if ( item != null ) - { - ((AEBaseContainer) this.inventorySlots).setTargetStack( item ); - InventoryAction direction = wheel > 0 ? InventoryAction.ROLL_DOWN : InventoryAction.ROLL_UP; - int times = Math.abs( wheel ); - final int inventorySize = this.getInventorySlots().size(); - for (int h = 0; h < times; h++) - { - PacketInventoryAction p = new PacketInventoryAction( direction, inventorySize, 0 ); - NetworkHandler.instance.sendToServer( p ); - } - } - } - } - - @Override - public void onGuiClosed() - { - super.onGuiClosed(); - this.subGui = true; // in case the gui is reopened later ( i'm looking at you NEI ) - } - - @Override - protected void mouseClicked(int xCoord, int yCoord, int btn) - { - this.drag_click.clear(); - - if ( btn == 1 ) - { - for (Object o : this.buttonList) - { - GuiButton guibutton = (GuiButton) o; - if ( guibutton.mousePressed( this.mc, xCoord, yCoord ) ) - { - super.mouseClicked( xCoord, yCoord, 0 ); - return; - } - } - } - - super.mouseClicked( xCoord, yCoord, btn ); - } - - boolean disableShiftClick = false; - Stopwatch dbl_clickTimer = Stopwatch.createStarted(); - ItemStack dbl_whichItem; - Slot bl_clicked; - - // drag y - final Set drag_click = new HashSet(); - - @Override - protected void handleMouseClick(Slot slot, int slotIdx, int ctrlDown, int key) - { - EntityPlayer player = Minecraft.getMinecraft().thePlayer; - - if ( slot instanceof SlotFake ) - { - final InventoryAction action = ctrlDown == 1 ? InventoryAction.SPLIT_OR_PLACE_SINGLE : InventoryAction.PICKUP_OR_SET_DOWN; - - if ( this.drag_click.size() > 1 ) - return; - - PacketInventoryAction p = new PacketInventoryAction( action, slotIdx, 0 ); - NetworkHandler.instance.sendToServer( p ); - - return; - } - - if ( slot instanceof SlotPatternTerm ) - { - if ( key == 6 ) - return; // prevent weird double clicks.. - - try - { - NetworkHandler.instance.sendToServer( ((SlotPatternTerm) slot).getRequest( key == 1 ) ); - } - catch (IOException e) - { - AELog.error( e ); - } - } - else if ( slot instanceof SlotCraftingTerm ) - { - if ( key == 6 ) - return; // prevent weird double clicks.. - - InventoryAction action = null; - if ( key == 1 ) - action = InventoryAction.CRAFT_SHIFT; - else - action = ctrlDown == 1 ? InventoryAction.CRAFT_STACK : InventoryAction.CRAFT_ITEM; - - PacketInventoryAction p = new PacketInventoryAction( action, slotIdx, 0 ); - NetworkHandler.instance.sendToServer( p ); - - return; - } - - if ( Keyboard.isKeyDown( Keyboard.KEY_SPACE ) ) - { - if ( this.enableSpaceClicking() ) - { - IAEItemStack stack = null; - if ( slot instanceof SlotME ) - stack = ((SlotME) slot).getAEStack(); - - int slotNum = this.getInventorySlots().size(); - - if ( !(slot instanceof SlotME) && slot != null ) - slotNum = slot.slotNumber; - - ((AEBaseContainer) this.inventorySlots).setTargetStack( stack ); - PacketInventoryAction p = new PacketInventoryAction( InventoryAction.MOVE_REGION, slotNum, 0 ); - NetworkHandler.instance.sendToServer( p ); - return; - } - } - - if ( slot instanceof SlotDisconnected ) - { - InventoryAction action = null; - - switch (key) - { - case 0: // pickup / set-down. - action = ctrlDown == 1 ? InventoryAction.SPLIT_OR_PLACE_SINGLE : InventoryAction.PICKUP_OR_SET_DOWN; - break; - case 1: - action = ctrlDown == 1 ? InventoryAction.PICKUP_SINGLE : InventoryAction.SHIFT_CLICK; - break; - - case 3: // creative dupe: - - if ( player.capabilities.isCreativeMode ) - { - action = InventoryAction.CREATIVE_DUPLICATE; - } - - break; - - default: - case 4: // drop item: - case 6: - } - - if ( action != null ) - { - PacketInventoryAction p = new PacketInventoryAction( action, slot.getSlotIndex(), ((SlotDisconnected) slot).mySlot.id ); - NetworkHandler.instance.sendToServer( p ); - } - - return; - } - - if ( slot instanceof SlotME ) - { - InventoryAction action = null; - IAEItemStack stack = null; - - switch (key) - { - case 0: // pickup / set-down. - action = ctrlDown == 1 ? InventoryAction.SPLIT_OR_PLACE_SINGLE : InventoryAction.PICKUP_OR_SET_DOWN; - stack = ((SlotME) slot).getAEStack(); - - if ( stack != null && action == InventoryAction.PICKUP_OR_SET_DOWN && stack.getStackSize() == 0 && player.inventory.getItemStack() == null ) - action = InventoryAction.AUTO_CRAFT; - - break; - case 1: - action = ctrlDown == 1 ? InventoryAction.PICKUP_SINGLE : InventoryAction.SHIFT_CLICK; - stack = ((SlotME) slot).getAEStack(); - break; - - case 3: // creative dupe: - - stack = ((SlotME) slot).getAEStack(); - if ( stack != null && stack.isCraftable() ) - action = InventoryAction.AUTO_CRAFT; - - else if ( player.capabilities.isCreativeMode ) - { - IAEItemStack slotItem = ((SlotME) slot).getAEStack(); - if ( slotItem != null ) - { - action = InventoryAction.CREATIVE_DUPLICATE; - } - } - break; - - default: - case 4: // drop item: - case 6: - } - - if ( action != null ) - { - ((AEBaseContainer) this.inventorySlots).setTargetStack( stack ); - PacketInventoryAction p = new PacketInventoryAction( action, this.getInventorySlots().size(), 0 ); - NetworkHandler.instance.sendToServer( p ); - } - - return; - } - - if ( !this.disableShiftClick && isShiftKeyDown() ) - { - this.disableShiftClick = true; - - if ( this.dbl_whichItem == null || this.bl_clicked != slot || this.dbl_clickTimer.elapsed( TimeUnit.MILLISECONDS ) > 150 ) - { - // some simple double click logic. - this.bl_clicked = slot; - this.dbl_clickTimer = Stopwatch.createStarted(); - if ( slot != null ) - this.dbl_whichItem = slot.getHasStack() ? slot.getStack().copy() : null; - else - this.dbl_whichItem = null; - } - else if ( this.dbl_whichItem != null ) - { - // a replica of the weird broken vanilla feature. - - final List slots = this.getInventorySlots(); - for (Slot inventorySlot : slots) - { - if ( inventorySlot != null && inventorySlot.canTakeStack( this.mc.thePlayer ) && inventorySlot.getHasStack() - && inventorySlot.inventory == slot.inventory && Container.func_94527_a( inventorySlot, this.dbl_whichItem, true ) ) - { - this.handleMouseClick( inventorySlot, inventorySlot.slotNumber, ctrlDown, 1 ); - } - } - } - - this.disableShiftClick = false; - } - - super.handleMouseClick( slot, slotIdx, ctrlDown, key ); - } - - @Override - protected void mouseClickMove(int x, int y, int c, long d) - { - Slot slot = this.getSlot( x, y ); - ItemStack itemstack = this.mc.thePlayer.inventory.getItemStack(); - - if ( slot instanceof SlotFake && itemstack != null ) - { - this.drag_click.add( slot ); - if ( this.drag_click.size() > 1 ) - { - for (Slot dr : this.drag_click) - { - PacketInventoryAction p = new PacketInventoryAction( c == 0 ? InventoryAction.PICKUP_OR_SET_DOWN : InventoryAction.PLACE_SINGLE, - dr.slotNumber, 0 ); - NetworkHandler.instance.sendToServer( p ); - } - } - } - else - super.mouseClickMove( x, y, c, d ); - } - - protected boolean enableSpaceClicking() - { - return true; - } - - @Override - protected boolean checkHotbarKeys(int p_146983_1_) - { - Slot theSlot; - - try - { - theSlot = ObfuscationReflectionHelper.getPrivateValue( GuiContainer.class, this, "theSlot", "field_147006_u", "f" ); - } - catch (Throwable t) - { - return false; - } - - if ( this.mc.thePlayer.inventory.getItemStack() == null && theSlot != null ) - { - for (int j = 0; j < 9; ++j) - { - if ( p_146983_1_ == this.mc.gameSettings.keyBindsHotbar[j].getKeyCode() ) - { - final List slots = this.getInventorySlots(); - for (Slot s : slots) - { - if ( s.getSlotIndex() == j && s.inventory == ((AEBaseContainer) this.inventorySlots).getPlayerInv() ) - { - if ( !s.canTakeStack( ((AEBaseContainer) this.inventorySlots).getPlayerInv().player ) ) - { - return false; - } - } - } - - if ( theSlot.getSlotStackLimit() == 64 ) - { - this.handleMouseClick( theSlot, theSlot.slotNumber, j, 2 ); - return true; - } - else - { - for (Slot s : slots) - { - if ( s.getSlotIndex() == j && s.inventory == ((AEBaseContainer) this.inventorySlots).getPlayerInv() ) - { - NetworkHandler.instance.sendToServer( new PacketSwapSlots( s.slotNumber, theSlot.slotNumber ) ); - return true; - } - } - } - } - } - } - - return false; - } - - @Override - public void drawScreen(int mouse_x, int mouse_y, float btn) + public void drawScreen( int mouse_x, int mouse_y, float btn ) { super.drawScreen( mouse_x, mouse_y, btn ); boolean hasClicked = Mouse.isButtonDown( 0 ); - if ( hasClicked && this.myScrollBar != null ) + if( hasClicked && this.myScrollBar != null ) this.myScrollBar.click( this, mouse_x - this.guiLeft, mouse_y - this.guiTop ); - for (Object c : this.buttonList) + for( Object c : this.buttonList ) { - if ( c instanceof ITooltip ) + if( c instanceof ITooltip ) { ITooltip tooltip = (ITooltip) c; int x = tooltip.xPos(); // ((GuiImgButton) c).xPosition; int y = tooltip.yPos(); // ((GuiImgButton) c).yPosition; - if ( x < mouse_x && x + tooltip.getWidth() > mouse_x && tooltip.isVisible() ) + if( x < mouse_x && x + tooltip.getWidth() > mouse_x && tooltip.isVisible() ) { - if ( y < mouse_y && y + tooltip.getHeight() > mouse_y ) + if( y < mouse_y && y + tooltip.getHeight() > mouse_y ) { - if ( y < 15 ) + if( y < 15 ) y = 15; String msg = tooltip.getMessage(); - if ( msg != null ) + if( msg != null ) this.drawTooltip( x + 11, y + 4, 0, msg ); } } @@ -515,7 +183,7 @@ public abstract class AEBaseGui extends GuiContainer } } - public void drawTooltip(int par2, int par3, int forceWidth, String Msg) + public void drawTooltip( int par2, int par3, int forceWidth, String Msg ) { GL11.glPushAttrib( GL11.GL_ALL_ATTRIB_BITS ); GL11.glDisable( GL12.GL_RESCALE_NORMAL ); @@ -524,17 +192,17 @@ public abstract class AEBaseGui extends GuiContainer GL11.glDisable( GL11.GL_DEPTH_TEST ); String[] var4 = Msg.split( "\n" ); - if ( var4.length > 0 ) + if( var4.length > 0 ) { int var5 = 0; int var6; int var7; - for (var6 = 0; var6 < var4.length; ++var6) + for( var6 = 0; var6 < var4.length; ++var6 ) { var7 = this.fontRendererObj.getStringWidth( var4[var6] ); - if ( var7 > var5 ) + if( var7 > var5 ) { var5 = var7; } @@ -544,17 +212,17 @@ public abstract class AEBaseGui extends GuiContainer var7 = par3 - 12; int var9 = 8; - if ( var4.length > 1 ) + if( var4.length > 1 ) { - var9 += 2 + (var4.length - 1) * 10; + var9 += 2 + ( var4.length - 1 ) * 10; } - if ( this.guiTop + var7 + var9 + 6 > this.height ) + if( this.guiTop + var7 + var9 + 6 > this.height ) { var7 = this.height - var9 - this.guiTop - 6; } - if ( forceWidth > 0 ) + if( forceWidth > 0 ) var5 = forceWidth; this.zLevel = 300.0F; @@ -566,17 +234,17 @@ public abstract class AEBaseGui extends GuiContainer this.drawGradientRect( var6 - 4, var7 - 3, var6 - 3, var7 + var9 + 3, var10, var10 ); this.drawGradientRect( var6 + var5 + 3, var7 - 3, var6 + var5 + 4, var7 + var9 + 3, var10, var10 ); int var11 = 1347420415; - int var12 = (var11 & 16711422) >> 1 | var11 & -16777216; + int var12 = ( var11 & 16711422 ) >> 1 | var11 & -16777216; this.drawGradientRect( var6 - 3, var7 - 3 + 1, var6 - 3 + 1, var7 + var9 + 3 - 1, var11, var12 ); this.drawGradientRect( var6 + var5 + 2, var7 - 3 + 1, var6 + var5 + 3, var7 + var9 + 3 - 1, var11, var12 ); this.drawGradientRect( var6 - 3, var7 - 3, var6 + var5 + 3, var7 - 3 + 1, var11, var11 ); this.drawGradientRect( var6 - 3, var7 + var9 + 2, var6 + var5 + 3, var7 + var9 + 3, var12, var12 ); - for (int var13 = 0; var13 < var4.length; ++var13) + for( int var13 = 0; var13 < var4.length; ++var13 ) { String var14 = var4[var13]; - if ( var13 == 0 ) + if( var13 == 0 ) { var14 = '\u00a7' + Integer.toHexString( 15 ) + var14; } @@ -587,7 +255,7 @@ public abstract class AEBaseGui extends GuiContainer this.fontRendererObj.drawStringWithShadow( var14, var6, var7, -1 ); - if ( var13 == 0 ) + if( var13 == 0 ) { var7 += 2; } @@ -601,41 +269,23 @@ public abstract class AEBaseGui extends GuiContainer GL11.glPopAttrib(); } - public abstract void drawBG(int offsetX, int offsetY, int mouseX, int mouseY); - - public abstract void drawFG(int offsetX, int offsetY, int mouseX, int mouseY); - - public void bindTexture(String base, String file) + @Override + final protected void drawGuiContainerForegroundLayer( int x, int y ) { - ResourceLocation loc = new ResourceLocation( base, "textures/" + file ); - this.mc.getTextureManager().bindTexture( loc ); + int ox = this.guiLeft; // (width - xSize) / 2; + int oy = this.guiTop; // (height - ySize) / 2; + GL11.glColor4f( 1.0F, 1.0F, 1.0F, 1.0F ); + + if( this.myScrollBar != null ) + this.myScrollBar.draw( this ); + + this.drawFG( ox, oy, x, y ); } - public void bindTexture(String file) - { - ResourceLocation loc = new ResourceLocation( "appliedenergistics2", "textures/" + file ); - this.mc.getTextureManager().bindTexture( loc ); - } - - protected void drawItem(int x, int y, ItemStack is) - { - this.zLevel = 100.0F; - itemRender.zLevel = 100.0F; - - GL11.glPushAttrib( GL11.GL_ALL_ATTRIB_BITS ); - GL11.glEnable( GL11.GL_LIGHTING ); - GL11.glEnable( GL12.GL_RESCALE_NORMAL ); - GL11.glEnable( GL11.GL_DEPTH_TEST ); - RenderHelper.enableGUIStandardItemLighting(); - itemRender.renderItemAndEffectIntoGUI( this.fontRendererObj, this.mc.renderEngine, is, x, y ); - GL11.glPopAttrib(); - - itemRender.zLevel = 0.0F; - this.zLevel = 0.0F; - } + public abstract void drawFG( int offsetX, int offsetY, int mouseX, int mouseY ); @Override - final protected void drawGuiContainerBackgroundLayer(float f, int x, int y) + final protected void drawGuiContainerBackgroundLayer( float f, int x, int y ) { int ox = this.guiLeft; // (width - xSize) / 2; int oy = this.guiTop; // (height - ySize) / 2; @@ -643,14 +293,14 @@ public abstract class AEBaseGui extends GuiContainer this.drawBG( ox, oy, x, y ); final List slots = this.getInventorySlots(); - for (Slot slot : slots) + for( Slot slot : slots ) { - if ( slot instanceof OptionalSlotFake ) + if( slot instanceof OptionalSlotFake ) { OptionalSlotFake fs = (OptionalSlotFake) slot; - if ( fs.renderDisabled() ) + if( fs.renderDisabled() ) { - if ( fs.isEnabled() ) + if( fs.isEnabled() ) { this.drawTexturedModalRect( ox + fs.xDisplayPosition - 1, oy + fs.yDisplayPosition - 1, fs.srcX - 1, fs.srcY - 1, 18, 18 ); } @@ -669,42 +319,346 @@ public abstract class AEBaseGui extends GuiContainer } @Override - final protected void drawGuiContainerForegroundLayer(int x, int y) + protected void mouseClicked( int xCoord, int yCoord, int btn ) { - int ox = this.guiLeft; // (width - xSize) / 2; - int oy = this.guiTop; // (height - ySize) / 2; - GL11.glColor4f( 1.0F, 1.0F, 1.0F, 1.0F ); + this.drag_click.clear(); - if ( this.myScrollBar != null ) - this.myScrollBar.draw( this ); + if( btn == 1 ) + { + for( Object o : this.buttonList ) + { + GuiButton guibutton = (GuiButton) o; + if( guibutton.mousePressed( this.mc, xCoord, yCoord ) ) + { + super.mouseClicked( xCoord, yCoord, 0 ); + return; + } + } + } - this.drawFG( ox, oy, x, y ); + super.mouseClicked( xCoord, yCoord, btn ); } - protected String getGuiDisplayName(String in) + @Override + protected void mouseClickMove( int x, int y, int c, long d ) { - return this.hasCustomInventoryName() ? this.getInventoryName() : in; + Slot slot = this.getSlot( x, y ); + ItemStack itemstack = this.mc.thePlayer.inventory.getItemStack(); + + if( slot instanceof SlotFake && itemstack != null ) + { + this.drag_click.add( slot ); + if( this.drag_click.size() > 1 ) + { + for( Slot dr : this.drag_click ) + { + PacketInventoryAction p = new PacketInventoryAction( c == 0 ? InventoryAction.PICKUP_OR_SET_DOWN : InventoryAction.PLACE_SINGLE, dr.slotNumber, 0 ); + NetworkHandler.instance.sendToServer( p ); + } + } + } + else + super.mouseClickMove( x, y, c, d ); } - private String getInventoryName() + @Override + protected void handleMouseClick( Slot slot, int slotIdx, int ctrlDown, int key ) { - return ((AEBaseContainer) this.inventorySlots).customName; + EntityPlayer player = Minecraft.getMinecraft().thePlayer; + + if( slot instanceof SlotFake ) + { + final InventoryAction action = ctrlDown == 1 ? InventoryAction.SPLIT_OR_PLACE_SINGLE : InventoryAction.PICKUP_OR_SET_DOWN; + + if( this.drag_click.size() > 1 ) + return; + + PacketInventoryAction p = new PacketInventoryAction( action, slotIdx, 0 ); + NetworkHandler.instance.sendToServer( p ); + + return; + } + + if( slot instanceof SlotPatternTerm ) + { + if( key == 6 ) + return; // prevent weird double clicks.. + + try + { + NetworkHandler.instance.sendToServer( ( (SlotPatternTerm) slot ).getRequest( key == 1 ) ); + } + catch( IOException e ) + { + AELog.error( e ); + } + } + else if( slot instanceof SlotCraftingTerm ) + { + if( key == 6 ) + return; // prevent weird double clicks.. + + InventoryAction action = null; + if( key == 1 ) + action = InventoryAction.CRAFT_SHIFT; + else + action = ctrlDown == 1 ? InventoryAction.CRAFT_STACK : InventoryAction.CRAFT_ITEM; + + PacketInventoryAction p = new PacketInventoryAction( action, slotIdx, 0 ); + NetworkHandler.instance.sendToServer( p ); + + return; + } + + if( Keyboard.isKeyDown( Keyboard.KEY_SPACE ) ) + { + if( this.enableSpaceClicking() ) + { + IAEItemStack stack = null; + if( slot instanceof SlotME ) + stack = ( (SlotME) slot ).getAEStack(); + + int slotNum = this.getInventorySlots().size(); + + if( !( slot instanceof SlotME ) && slot != null ) + slotNum = slot.slotNumber; + + ( (AEBaseContainer) this.inventorySlots ).setTargetStack( stack ); + PacketInventoryAction p = new PacketInventoryAction( InventoryAction.MOVE_REGION, slotNum, 0 ); + NetworkHandler.instance.sendToServer( p ); + return; + } + } + + if( slot instanceof SlotDisconnected ) + { + InventoryAction action = null; + + switch( key ) + { + case 0: // pickup / set-down. + action = ctrlDown == 1 ? InventoryAction.SPLIT_OR_PLACE_SINGLE : InventoryAction.PICKUP_OR_SET_DOWN; + break; + case 1: + action = ctrlDown == 1 ? InventoryAction.PICKUP_SINGLE : InventoryAction.SHIFT_CLICK; + break; + + case 3: // creative dupe: + + if( player.capabilities.isCreativeMode ) + { + action = InventoryAction.CREATIVE_DUPLICATE; + } + + break; + + default: + case 4: // drop item: + case 6: + } + + if( action != null ) + { + PacketInventoryAction p = new PacketInventoryAction( action, slot.getSlotIndex(), ( (SlotDisconnected) slot ).mySlot.id ); + NetworkHandler.instance.sendToServer( p ); + } + + return; + } + + if( slot instanceof SlotME ) + { + InventoryAction action = null; + IAEItemStack stack = null; + + switch( key ) + { + case 0: // pickup / set-down. + action = ctrlDown == 1 ? InventoryAction.SPLIT_OR_PLACE_SINGLE : InventoryAction.PICKUP_OR_SET_DOWN; + stack = ( (SlotME) slot ).getAEStack(); + + if( stack != null && action == InventoryAction.PICKUP_OR_SET_DOWN && stack.getStackSize() == 0 && player.inventory.getItemStack() == null ) + action = InventoryAction.AUTO_CRAFT; + + break; + case 1: + action = ctrlDown == 1 ? InventoryAction.PICKUP_SINGLE : InventoryAction.SHIFT_CLICK; + stack = ( (SlotME) slot ).getAEStack(); + break; + + case 3: // creative dupe: + + stack = ( (SlotME) slot ).getAEStack(); + if( stack != null && stack.isCraftable() ) + action = InventoryAction.AUTO_CRAFT; + + else if( player.capabilities.isCreativeMode ) + { + IAEItemStack slotItem = ( (SlotME) slot ).getAEStack(); + if( slotItem != null ) + { + action = InventoryAction.CREATIVE_DUPLICATE; + } + } + break; + + default: + case 4: // drop item: + case 6: + } + + if( action != null ) + { + ( (AEBaseContainer) this.inventorySlots ).setTargetStack( stack ); + PacketInventoryAction p = new PacketInventoryAction( action, this.getInventorySlots().size(), 0 ); + NetworkHandler.instance.sendToServer( p ); + } + + return; + } + + if( !this.disableShiftClick && isShiftKeyDown() ) + { + this.disableShiftClick = true; + + if( this.dbl_whichItem == null || this.bl_clicked != slot || this.dbl_clickTimer.elapsed( TimeUnit.MILLISECONDS ) > 150 ) + { + // some simple double click logic. + this.bl_clicked = slot; + this.dbl_clickTimer = Stopwatch.createStarted(); + if( slot != null ) + this.dbl_whichItem = slot.getHasStack() ? slot.getStack().copy() : null; + else + this.dbl_whichItem = null; + } + else if( this.dbl_whichItem != null ) + { + // a replica of the weird broken vanilla feature. + + final List slots = this.getInventorySlots(); + for( Slot inventorySlot : slots ) + { + if( inventorySlot != null && inventorySlot.canTakeStack( this.mc.thePlayer ) && inventorySlot.getHasStack() && inventorySlot.inventory == slot.inventory && Container.func_94527_a( inventorySlot, this.dbl_whichItem, true ) ) + { + this.handleMouseClick( inventorySlot, inventorySlot.slotNumber, ctrlDown, 1 ); + } + } + } + + this.disableShiftClick = false; + } + + super.handleMouseClick( slot, slotIdx, ctrlDown, key ); } - private boolean hasCustomInventoryName() + @Override + protected boolean checkHotbarKeys( int p_146983_1_ ) { - if ( this.inventorySlots instanceof AEBaseContainer ) - return ((AEBaseContainer) this.inventorySlots).customName != null; + Slot theSlot; + + try + { + theSlot = ObfuscationReflectionHelper.getPrivateValue( GuiContainer.class, this, "theSlot", "field_147006_u", "f" ); + } + catch( Throwable t ) + { + return false; + } + + if( this.mc.thePlayer.inventory.getItemStack() == null && theSlot != null ) + { + for( int j = 0; j < 9; ++j ) + { + if( p_146983_1_ == this.mc.gameSettings.keyBindsHotbar[j].getKeyCode() ) + { + final List slots = this.getInventorySlots(); + for( Slot s : slots ) + { + if( s.getSlotIndex() == j && s.inventory == ( (AEBaseContainer) this.inventorySlots ).getPlayerInv() ) + { + if( !s.canTakeStack( ( (AEBaseContainer) this.inventorySlots ).getPlayerInv().player ) ) + { + return false; + } + } + } + + if( theSlot.getSlotStackLimit() == 64 ) + { + this.handleMouseClick( theSlot, theSlot.slotNumber, j, 2 ); + return true; + } + else + { + for( Slot s : slots ) + { + if( s.getSlotIndex() == j && s.inventory == ( (AEBaseContainer) this.inventorySlots ).getPlayerInv() ) + { + NetworkHandler.instance.sendToServer( new PacketSwapSlots( s.slotNumber, theSlot.slotNumber ) ); + return true; + } + } + } + } + } + } + return false; } - protected Slot getSlot(int mouseX, int mouseY) + @Override + public void onGuiClosed() + { + super.onGuiClosed(); + this.subGui = true; // in case the gui is reopened later ( i'm looking at you NEI ) + } + + public abstract void drawBG( int offsetX, int offsetY, int mouseX, int mouseY ); + + @Override + public void handleMouseInput() + { + super.handleMouseInput(); + + int i = Mouse.getEventDWheel(); + if( i != 0 && isShiftKeyDown() ) + { + int x = Mouse.getEventX() * this.width / this.mc.displayWidth; + int y = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1; + this.mouseWheelEvent( x, y, i / Math.abs( i ) ); + } + else if( i != 0 && this.myScrollBar != null ) + this.myScrollBar.wheel( i ); + } + + protected void mouseWheelEvent( int x, int y, int wheel ) + { + Slot slot = this.getSlot( x, y ); + if( slot instanceof SlotME ) + { + IAEItemStack item = ( (SlotME) slot ).getAEStack(); + if( item != null ) + { + ( (AEBaseContainer) this.inventorySlots ).setTargetStack( item ); + InventoryAction direction = wheel > 0 ? InventoryAction.ROLL_DOWN : InventoryAction.ROLL_UP; + int times = Math.abs( wheel ); + final int inventorySize = this.getInventorySlots().size(); + for( int h = 0; h < times; h++ ) + { + PacketInventoryAction p = new PacketInventoryAction( direction, inventorySize, 0 ); + NetworkHandler.instance.sendToServer( p ); + } + } + } + } + + protected Slot getSlot( int mouseX, int mouseY ) { final List slots = this.getInventorySlots(); - for (Slot slot : slots) + for( Slot slot : slots ) { // isPointInRegion - if ( this.func_146978_c( slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, mouseX, mouseY ) ) + if( this.func_146978_c( slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, mouseX, mouseY ) ) { return slot; } @@ -713,65 +667,59 @@ public abstract class AEBaseGui extends GuiContainer return null; } - protected static String join(Collection toolTip, String delimiter) - { - final Joiner joiner = Joiner.on( delimiter ); - - return joiner.join( toolTip ); - } - - boolean useNEI = false; - - private RenderItem setItemRender(RenderItem item) - { - if ( AppEng.instance.isIntegrationEnabled( IntegrationType.NEI ) ) - { - return ((INEI) AppEng.instance.getIntegration( IntegrationType.NEI )).setItemRender( item ); - } - else - { - RenderItem ri = itemRender; - itemRender = item; - return ri; - } - } - - private void safeDrawSlot(Slot s) - { - try - { - // drawSlotInventory - // super.func_146977_a( s );r - GuiContainer.class.getDeclaredMethod( "func_146977_a_original", Slot.class ).invoke( this, s ); - } - catch (Exception err) - { - Tessellator tessellator = Tessellator.instance; - if ( Platform.isDrawing( tessellator ) ) - tessellator.draw(); - } - } - - final AppEngRenderItem aeRenderItem = new AppEngRenderItem(); - - protected boolean isPowered() + protected boolean enableSpaceClicking() { return true; } - public void a(Slot s) + public void bindTexture( String base, String file ) + { + ResourceLocation loc = new ResourceLocation( base, "textures/" + file ); + this.mc.getTextureManager().bindTexture( loc ); + } + + protected void drawItem( int x, int y, ItemStack is ) + { + this.zLevel = 100.0F; + itemRender.zLevel = 100.0F; + + GL11.glPushAttrib( GL11.GL_ALL_ATTRIB_BITS ); + GL11.glEnable( GL11.GL_LIGHTING ); + GL11.glEnable( GL12.GL_RESCALE_NORMAL ); + GL11.glEnable( GL11.GL_DEPTH_TEST ); + RenderHelper.enableGUIStandardItemLighting(); + itemRender.renderItemAndEffectIntoGUI( this.fontRendererObj, this.mc.renderEngine, is, x, y ); + GL11.glPopAttrib(); + + itemRender.zLevel = 0.0F; + this.zLevel = 0.0F; + } + + protected String getGuiDisplayName( String in ) + { + return this.hasCustomInventoryName() ? this.getInventoryName() : in; + } + + private boolean hasCustomInventoryName() + { + if( this.inventorySlots instanceof AEBaseContainer ) + return ( (AEBaseContainer) this.inventorySlots ).customName != null; + return false; + } + + private String getInventoryName() + { + return ( (AEBaseContainer) this.inventorySlots ).customName; + } + + public void a( Slot s ) { this.drawSlot( s ); } - public void func_146977_a(Slot s) + public void drawSlot( Slot s ) { - this.drawSlot( s ); - } - - public void drawSlot(Slot s) - { - if ( s instanceof SlotME ) + if( s instanceof SlotME ) { RenderItem pIR = this.setItemRender( this.aeRenderItem ); try @@ -779,7 +727,7 @@ public abstract class AEBaseGui extends GuiContainer this.zLevel = 100.0F; itemRender.zLevel = 100.0F; - if ( !this.isPowered() ) + if( !this.isPowered() ) { GL11.glDisable( GL11.GL_LIGHTING ); drawRect( s.xDisplayPosition, s.yDisplayPosition, 16 + s.xDisplayPosition, 16 + s.yDisplayPosition, 0x66111111 ); @@ -789,14 +737,14 @@ public abstract class AEBaseGui extends GuiContainer this.zLevel = 0.0F; itemRender.zLevel = 0.0F; - this.aeRenderItem.aeStack = ((SlotME) s).getAEStack(); + this.aeRenderItem.aeStack = ( (SlotME) s ).getAEStack(); this.safeDrawSlot( s ); } - catch (Exception err) + catch( Exception err ) { AELog.warning( "[AppEng] AE prevented crash while drawing slot: " + err.toString() ); - if ( Platform.isDrawing( Tessellator.instance ) ) + if( Platform.isDrawing( Tessellator.instance ) ) Tessellator.instance.draw(); } this.setItemRender( pIR ); @@ -807,10 +755,10 @@ public abstract class AEBaseGui extends GuiContainer try { ItemStack is = s.getStack(); - if ( s instanceof AppEngSlot && (((AppEngSlot) s).renderIconWithItem() || is == null) && (((AppEngSlot) s).shouldDisplay()) ) + if( s instanceof AppEngSlot && ( ( (AppEngSlot) s ).renderIconWithItem() || is == null ) && ( ( (AppEngSlot) s ).shouldDisplay() ) ) { AppEngSlot aes = (AppEngSlot) s; - if ( aes.getIcon() >= 0 ) + if( aes.getIcon() >= 0 ) { this.bindTexture( "guis/states.png" ); @@ -837,47 +785,42 @@ public abstract class AEBaseGui extends GuiContainer float f1 = 0.00390625F; tessellator.startDrawingQuads(); tessellator.setColorRGBA_F( 1.0f, 1.0f, 1.0f, aes.getOpacityOfIcon() ); - tessellator.addVertexWithUV( par1 + 0, par2 + par6, this.zLevel, (par3 + 0) * f, - (par4 + par6) * f1 ); - tessellator.addVertexWithUV( par1 + par5, par2 + par6, this.zLevel, - (par3 + par5) * f, (par4 + par6) * f1 ); - tessellator.addVertexWithUV( par1 + par5, par2 + 0, this.zLevel, - (par3 + par5) * f, (par4 + 0) * f1 ); - tessellator.addVertexWithUV( par1 + 0, par2 + 0, this.zLevel, (par3 + 0) * f, - (par4 + 0) * f1 ); + tessellator.addVertexWithUV( par1 + 0, par2 + par6, this.zLevel, ( par3 + 0 ) * f, ( par4 + par6 ) * f1 ); + tessellator.addVertexWithUV( par1 + par5, par2 + par6, this.zLevel, ( par3 + par5 ) * f, ( par4 + par6 ) * f1 ); + tessellator.addVertexWithUV( par1 + par5, par2 + 0, this.zLevel, ( par3 + par5 ) * f, ( par4 + 0 ) * f1 ); + tessellator.addVertexWithUV( par1 + 0, par2 + 0, this.zLevel, ( par3 + 0 ) * f, ( par4 + 0 ) * f1 ); tessellator.setColorRGBA_F( 1.0f, 1.0f, 1.0f, 1.0f ); tessellator.draw(); } - catch (Exception err) + catch( Exception err ) { - if ( Platform.isDrawing( tessellator ) ) + if( Platform.isDrawing( tessellator ) ) tessellator.draw(); } GL11.glPopAttrib(); } } - if ( is != null && s instanceof AppEngSlot ) + if( is != null && s instanceof AppEngSlot ) { - if ( ((AppEngSlot) s).isValid == hasCalculatedValidness.NotAvailable ) + if( ( (AppEngSlot) s ).isValid == hasCalculatedValidness.NotAvailable ) { - boolean isValid = s.isItemValid( is ) || s instanceof SlotOutput || s instanceof AppEngCraftingSlot || s instanceof SlotDisabled - || s instanceof SlotInaccessible || s instanceof SlotFake || s instanceof SlotRestrictedInput || s instanceof SlotDisconnected; - if ( isValid && s instanceof SlotRestrictedInput ) + boolean isValid = s.isItemValid( is ) || s instanceof SlotOutput || s instanceof AppEngCraftingSlot || s instanceof SlotDisabled || s instanceof SlotInaccessible || s instanceof SlotFake || s instanceof SlotRestrictedInput || s instanceof SlotDisconnected; + if( isValid && s instanceof SlotRestrictedInput ) { try { - isValid = ((SlotRestrictedInput) s).isValid( is, this.mc.theWorld ); + isValid = ( (SlotRestrictedInput) s ).isValid( is, this.mc.theWorld ); } - catch (Exception err) + catch( Exception err ) { AELog.error( err ); } } - ((AppEngSlot) s).isValid = isValid ? hasCalculatedValidness.Valid : hasCalculatedValidness.Invalid; + ( (AppEngSlot) s ).isValid = isValid ? hasCalculatedValidness.Valid : hasCalculatedValidness.Invalid; } - if ( ((AppEngSlot) s).isValid == hasCalculatedValidness.Invalid ) + if( ( (AppEngSlot) s ).isValid == hasCalculatedValidness.Invalid ) { this.zLevel = 100.0F; itemRender.zLevel = 100.0F; @@ -891,9 +834,9 @@ public abstract class AEBaseGui extends GuiContainer } } - if ( s instanceof AppEngSlot ) + if( s instanceof AppEngSlot ) { - ((AppEngSlot) s).isDisplay = true; + ( (AppEngSlot) s ).isDisplay = true; this.safeDrawSlot( s ); } else @@ -901,7 +844,7 @@ public abstract class AEBaseGui extends GuiContainer return; } - catch (Exception err) + catch( Exception err ) { AELog.warning( "[AppEng] AE prevented crash while drawing slot: " + err.toString() ); } @@ -910,4 +853,49 @@ public abstract class AEBaseGui extends GuiContainer this.safeDrawSlot( s ); } + private RenderItem setItemRender( RenderItem item ) + { + if( AppEng.instance.isIntegrationEnabled( IntegrationType.NEI ) ) + { + return ( (INEI) AppEng.instance.getIntegration( IntegrationType.NEI ) ).setItemRender( item ); + } + else + { + RenderItem ri = itemRender; + itemRender = item; + return ri; + } + } + + protected boolean isPowered() + { + return true; + } + + private void safeDrawSlot( Slot s ) + { + try + { + // drawSlotInventory + // super.func_146977_a( s );r + GuiContainer.class.getDeclaredMethod( "func_146977_a_original", Slot.class ).invoke( this, s ); + } + catch( Exception err ) + { + Tessellator tessellator = Tessellator.instance; + if( Platform.isDrawing( tessellator ) ) + tessellator.draw(); + } + } + + public void bindTexture( String file ) + { + ResourceLocation loc = new ResourceLocation( "appliedenergistics2", "textures/" + file ); + this.mc.getTextureManager().bindTexture( loc ); + } + + public void func_146977_a( Slot s ) + { + this.drawSlot( s ); + } } diff --git a/src/main/java/appeng/client/render/AppEngRenderItem.java b/src/main/java/appeng/client/render/AppEngRenderItem.java index 1b899da54..cfbd37c77 100644 --- a/src/main/java/appeng/client/render/AppEngRenderItem.java +++ b/src/main/java/appeng/client/render/AppEngRenderItem.java @@ -57,8 +57,8 @@ public class AppEngRenderItem extends RenderItem { if ( is != null ) { - float ScaleFactor = AEConfig.instance.useTerminalUseLargeFont() ? 0.85f : 0.5f; - float RScaleFactor = 1.0f / ScaleFactor; + float scaleFactor = AEConfig.instance.useTerminalUseLargeFont() ? 0.85f : 0.5f; + float inverseScaleFactor = 1.0f / scaleFactor; int offset = AEConfig.instance.useTerminalUseLargeFont() ? 0 : -1; boolean unicodeFlag = par1FontRenderer.getUnicodeFlag(); @@ -93,9 +93,9 @@ public class AppEngRenderItem extends RenderItem GL11.glDisable( GL11.GL_LIGHTING ); GL11.glDisable( GL11.GL_DEPTH_TEST ); GL11.glPushMatrix(); - GL11.glScaled( ScaleFactor, ScaleFactor, ScaleFactor ); - int X = (int) (((float) par4 + offset + 16.0f - par1FontRenderer.getStringWidth( var6 ) * ScaleFactor) * RScaleFactor); - int Y = (int) (((float) par5 + offset + 16.0f - 7.0f * ScaleFactor) * RScaleFactor); + GL11.glScaled( scaleFactor, scaleFactor, scaleFactor ); + int X = (int) (((float) par4 + offset + 16.0f - par1FontRenderer.getStringWidth( var6 ) * scaleFactor) * inverseScaleFactor); + int Y = (int) (((float) par5 + offset + 16.0f - 7.0f * scaleFactor) * inverseScaleFactor); par1FontRenderer.drawStringWithShadow( var6, X, Y, 16777215 ); GL11.glPopMatrix(); GL11.glEnable( GL11.GL_LIGHTING ); @@ -103,8 +103,7 @@ public class AppEngRenderItem extends RenderItem } long amount = this.aeStack != null ? this.aeStack.getStackSize() : is.stackSize; - if ( amount > 999999999999L ) - amount = 999999999999L; + amount = Math.max( amount, 999999999999L ); if ( amount != 0 ) { @@ -156,9 +155,9 @@ public class AppEngRenderItem extends RenderItem GL11.glDisable( GL11.GL_LIGHTING ); GL11.glDisable( GL11.GL_DEPTH_TEST ); GL11.glPushMatrix(); - GL11.glScaled( ScaleFactor, ScaleFactor, ScaleFactor ); - int X = (int) (((float) par4 + offset + 16.0f - par1FontRenderer.getStringWidth( var6 ) * ScaleFactor) * RScaleFactor); - int Y = (int) (((float) par5 + offset + 16.0f - 7.0f * ScaleFactor) * RScaleFactor); + GL11.glScaled( scaleFactor, scaleFactor, scaleFactor ); + int X = (int) (((float) par4 + offset + 16.0f - par1FontRenderer.getStringWidth( var6 ) * scaleFactor) * inverseScaleFactor); + int Y = (int) (((float) par5 + offset + 16.0f - 7.0f * scaleFactor) * inverseScaleFactor); par1FontRenderer.drawStringWithShadow( var6, X, Y, 16777215 ); GL11.glPopMatrix(); GL11.glEnable( GL11.GL_LIGHTING ); diff --git a/src/main/java/appeng/client/render/BaseBlockRender.java b/src/main/java/appeng/client/render/BaseBlockRender.java index 20ecc331f..f411fe770 100644 --- a/src/main/java/appeng/client/render/BaseBlockRender.java +++ b/src/main/java/appeng/client/render/BaseBlockRender.java @@ -22,6 +22,8 @@ package appeng.client.render; import java.nio.FloatBuffer; import java.util.EnumSet; +import javax.annotation.Nullable; + import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; @@ -432,6 +434,7 @@ public class BaseBlockRender renderer.uvRotateBottom = renderer.uvRotateEast = renderer.uvRotateNorth = renderer.uvRotateSouth = renderer.uvRotateTop = renderer.uvRotateWest = 0; } + @Nullable public IOrientable getOrientable( AEBaseBlock block, IBlockAccess w, int x, int y, int z ) { if ( block.hasBlockTileEntity() ) diff --git a/src/main/java/appeng/client/render/blocks/RenderBlockInscriber.java b/src/main/java/appeng/client/render/blocks/RenderBlockInscriber.java index 2d5042e3b..f267058e8 100644 --- a/src/main/java/appeng/client/render/blocks/RenderBlockInscriber.java +++ b/src/main/java/appeng/client/render/blocks/RenderBlockInscriber.java @@ -102,6 +102,7 @@ public class RenderBlockInscriber extends BaseBlockRender BlockInscriber blk = (BlockInscriber) block; IOrientable te = this.getOrientable( block, world, x, y, z ); + if ( te == null ) return false; ForgeDirection fdy = te.getUp(); ForgeDirection fdz = te.getForward(); diff --git a/src/main/java/appeng/client/render/blocks/RenderBlockInterface.java b/src/main/java/appeng/client/render/blocks/RenderBlockInterface.java index 181c8e463..368aa53bb 100644 --- a/src/main/java/appeng/client/render/blocks/RenderBlockInterface.java +++ b/src/main/java/appeng/client/render/blocks/RenderBlockInterface.java @@ -40,10 +40,9 @@ public class RenderBlockInterface extends BaseBlockRender public boolean renderInWorld(AEBaseBlock block, IBlockAccess world, int x, int y, int z, RenderBlocks renderer) { TileInterface ti = block.getTileEntity( world, x, y, z ); - BlockRenderInfo info = block.getRendererInstance(); - if ( ti.getForward() != ForgeDirection.UNKNOWN ) + if ( ti != null && ti.getForward() != ForgeDirection.UNKNOWN ) { IIcon side = ExtraBlockTextures.BlockInterfaceAlternateArrow.getIcon(); info.setTemporaryRenderIcons( ExtraBlockTextures.BlockInterfaceAlternate.getIcon(), block.getIcon( 0, 0 ), side, side, side, side ); diff --git a/src/main/java/appeng/client/render/blocks/RenderMEChest.java b/src/main/java/appeng/client/render/blocks/RenderMEChest.java index 63d3858f7..eb29f26cb 100644 --- a/src/main/java/appeng/client/render/blocks/RenderMEChest.java +++ b/src/main/java/appeng/client/render/blocks/RenderMEChest.java @@ -67,6 +67,8 @@ public class RenderMEChest extends BaseBlockRender TileChest sp = imb.getTileEntity( world, x, y, z ); renderer.setRenderBounds( 0, 0, 0, 1, 1, 1 ); + if ( sp == null ) return false; + ForgeDirection up = sp.getUp(); ForgeDirection forward = sp.getForward(); ForgeDirection west = Platform.crossProduct( forward, up ); diff --git a/src/main/java/appeng/client/render/blocks/RenderSpatialPylon.java b/src/main/java/appeng/client/render/blocks/RenderSpatialPylon.java index 1286048aa..851477b6c 100644 --- a/src/main/java/appeng/client/render/blocks/RenderSpatialPylon.java +++ b/src/main/java/appeng/client/render/blocks/RenderSpatialPylon.java @@ -18,6 +18,7 @@ package appeng.client.render.blocks; + import net.minecraft.client.renderer.RenderBlocks; import net.minecraft.client.renderer.Tessellator; import net.minecraft.item.ItemStack; @@ -32,15 +33,17 @@ import appeng.client.render.BlockRenderInfo; import appeng.client.texture.ExtraBlockTextures; import appeng.tile.spatial.TileSpatialPylon; + public class RenderSpatialPylon extends BaseBlockRender { - public RenderSpatialPylon() { + public RenderSpatialPylon() + { super( false, 0 ); } @Override - public void renderInventory(AEBaseBlock block, ItemStack is, RenderBlocks renderer, ItemRenderType type, Object[] obj) + public void renderInventory( AEBaseBlock block, ItemStack is, RenderBlocks renderer, ItemRenderType type, Object[] obj ) { renderer.overrideBlockTexture = ExtraBlockTextures.BlockSpatialPylon_dim.getIcon(); super.renderInventory( block, is, renderer, type, obj ); @@ -49,28 +52,28 @@ public class RenderSpatialPylon extends BaseBlockRender } @Override - public boolean renderInWorld(AEBaseBlock imb, IBlockAccess world, int x, int y, int z, RenderBlocks renderer) + public boolean renderInWorld( AEBaseBlock imb, IBlockAccess world, int x, int y, int z, RenderBlocks renderer ) { renderer.setRenderBounds( 0, 0, 0, 1, 1, 1 ); TileSpatialPylon sp = imb.getTileEntity( world, x, y, z ); - int displayBits = sp.getDisplayBits(); + int displayBits = ( sp == null ) ? 0 : sp.getDisplayBits(); ForgeDirection ori = ForgeDirection.UNKNOWN; - if ( displayBits != 0 ) + if( displayBits != 0 ) { - if ( (displayBits & sp.DISPLAY_Z) == sp.DISPLAY_X ) + if( ( displayBits & sp.DISPLAY_Z ) == sp.DISPLAY_X ) { ori = ForgeDirection.EAST; - if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_END_MAX ) + if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_END_MAX ) { renderer.uvRotateEast = 1; renderer.uvRotateWest = 2; renderer.uvRotateTop = 2; renderer.uvRotateBottom = 1; } - else if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_END_MIN ) + else if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_END_MIN ) { renderer.uvRotateEast = 2; renderer.uvRotateWest = 1; @@ -86,10 +89,10 @@ public class RenderSpatialPylon extends BaseBlockRender } } - else if ( (displayBits & sp.DISPLAY_Z) == sp.DISPLAY_Y ) + else if( ( displayBits & sp.DISPLAY_Z ) == sp.DISPLAY_Y ) { ori = ForgeDirection.UP; - if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_END_MAX ) + if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_END_MAX ) { renderer.uvRotateNorth = 3; renderer.uvRotateSouth = 3; @@ -98,15 +101,15 @@ public class RenderSpatialPylon extends BaseBlockRender } } - else if ( (displayBits & sp.DISPLAY_Z) == sp.DISPLAY_Z ) + else if( ( displayBits & sp.DISPLAY_Z ) == sp.DISPLAY_Z ) { ori = ForgeDirection.NORTH; - if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_END_MAX ) + if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_END_MAX ) { renderer.uvRotateSouth = 1; renderer.uvRotateNorth = 2; } - else if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_END_MIN ) + else if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_END_MIN ) { renderer.uvRotateNorth = 1; renderer.uvRotateSouth = 2; @@ -122,33 +125,23 @@ public class RenderSpatialPylon extends BaseBlockRender BlockRenderInfo bri = imb.getRendererInstance(); bri.setTemporaryRenderIcon( null ); - bri.setTemporaryRenderIcons( this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.UP ), - this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.DOWN ), - this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.SOUTH ), - this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.NORTH ), - this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.EAST ), - this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.WEST ) ); + bri.setTemporaryRenderIcons( this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.UP ), this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.DOWN ), this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.SOUTH ), this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.NORTH ), this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.EAST ), this.getBlockTextureFromSideOutside( imb, sp, displayBits, ori, ForgeDirection.WEST ) ); boolean r = renderer.renderStandardBlock( imb, x, y, z ); - if ( (displayBits & sp.DISPLAY_POWERED_ENABLED) == sp.DISPLAY_POWERED_ENABLED ) + if( ( displayBits & sp.DISPLAY_POWERED_ENABLED ) == sp.DISPLAY_POWERED_ENABLED ) { int bn = 15; Tessellator.instance.setBrightness( bn << 20 | bn << 4 ); Tessellator.instance.setColorOpaque_I( 0xffffff ); - for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) + for( ForgeDirection d : ForgeDirection.VALID_DIRECTIONS ) this.renderFace( x, y, z, imb, this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, d ), renderer, d ); } else { bri.setTemporaryRenderIcon( null ); - bri.setTemporaryRenderIcons( this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.UP ), - this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.DOWN ), - this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.SOUTH ), - this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.NORTH ), - this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.EAST ), - this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.WEST ) ); + bri.setTemporaryRenderIcons( this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.UP ), this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.DOWN ), this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.SOUTH ), this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.NORTH ), this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.EAST ), this.getBlockTextureFromSideInside( imb, sp, displayBits, ori, ForgeDirection.WEST ) ); renderer.renderStandardBlock( imb, x, y, z ); } @@ -169,38 +162,38 @@ public class RenderSpatialPylon extends BaseBlockRender return result; } - private IIcon getBlockTextureFromSideOutside(AEBaseBlock blk, TileSpatialPylon sp, int displayBits, ForgeDirection ori, ForgeDirection dir) + private IIcon getBlockTextureFromSideOutside( AEBaseBlock blk, TileSpatialPylon sp, int displayBits, ForgeDirection ori, ForgeDirection dir ) { - if ( ori == dir || ori.getOpposite() == dir ) + if( ori == dir || ori.getOpposite() == dir ) return blk.getRendererInstance().getTexture( dir ); - if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_MIDDLE ) + if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_MIDDLE ) return ExtraBlockTextures.BlockSpatialPylonC.getIcon(); - else if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_END_MIN ) + else if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_END_MIN ) return ExtraBlockTextures.BlockSpatialPylonE.getIcon(); - else if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_END_MAX ) + else if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_END_MAX ) return ExtraBlockTextures.BlockSpatialPylonE.getIcon(); return blk.getIcon( 0, 0 ); } - private IIcon getBlockTextureFromSideInside(AEBaseBlock blk, TileSpatialPylon sp, int displayBits, ForgeDirection ori, ForgeDirection dir) + private IIcon getBlockTextureFromSideInside( AEBaseBlock blk, TileSpatialPylon sp, int displayBits, ForgeDirection ori, ForgeDirection dir ) { - boolean good = (displayBits & sp.DISPLAY_ENABLED) == sp.DISPLAY_ENABLED; + boolean good = ( displayBits & sp.DISPLAY_ENABLED ) == sp.DISPLAY_ENABLED; - if ( ori == dir || ori.getOpposite() == dir ) + if( ori == dir || ori.getOpposite() == dir ) return good ? ExtraBlockTextures.BlockSpatialPylon_dim.getIcon() : ExtraBlockTextures.BlockSpatialPylon_red.getIcon(); - if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_MIDDLE ) + if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_MIDDLE ) return good ? ExtraBlockTextures.BlockSpatialPylonC_dim.getIcon() : ExtraBlockTextures.BlockSpatialPylonC_red.getIcon(); - else if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_END_MIN ) + else if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_END_MIN ) return good ? ExtraBlockTextures.BlockSpatialPylonE_dim.getIcon() : ExtraBlockTextures.BlockSpatialPylonE_red.getIcon(); - else if ( (displayBits & sp.DISPLAY_MIDDLE) == sp.DISPLAY_END_MAX ) + else if( ( displayBits & sp.DISPLAY_MIDDLE ) == sp.DISPLAY_END_MAX ) return good ? ExtraBlockTextures.BlockSpatialPylonE_dim.getIcon() : ExtraBlockTextures.BlockSpatialPylonE_red.getIcon(); return blk.getIcon( 0, 0 ); diff --git a/src/main/java/appeng/tile/storage/TileIOPort.java b/src/main/java/appeng/tile/storage/TileIOPort.java index 8ec3901ac..630096a94 100644 --- a/src/main/java/appeng/tile/storage/TileIOPort.java +++ b/src/main/java/appeng/tile/storage/TileIOPort.java @@ -21,6 +21,7 @@ package appeng.tile.storage; import java.util.ArrayList; +import net.minecraft.block.Block; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -114,7 +115,8 @@ public class TileIOPort extends AENetworkInvTile implements IUpgradeableHost, IC this.mySrc = new MachineSource( this ); this.lastRedstoneState = YesNo.UNDECIDED; - this.upgrades = new BlockUpgradeInventory( this.getBlockType(), this, 3 ); + final Block ioPortBlock = AEApi.instance().definitions().blocks().iOPort().maybeBlock().get(); + this.upgrades = new BlockUpgradeInventory( ioPortBlock, this, 3 ); } @TileEvent( TileEventType.WORLD_NBT_WRITE ) diff --git a/src/main/java/appeng/util/item/AEItemStack.java b/src/main/java/appeng/util/item/AEItemStack.java index 7538236c0..aa894c219 100644 --- a/src/main/java/appeng/util/item/AEItemStack.java +++ b/src/main/java/appeng/util/item/AEItemStack.java @@ -18,12 +18,15 @@ package appeng.util.item; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.security.InvalidParameterException; import java.util.List; +import javax.annotation.Nullable; import io.netty.buffer.ByteBuf; @@ -40,50 +43,40 @@ import cpw.mods.fml.relauncher.SideOnly; import appeng.api.config.FuzzyMode; import appeng.api.storage.StorageChannel; import appeng.api.storage.data.IAEItemStack; -import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IAETagCompound; import appeng.util.Platform; + public final class AEItemStack extends AEStack implements IAEItemStack, Comparable { AEItemDef def; - @Override - public String toString() + private AEItemStack( AEItemStack is ) { - return this.getItemStack().toString(); - } - - @Override - public void add(IAEItemStack option) - { - if ( option == null ) - return; - - // if ( priority < ((AEItemStack) option).priority ) - // priority = ((AEItemStack) option).priority; - - this.incStackSize( option.getStackSize() ); - this.setCountRequestable( this.getCountRequestable() + option.getCountRequestable() ); - this.setCraftable( this.isCraftable() || option.isCraftable() ); - } - - private AEItemStack(AEItemStack is) { this.def = is.def; this.stackSize = is.stackSize; this.setCraftable( is.isCraftable() ); this.setCountRequestable( is.getCountRequestable() ); } - private AEItemStack( ItemStack is ) { - if ( is == null ) - throw new RuntimeException( "Invalid Itemstack." ); + private AEItemStack( ItemStack is ) + { + if( is == null ) + { + throw new InvalidParameterException( "null is not a valid ItemStack for AEItemStack." ); + } - this.def = new AEItemDef( is.getItem() ); + final Item item = is.getItem(); + if( item == null ) + { + throw new InvalidParameterException( "Contained item is null, thus not a valid ItemStack for AEItemStack." ); + } - if ( this.def.item == null ) - throw new RuntimeException( "This ItemStack is bad, it has a null item." ); + this.def = new AEItemDef( item ); + + if( this.def.item == null ) + throw new InvalidParameterException( "This ItemStack is bad, it has a null item." ); /* * Prevent an Item from changing the damage value on me... Either, this or a core mod. @@ -103,7 +96,7 @@ public final class AEItemStack extends AEStack implements IAEItemS this.def.maxDamage = is.getMaxDamage(); NBTTagCompound tagCompound = is.getTagCompound(); - if ( tagCompound != null ) + if( tagCompound != null ) this.def.tagCompound = (AESharedNBT) AESharedNBT.getSharedTagCompound( tagCompound, is ); this.stackSize = is.stackSize; @@ -114,74 +107,93 @@ public final class AEItemStack extends AEStack implements IAEItemS this.def.isOre = OreHelper.INSTANCE.isOre( is ); } + public static IAEItemStack loadItemStackFromNBT( NBTTagCompound i ) + { + if( i == null ) + return null; + + ItemStack itemstack = ItemStack.loadItemStackFromNBT( i ); + if( itemstack == null ) + return null; + + AEItemStack item = AEItemStack.create( itemstack ); + // item.priority = i.getInteger( "Priority" ); + item.stackSize = i.getLong( "Cnt" ); + item.setCountRequestable( i.getLong( "Req" ) ); + item.setCraftable( i.getBoolean( "Craft" ) ); + return item; + } + + @Nullable public static AEItemStack create( ItemStack stack ) { + if( stack == null ) + { + return null; + } + return new AEItemStack( stack ); } - public static IAEStack create( IAEStack stack ) + public static IAEItemStack loadItemStackFromPacket( ByteBuf data ) throws IOException { - return stack.copy(); - } + byte mask = data.readByte(); + // byte PriorityType = (byte) (mask & 0x03); + byte StackType = (byte) ( ( mask & 0x0C ) >> 2 ); + byte CountReqType = (byte) ( ( mask & 0x30 ) >> 4 ); + boolean isCraftable = ( mask & 0x40 ) > 0; + boolean hasTagCompound = ( mask & 0x80 ) > 0; - @Override - public Item getItem() - { - return this.def.item; - } + // don't send this... + NBTTagCompound d = new NBTTagCompound(); - @Override - public boolean equals(Object ia) - { - if ( ia instanceof AEItemStack ) + d.setShort( "id", data.readShort() ); + d.setShort( "Damage", data.readShort() ); + d.setByte( "Count", (byte) 0 ); + + if( hasTagCompound ) { - return ((AEItemStack) ia).def.equals( this.def );// && def.tagCompound == ((AEItemStack) ia).def.tagCompound; + int len = data.readInt(); + + byte[] bd = new byte[len]; + data.readBytes( bd ); + + ByteArrayInputStream di = new ByteArrayInputStream( bd ); + d.setTag( "tag", CompressedStreamTools.read( new DataInputStream( di ) ) ); } - else if ( ia instanceof ItemStack ) - { - ItemStack is = (ItemStack) ia; - if ( is.getItem() == this.def.item && is.getItemDamage() == this.def.damageValue ) - { - NBTTagCompound ta = this.def.tagCompound; - NBTTagCompound tb = is.getTagCompound(); - if ( ta == tb ) - return true; + // long priority = getPacketValue( PriorityType, data ); + long stackSize = getPacketValue( StackType, data ); + long countRequestable = getPacketValue( CountReqType, data ); - if ( (ta == null && tb == null) || (ta != null && ta.hasNoTags() && tb == null) || (tb != null && tb.hasNoTags() && ta == null) - || (ta != null && ta.hasNoTags() && tb != null && tb.hasNoTags()) ) - return true; + ItemStack itemstack = ItemStack.loadItemStackFromNBT( d ); + if( itemstack == null ) + return null; - if ( (ta == null && tb != null) || (ta != null && tb == null) ) - return false; - - if ( AESharedNBT.isShared( tb ) ) - return ta == tb; - - return Platform.NBTEqualityTest( ta, tb ); - } - } - return false; + AEItemStack item = AEItemStack.create( itemstack ); + // item.priority = (int) priority; + item.stackSize = stackSize; + item.setCountRequestable( countRequestable ); + item.setCraftable( isCraftable ); + return item; } @Override - public ItemStack getItemStack() + public void add( IAEItemStack option ) { - ItemStack is = new ItemStack( this.def.item, (int) Math.min( Integer.MAX_VALUE, this.stackSize ), this.def.damageValue ); - if ( this.def.tagCompound != null ) - is.setTagCompound( this.def.tagCompound.getNBTTagCompoundCopy() ); + if( option == null ) + return; - return is; + // if ( priority < ((AEItemStack) option).priority ) + // priority = ((AEItemStack) option).priority; + + this.incStackSize( option.getStackSize() ); + this.setCountRequestable( this.getCountRequestable() + option.getCountRequestable() ); + this.setCraftable( this.isCraftable() || option.isCraftable() ); } @Override - public IAEItemStack copy() - { - return new AEItemStack( this ); - } - - @Override - public void writeToNBT(NBTTagCompound i) + public void writeToNBT( NBTTagCompound i ) { /* * Mojang Fucked this over ; GC Optimization - Ugly Yes, but it saves a lot in the memory department. @@ -224,247 +236,63 @@ public final class AEItemStack extends AEStack implements IAEItemS */ i.setShort( "Damage", (short) this.def.damageValue ); - if ( this.def.tagCompound != null ) + if( this.def.tagCompound != null ) i.setTag( "tag", this.def.tagCompound ); else i.removeTag( "tag" ); - - } - - public static IAEItemStack loadItemStackFromNBT(NBTTagCompound i) - { - if ( i == null ) - return null; - - ItemStack itemstack = ItemStack.loadItemStackFromNBT( i ); - if ( itemstack == null ) - return null; - - AEItemStack item = AEItemStack.create( itemstack ); - // item.priority = i.getInteger( "Priority" ); - item.stackSize = i.getLong( "Cnt" ); - item.setCountRequestable( i.getLong( "Req" ) ); - item.setCraftable( i.getBoolean( "Craft" ) ); - return item; } @Override - public boolean hasTagCompound() + public boolean fuzzyComparison( Object st, FuzzyMode Mode ) { - return this.def.tagCompound != null; - } - - @Override - public int hashCode() - { - return this.def.myHash; - } - - @Override - public int compareTo(AEItemStack b) - { - int id = this.def.itemID - b.def.itemID; - if (id != 0) - return id; - - int damageValue = this.def.damageValue - b.def.damageValue ; - if (damageValue != 0) - return damageValue; - - int displayDamage = this.def.displayDamage - b.def.displayDamage; - if (displayDamage != 0) - return displayDamage; - - return (this.def.tagCompound == b.def.tagCompound) ? 0 : this.compareNBT( b.def ); - } - - private int compareNBT(AEItemDef b) - { - int nbt = this.compare( (this.def.tagCompound == null ? 0 : this.def.tagCompound.getHash()), (b.tagCompound == null ? 0 : b.tagCompound.getHash()) ); - if ( nbt == 0 ) - return this.compare( System.identityHashCode( this.def.tagCompound ), System.identityHashCode( b.tagCompound ) ); - return nbt; - } - - private int compare(int l, int m) - { - return l < m ? -1 : (l > m ? 1 : 0); - } - - @SideOnly(Side.CLIENT) - public List getToolTip() - { - if ( this.def.tooltip != null ) - return this.def.tooltip; - - return this.def.tooltip = Platform.getTooltip( this.getItemStack() ); - } - - @SideOnly(Side.CLIENT) - public String getDisplayName() - { - if ( this.def.displayName != null ) - return this.def.displayName; - - return this.def.displayName = Platform.getItemDisplayName( this.getItemStack() ); - } - - @SideOnly(Side.CLIENT) - public String getModID() - { - if ( this.def.uniqueID != null ) - return this.getModName( this.def.uniqueID ); - - return this.getModName( this.def.uniqueID = GameRegistry.findUniqueIdentifierFor( this.def.item ) ); - } - - private String getModName(UniqueIdentifier uniqueIdentifier) - { - if ( uniqueIdentifier == null ) - return "** Null"; - - return uniqueIdentifier.modId == null ? "** Null" : uniqueIdentifier.modId; - } - - @Override - public IAEItemStack empty() - { - IAEItemStack dup = this.copy(); - dup.reset(); - return dup; - } - - @Override - public int getItemDamage() - { - return this.def.damageValue; - } - - @Override - void writeIdentity(ByteBuf i) throws IOException - { - i.writeShort( Item.itemRegistry.getIDForObject( this.def.item ) ); - i.writeShort( this.getItemDamage() ); - } - - @Override - void readNBT(ByteBuf i) throws IOException - { - if ( this.hasTagCompound() ) - { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - DataOutputStream data = new DataOutputStream( bytes ); - - CompressedStreamTools.write( (NBTTagCompound) this.getTagCompound(), data ); - - byte[] tagBytes = bytes.toByteArray(); - int size = tagBytes.length; - - i.writeInt( size ); - i.writeBytes( tagBytes ); - } - } - - public static IAEItemStack loadItemStackFromPacket(ByteBuf data) throws IOException - { - byte mask = data.readByte(); - // byte PriorityType = (byte) (mask & 0x03); - byte StackType = (byte) ((mask & 0x0C) >> 2); - byte CountReqType = (byte) ((mask & 0x30) >> 4); - boolean isCraftable = (mask & 0x40) > 0; - boolean hasTagCompound = (mask & 0x80) > 0; - - // don't send this... - NBTTagCompound d = new NBTTagCompound(); - - d.setShort( "id", data.readShort() ); - d.setShort( "Damage", data.readShort() ); - d.setByte( "Count", (byte) 0 ); - - if ( hasTagCompound ) - { - int len = data.readInt(); - - byte[] bd = new byte[len]; - data.readBytes( bd ); - - ByteArrayInputStream di = new ByteArrayInputStream( bd ); - d.setTag( "tag", CompressedStreamTools.read( new DataInputStream( di ) ) ); - } - - // long priority = getPacketValue( PriorityType, data ); - long stackSize = getPacketValue( StackType, data ); - long countRequestable = getPacketValue( CountReqType, data ); - - ItemStack itemstack = ItemStack.loadItemStackFromNBT( d ); - if ( itemstack == null ) - return null; - - AEItemStack item = AEItemStack.create( itemstack ); - // item.priority = (int) priority; - item.stackSize = stackSize; - item.setCountRequestable( countRequestable ); - item.setCraftable( isCraftable ); - return item; - } - - @Override - public boolean sameOre(IAEItemStack is) - { - return OreHelper.INSTANCE.sameOre( this, is ); - } - - @Override - public boolean fuzzyComparison(Object st, FuzzyMode Mode) - { - if ( st instanceof IAEItemStack ) + if( st instanceof IAEItemStack ) { IAEItemStack o = (IAEItemStack) st; - if ( this.sameOre( o ) ) + if( this.sameOre( o ) ) return true; - if ( o.getItem() == this.getItem() ) + if( o.getItem() == this.getItem() ) { - if ( this.def.item.isDamageable() ) + if( this.def.item.isDamageable() ) { ItemStack a = this.getItemStack(); ItemStack b = o.getItemStack(); try { - if ( Mode == FuzzyMode.IGNORE_ALL ) + if( Mode == FuzzyMode.IGNORE_ALL ) { return true; } - else if ( Mode == FuzzyMode.PERCENT_99 ) + else if( Mode == FuzzyMode.PERCENT_99 ) { - return (a.getItemDamageForDisplay() > 1) == (b.getItemDamageForDisplay() > 1); + return ( a.getItemDamageForDisplay() > 1 ) == ( b.getItemDamageForDisplay() > 1 ); } else { float APercentDamaged = 1.0f - (float) a.getItemDamageForDisplay() / (float) a.getMaxDamage(); float BPercentDamaged = 1.0f - (float) b.getItemDamageForDisplay() / (float) b.getMaxDamage(); - return (APercentDamaged > Mode.breakPoint) == (BPercentDamaged > Mode.breakPoint); + return ( APercentDamaged > Mode.breakPoint ) == ( BPercentDamaged > Mode.breakPoint ); } } - catch (Throwable e) + catch( Throwable e ) { - if ( Mode == FuzzyMode.IGNORE_ALL ) + if( Mode == FuzzyMode.IGNORE_ALL ) { return true; } - else if ( Mode == FuzzyMode.PERCENT_99 ) + else if( Mode == FuzzyMode.PERCENT_99 ) { - return (a.getItemDamage() > 1) == (b.getItemDamage() > 1); + return ( a.getItemDamage() > 1 ) == ( b.getItemDamage() > 1 ); } else { float APercentDamaged = (float) a.getItemDamage() / (float) a.getMaxDamage(); float BPercentDamaged = (float) b.getItemDamage() / (float) b.getMaxDamage(); - return (APercentDamaged > Mode.breakPoint) == (BPercentDamaged > Mode.breakPoint); + return ( APercentDamaged > Mode.breakPoint ) == ( BPercentDamaged > Mode.breakPoint ); } } } @@ -473,52 +301,52 @@ public final class AEItemStack extends AEStack implements IAEItemS } } - if ( st instanceof ItemStack ) + if( st instanceof ItemStack ) { ItemStack o = (ItemStack) st; OreHelper.INSTANCE.sameOre( this, o ); - if ( o.getItem() == this.getItem() ) + if( o.getItem() == this.getItem() ) { - if ( this.def.item.isDamageable() ) + if( this.def.item.isDamageable() ) { ItemStack a = this.getItemStack(); try { - if ( Mode == FuzzyMode.IGNORE_ALL ) + if( Mode == FuzzyMode.IGNORE_ALL ) { return true; } - else if ( Mode == FuzzyMode.PERCENT_99 ) + else if( Mode == FuzzyMode.PERCENT_99 ) { - return (a.getItemDamageForDisplay() > 1) == (o.getItemDamageForDisplay() > 1); + return ( a.getItemDamageForDisplay() > 1 ) == ( o.getItemDamageForDisplay() > 1 ); } else { float APercentDamaged = 1.0f - (float) a.getItemDamageForDisplay() / (float) a.getMaxDamage(); float BPercentDamaged = 1.0f - (float) o.getItemDamageForDisplay() / (float) o.getMaxDamage(); - return (APercentDamaged > Mode.breakPoint) == (BPercentDamaged > Mode.breakPoint); + return ( APercentDamaged > Mode.breakPoint ) == ( BPercentDamaged > Mode.breakPoint ); } } - catch (Throwable e) + catch( Throwable e ) { - if ( Mode == FuzzyMode.IGNORE_ALL ) + if( Mode == FuzzyMode.IGNORE_ALL ) { return true; } - else if ( Mode == FuzzyMode.PERCENT_99 ) + else if( Mode == FuzzyMode.PERCENT_99 ) { - return (a.getItemDamage() > 1) == (o.getItemDamage() > 1); + return ( a.getItemDamage() > 1 ) == ( o.getItemDamage() > 1 ); } else { float APercentDamaged = (float) a.getItemDamage() / (float) a.getMaxDamage(); float BPercentDamaged = (float) o.getItemDamage() / (float) o.getMaxDamage(); - return (APercentDamaged > Mode.breakPoint) == (BPercentDamaged > Mode.breakPoint); + return ( APercentDamaged > Mode.breakPoint ) == ( BPercentDamaged > Mode.breakPoint ); } } } @@ -530,83 +358,18 @@ public final class AEItemStack extends AEStack implements IAEItemS return false; } - public IAEItemStack getLow(FuzzyMode fuzzy, boolean ignoreMeta) + @Override + public IAEItemStack copy() { - AEItemStack bottom = new AEItemStack( this ); - AEItemDef newDef = bottom.def = bottom.def.copy(); - - if ( ignoreMeta ) - { - newDef.displayDamage = newDef.damageValue = 0; - newDef.reHash(); - return bottom; - } - - if ( newDef.item.isDamageable() ) - { - if ( fuzzy == FuzzyMode.IGNORE_ALL ) - { - newDef.displayDamage = 0; - } - else if ( fuzzy == FuzzyMode.PERCENT_99 ) - { - if ( this.def.damageValue == 0 ) - newDef.displayDamage = 0; - else - newDef.displayDamage = 1; - - } - else - { - int breakpoint = fuzzy.calculateBreakPoint( this.def.maxDamage ); - newDef.displayDamage = breakpoint <= this.def.displayDamage ? breakpoint : 0; - } - - newDef.damageValue = newDef.displayDamage; - } - - newDef.tagCompound = AEItemDef.LOW_TAG; - newDef.reHash(); - return bottom; + return new AEItemStack( this ); } - public IAEItemStack getHigh(FuzzyMode fuzzy, boolean ignoreMeta) + @Override + public IAEItemStack empty() { - AEItemStack top = new AEItemStack( this ); - AEItemDef newDef = top.def = top.def.copy(); - - if ( ignoreMeta ) - { - newDef.displayDamage = newDef.damageValue = Integer.MAX_VALUE; - newDef.reHash(); - return top; - } - - if ( newDef.item.isDamageable() ) - { - if ( fuzzy == FuzzyMode.IGNORE_ALL ) - { - newDef.displayDamage = this.def.maxDamage + 1; - } - else if ( fuzzy == FuzzyMode.PERCENT_99 ) - { - if ( this.def.damageValue == 0 ) - newDef.displayDamage = 0; - else - newDef.displayDamage = this.def.maxDamage + 1; - } - else - { - int breakpoint = fuzzy.calculateBreakPoint( this.def.maxDamage ); - newDef.displayDamage = this.def.displayDamage < breakpoint ? breakpoint - 1 : this.def.maxDamage + 1; - } - - newDef.damageValue = newDef.displayDamage; - } - - newDef.tagCompound = AEItemDef.HIGH_TAG; - newDef.reHash(); - return top; + IAEItemStack dup = this.copy(); + dup.reset(); + return dup; } @Override @@ -615,24 +378,6 @@ public final class AEItemStack extends AEStack implements IAEItemS return this.def.tagCompound; } - @Override - public boolean isSameType(IAEItemStack otherStack) - { - if ( otherStack == null ) - return false; - - return this.def.equals( ((AEItemStack) otherStack).def ); - } - - @Override - public boolean isSameType(ItemStack otherStack) - { - if ( otherStack == null ) - return false; - - return this.def.isItem( otherStack ); - } - @Override public boolean isItem() { @@ -651,9 +396,274 @@ public final class AEItemStack extends AEStack implements IAEItemS return StorageChannel.ITEMS; } + @Override + public int hashCode() + { + return this.def.myHash; + } + + @Override + public boolean equals( Object ia ) + { + if( ia instanceof AEItemStack ) + { + return ( (AEItemStack) ia ).def.equals( this.def );// && def.tagCompound == ((AEItemStack) ia).def.tagCompound; + } + else if( ia instanceof ItemStack ) + { + ItemStack is = (ItemStack) ia; + + if( is.getItem() == this.def.item && is.getItemDamage() == this.def.damageValue ) + { + NBTTagCompound ta = this.def.tagCompound; + NBTTagCompound tb = is.getTagCompound(); + if( ta == tb ) + return true; + + if( ( ta == null && tb == null ) || ( ta != null && ta.hasNoTags() && tb == null ) || ( tb != null && tb.hasNoTags() && ta == null ) || ( ta != null && ta.hasNoTags() && tb != null && tb.hasNoTags() ) ) + return true; + + if( ( ta == null && tb != null ) || ( ta != null && tb == null ) ) + return false; + + if( AESharedNBT.isShared( tb ) ) + return ta == tb; + + return Platform.NBTEqualityTest( ta, tb ); + } + } + return false; + } + + @Override + public String toString() + { + return this.getItemStack().toString(); + } + + @Override + public ItemStack getItemStack() + { + ItemStack is = new ItemStack( this.def.item, (int) Math.min( Integer.MAX_VALUE, this.stackSize ), this.def.damageValue ); + if( this.def.tagCompound != null ) + is.setTagCompound( this.def.tagCompound.getNBTTagCompoundCopy() ); + + return is; + } + + @Override + public Item getItem() + { + return this.def.item; + } + + @Override + public int getItemDamage() + { + return this.def.damageValue; + } + + @Override + public boolean sameOre( IAEItemStack is ) + { + return OreHelper.INSTANCE.sameOre( this, is ); + } + + @Override + public boolean isSameType( IAEItemStack otherStack ) + { + if( otherStack == null ) + return false; + + return this.def.equals( ( (AEItemStack) otherStack ).def ); + } + + @Override + public boolean isSameType( ItemStack otherStack ) + { + if( otherStack == null ) + return false; + + return this.def.isItem( otherStack ); + } + + @Override + public int compareTo( AEItemStack b ) + { + int id = this.def.itemID - b.def.itemID; + if( id != 0 ) + return id; + + int damageValue = this.def.damageValue - b.def.damageValue; + if( damageValue != 0 ) + return damageValue; + + int displayDamage = this.def.displayDamage - b.def.displayDamage; + if( displayDamage != 0 ) + return displayDamage; + + return ( this.def.tagCompound == b.def.tagCompound ) ? 0 : this.compareNBT( b.def ); + } + + private int compareNBT( AEItemDef b ) + { + int nbt = this.compare( ( this.def.tagCompound == null ? 0 : this.def.tagCompound.getHash() ), ( b.tagCompound == null ? 0 : b.tagCompound.getHash() ) ); + if( nbt == 0 ) + return this.compare( System.identityHashCode( this.def.tagCompound ), System.identityHashCode( b.tagCompound ) ); + return nbt; + } + + private int compare( int l, int m ) + { + return l < m ? -1 : ( l > m ? 1 : 0 ); + } + + @SideOnly( Side.CLIENT ) + public List getToolTip() + { + if( this.def.tooltip != null ) + return this.def.tooltip; + + return this.def.tooltip = Platform.getTooltip( this.getItemStack() ); + } + + @SideOnly( Side.CLIENT ) + public String getDisplayName() + { + if( this.def.displayName != null ) + return this.def.displayName; + + return this.def.displayName = Platform.getItemDisplayName( this.getItemStack() ); + } + + @SideOnly( Side.CLIENT ) + public String getModID() + { + if( this.def.uniqueID != null ) + return this.getModName( this.def.uniqueID ); + + return this.getModName( this.def.uniqueID = GameRegistry.findUniqueIdentifierFor( this.def.item ) ); + } + + private String getModName( UniqueIdentifier uniqueIdentifier ) + { + if( uniqueIdentifier == null ) + return "** Null"; + + return uniqueIdentifier.modId == null ? "** Null" : uniqueIdentifier.modId; + } + + @Override + void writeIdentity( ByteBuf i ) throws IOException + { + i.writeShort( Item.itemRegistry.getIDForObject( this.def.item ) ); + i.writeShort( this.getItemDamage() ); + } + + @Override + void readNBT( ByteBuf i ) throws IOException + { + if( this.hasTagCompound() ) + { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + DataOutputStream data = new DataOutputStream( bytes ); + + CompressedStreamTools.write( (NBTTagCompound) this.getTagCompound(), data ); + + byte[] tagBytes = bytes.toByteArray(); + int size = tagBytes.length; + + i.writeInt( size ); + i.writeBytes( tagBytes ); + } + } + + @Override + public boolean hasTagCompound() + { + return this.def.tagCompound != null; + } + + public IAEItemStack getLow( FuzzyMode fuzzy, boolean ignoreMeta ) + { + AEItemStack bottom = new AEItemStack( this ); + AEItemDef newDef = bottom.def = bottom.def.copy(); + + if( ignoreMeta ) + { + newDef.displayDamage = newDef.damageValue = 0; + newDef.reHash(); + return bottom; + } + + if( newDef.item.isDamageable() ) + { + if( fuzzy == FuzzyMode.IGNORE_ALL ) + { + newDef.displayDamage = 0; + } + else if( fuzzy == FuzzyMode.PERCENT_99 ) + { + if( this.def.damageValue == 0 ) + newDef.displayDamage = 0; + else + newDef.displayDamage = 1; + } + else + { + int breakpoint = fuzzy.calculateBreakPoint( this.def.maxDamage ); + newDef.displayDamage = breakpoint <= this.def.displayDamage ? breakpoint : 0; + } + + newDef.damageValue = newDef.displayDamage; + } + + newDef.tagCompound = AEItemDef.LOW_TAG; + newDef.reHash(); + return bottom; + } + + public IAEItemStack getHigh( FuzzyMode fuzzy, boolean ignoreMeta ) + { + AEItemStack top = new AEItemStack( this ); + AEItemDef newDef = top.def = top.def.copy(); + + if( ignoreMeta ) + { + newDef.displayDamage = newDef.damageValue = Integer.MAX_VALUE; + newDef.reHash(); + return top; + } + + if( newDef.item.isDamageable() ) + { + if( fuzzy == FuzzyMode.IGNORE_ALL ) + { + newDef.displayDamage = this.def.maxDamage + 1; + } + else if( fuzzy == FuzzyMode.PERCENT_99 ) + { + if( this.def.damageValue == 0 ) + newDef.displayDamage = 0; + else + newDef.displayDamage = this.def.maxDamage + 1; + } + else + { + int breakpoint = fuzzy.calculateBreakPoint( this.def.maxDamage ); + newDef.displayDamage = this.def.displayDamage < breakpoint ? breakpoint - 1 : this.def.maxDamage + 1; + } + + newDef.damageValue = newDef.displayDamage; + } + + newDef.tagCompound = AEItemDef.HIGH_TAG; + newDef.reHash(); + return top; + } + public boolean isOre() { return this.def.isOre != null; } - }