2015-06-23 22:29:05 +00:00
package gregtech.common.blocks ;
import gregtech.api.GregTech_API ;
import gregtech.api.enums.GT_Values ;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity ;
2016-08-10 19:43:37 +00:00
import gregtech.api.items.GT_Generic_Block ;
2015-06-23 22:29:05 +00:00
import gregtech.api.util.GT_ItsNotMyFaultException ;
import gregtech.api.util.GT_LanguageManager ;
import gregtech.api.util.GT_Log ;
import gregtech.api.util.GT_Utility ;
import net.minecraft.block.Block ;
2016-08-10 19:43:37 +00:00
import net.minecraft.block.state.IBlockState ;
2015-06-23 22:29:05 +00:00
import net.minecraft.entity.player.EntityPlayer ;
import net.minecraft.item.ItemBlock ;
import net.minecraft.item.ItemStack ;
import net.minecraft.nbt.NBTTagCompound ;
import net.minecraft.tileentity.TileEntity ;
2016-08-10 19:43:37 +00:00
import net.minecraft.util.EnumActionResult ;
import net.minecraft.util.EnumFacing ;
import net.minecraft.util.EnumHand ;
import net.minecraft.util.math.BlockPos ;
import net.minecraft.util.text.TextFormatting ;
2015-06-23 22:29:05 +00:00
import net.minecraft.world.World ;
2015-10-22 02:06:25 +00:00
import java.util.List ;
2015-06-23 22:29:05 +00:00
public class GT_Item_Machines
2015-10-22 02:06:25 +00:00
extends ItemBlock {
public GT_Item_Machines ( Block par1 ) {
super ( par1 ) ;
setMaxDamage ( 0 ) ;
setHasSubtypes ( true ) ;
setCreativeTab ( GregTech_API . TAB_GREGTECH ) ;
}
2016-08-10 19:43:37 +00:00
@Override
public void addInformation ( ItemStack aStack , EntityPlayer aPlayer , List < String > aList , boolean par4 ) {
2015-10-22 02:06:25 +00:00
try {
int tDamage = getDamage ( aStack ) ;
if ( ( tDamage < = 0 ) | | ( tDamage > = GregTech_API . METATILEENTITIES . length ) ) {
return ;
}
if ( tDamage = = 0 ) {
aList . add ( " WARNING, THE EXISTENCE OF THIS ITEM IS A BUG " ) ;
aList . add ( " IF YOU GOT IT IN SURVIVAL THEN PLEASE REPORT IT " ) ;
} else {
2016-08-10 19:43:37 +00:00
TileEntity temp = GregTech_API . sBlockMachines . createNewTileEntity ( aPlayer = = null ? GT_Values . DW : aPlayer . worldObj , GregTech_API . METATILEENTITIES [ tDamage ] = = null ? 0 : GregTech_API . METATILEENTITIES [ tDamage ] . getTileEntityBaseType ( ) ) ;
2015-10-22 02:06:25 +00:00
if ( temp ! = null ) {
temp . setWorldObj ( aPlayer = = null ? GT_Values . DW : aPlayer . worldObj ) ;
2016-08-10 19:43:37 +00:00
temp . setPos ( BlockPos . ORIGIN ) ;
2015-10-22 02:06:25 +00:00
if ( ( temp instanceof IGregTechTileEntity ) ) {
IGregTechTileEntity tTileEntity = ( IGregTechTileEntity ) temp ;
tTileEntity . setInitialValuesAsNBT ( new NBTTagCompound ( ) , ( short ) tDamage ) ;
if ( tTileEntity . getDescription ( ) ! = null ) {
int i = 0 ;
for ( String tDescription : tTileEntity . getDescription ( ) ) {
if ( GT_Utility . isStringValid ( tDescription ) ) {
aList . add ( GT_LanguageManager . addStringLocalization ( " TileEntity_DESCRIPTION_ " + tDamage + " _Index_ " + i + + , tDescription , ! GregTech_API . sPostloadFinished ) ) ;
}
}
}
if ( tTileEntity . getEUCapacity ( ) > 0L ) {
if ( tTileEntity . getInputVoltage ( ) > 0L ) {
2016-08-10 19:43:37 +00:00
aList . add ( GT_LanguageManager . addStringLocalization ( " TileEntity_EUp_IN " , " Voltage IN: " , ! GregTech_API . sPostloadFinished ) + TextFormatting . GREEN + tTileEntity . getInputVoltage ( ) + " ( " + GT_Values . VN [ GT_Utility . getTier ( tTileEntity . getInputVoltage ( ) ) ] + " ) " + TextFormatting . GRAY ) ;
2015-10-22 02:06:25 +00:00
}
if ( tTileEntity . getOutputVoltage ( ) > 0L ) {
2016-08-10 19:43:37 +00:00
aList . add ( GT_LanguageManager . addStringLocalization ( " TileEntity_EUp_OUT " , " Voltage OUT: " , ! GregTech_API . sPostloadFinished ) + TextFormatting . GREEN + tTileEntity . getOutputVoltage ( ) + " ( " + GT_Values . VN [ GT_Utility . getTier ( tTileEntity . getOutputVoltage ( ) ) ] + " ) " + TextFormatting . GRAY ) ;
2015-10-22 02:06:25 +00:00
}
if ( tTileEntity . getOutputAmperage ( ) > 1L ) {
2016-08-10 19:43:37 +00:00
aList . add ( GT_LanguageManager . addStringLocalization ( " TileEntity_EUp_AMOUNT " , " Amperage: " , ! GregTech_API . sPostloadFinished ) + TextFormatting . YELLOW + tTileEntity . getOutputAmperage ( ) + TextFormatting . GRAY ) ;
2015-10-22 02:06:25 +00:00
}
2016-08-10 19:43:37 +00:00
aList . add ( GT_LanguageManager . addStringLocalization ( " TileEntity_EUp_STORE " , " Capacity: " , ! GregTech_API . sPostloadFinished ) + TextFormatting . BLUE + tTileEntity . getEUCapacity ( ) + TextFormatting . GRAY ) ;
2015-10-22 02:06:25 +00:00
}
}
2015-06-23 22:29:05 +00:00
}
}
2015-10-22 02:06:25 +00:00
NBTTagCompound aNBT = aStack . getTagCompound ( ) ;
if ( aNBT ! = null ) {
if ( aNBT . getBoolean ( " mMuffler " ) ) {
aList . add ( GT_LanguageManager . addStringLocalization ( " GT_TileEntity_MUFFLER " , " has Muffler Upgrade " , ! GregTech_API . sPostloadFinished ) ) ;
}
if ( aNBT . getBoolean ( " mSteamConverter " ) ) {
aList . add ( GT_LanguageManager . addStringLocalization ( " GT_TileEntity_STEAMCONVERTER " , " has Steam Upgrade " , ! GregTech_API . sPostloadFinished ) ) ;
}
int tAmount = 0 ;
if ( ( tAmount = aNBT . getByte ( " mSteamTanks " ) ) > 0 ) {
aList . add ( tAmount + " " + GT_LanguageManager . addStringLocalization ( " GT_TileEntity_STEAMTANKS " , " Steam Tank Upgrades " , ! GregTech_API . sPostloadFinished ) ) ;
}
2015-06-23 22:29:05 +00:00
}
2015-10-22 02:06:25 +00:00
} catch ( Throwable e ) {
e . printStackTrace ( GT_Log . err ) ;
2015-06-23 22:29:05 +00:00
}
}
2015-10-22 02:06:25 +00:00
2016-08-10 19:43:37 +00:00
@Override
public EnumActionResult onItemUseFirst ( ItemStack stack , EntityPlayer player , World world , BlockPos pos , EnumFacing side , float hitX , float hitY , float hitZ , EnumHand hand ) {
2016-09-06 09:53:19 +00:00
return EnumActionResult . PASS ;
2015-10-22 02:06:25 +00:00
}
2016-08-10 19:43:37 +00:00
@Override
2015-10-22 02:06:25 +00:00
public String getUnlocalizedName ( ItemStack aStack ) {
short tDamage = ( short ) getDamage ( aStack ) ;
if ( ( tDamage < 0 ) | | ( tDamage > = GregTech_API . METATILEENTITIES . length ) ) {
return " " ;
2015-06-23 22:29:05 +00:00
}
2015-10-22 02:06:25 +00:00
if ( GregTech_API . METATILEENTITIES [ tDamage ] ! = null ) {
return getUnlocalizedName ( ) + " . " + GregTech_API . METATILEENTITIES [ tDamage ] . getMetaName ( ) ;
}
return " " ;
2015-06-23 22:29:05 +00:00
}
2015-10-22 02:06:25 +00:00
2016-08-10 19:43:37 +00:00
@Override
2015-10-22 02:06:25 +00:00
public void onCreated ( ItemStack aStack , World aWorld , EntityPlayer aPlayer ) {
super . onCreated ( aStack , aWorld , aPlayer ) ;
short tDamage = ( short ) getDamage ( aStack ) ;
if ( ( tDamage < 0 ) | | ( ( tDamage > = GregTech_API . METATILEENTITIES . length ) & & ( GregTech_API . METATILEENTITIES [ tDamage ] ! = null ) ) ) {
GregTech_API . METATILEENTITIES [ tDamage ] . onCreated ( aStack , aWorld , aPlayer ) ;
}
2015-06-23 22:29:05 +00:00
}
2015-10-22 02:06:25 +00:00
2016-08-10 19:43:37 +00:00
@Override
public boolean placeBlockAt ( ItemStack stack , EntityPlayer player , World world , BlockPos pos , EnumFacing side , float hitX , float hitY , float hitZ , IBlockState newState ) {
short tDamage = ( short ) getDamage ( stack ) ;
2016-09-06 09:53:19 +00:00
System . out . println ( " Place block " + block + " " + tDamage ) ;
2015-10-22 02:06:25 +00:00
if ( tDamage > 0 ) {
if ( GregTech_API . METATILEENTITIES [ tDamage ] = = null ) {
return false ;
}
int tMetaData = GregTech_API . METATILEENTITIES [ tDamage ] . getTileEntityBaseType ( ) ;
2016-09-06 09:53:19 +00:00
if ( ! world . setBlockState ( pos , block . getStateFromMeta ( tMetaData ) ) ) {
2015-10-22 02:06:25 +00:00
return false ;
}
2016-08-10 19:43:37 +00:00
IBlockState placed = world . getBlockState ( pos ) ;
2016-09-06 09:53:19 +00:00
if ( placed ! = block . getStateFromMeta ( tMetaData ) ) {
throw new GT_ItsNotMyFaultException ( " Failed to place Block even though World.setBlockState returned true. It COULD be MCPC/Bukkit causing that. In case you really have that installed, don't report this Bug to me, I don't know how to fix it. " ) ;
2015-10-22 02:06:25 +00:00
}
2016-08-10 19:43:37 +00:00
if ( placed . getValue ( GT_Generic_Block . METADATA ) ! = tMetaData ) {
2015-10-22 02:06:25 +00:00
throw new GT_ItsNotMyFaultException ( " Failed to set the MetaValue of the Block even though World.setBlock returned true. It COULD be MCPC/Bukkit causing that. In case you really have that installed, don't report this Bug to me, I don't know how to fix it. " ) ;
}
2016-08-10 19:43:37 +00:00
IGregTechTileEntity tTileEntity = ( IGregTechTileEntity ) world . getTileEntity ( pos ) ;
2015-10-22 02:06:25 +00:00
if ( tTileEntity ! = null ) {
2016-08-10 19:43:37 +00:00
tTileEntity . setInitialValuesAsNBT ( tTileEntity . isServerSide ( ) ? stack . getTagCompound ( ) : null , tDamage ) ;
if ( player ! = null ) {
tTileEntity . setOwnerName ( player . getName ( ) ) ;
2015-10-22 02:06:25 +00:00
}
2016-08-10 19:43:37 +00:00
tTileEntity . getMetaTileEntity ( ) . initDefaultModes ( stack . getTagCompound ( ) ) ;
2015-10-22 02:06:25 +00:00
}
2016-08-10 19:43:37 +00:00
} else if ( ! world . setBlockState ( pos , block . getStateFromMeta ( tDamage ) ) ) {
2015-10-22 02:06:25 +00:00
return false ;
}
2016-08-10 19:43:37 +00:00
IBlockState state = world . getBlockState ( pos ) ;
if ( state . getBlock ( ) = = block ) {
this . block . onBlockPlacedBy ( world , pos , state , player , stack ) ;
2015-10-22 02:06:25 +00:00
}
return true ;
2015-06-23 22:29:05 +00:00
}
2016-08-10 19:43:37 +00:00
2015-06-23 22:29:05 +00:00
}