GT5-Unofficial/src/main/java/gregtech/common/blocks/GT_Item_Machines.java

159 lines
8.7 KiB
Java
Raw Normal View History

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;
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;
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;
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);
}
@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 {
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);
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) {
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) {
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) {
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
}
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
@Override
public EnumActionResult onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
return EnumActionResult.PASS;
2015-10-22 02:06:25 +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
@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
@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);
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();
if (!world.setBlockState(pos, block.getStateFromMeta(tMetaData))) {
2015-10-22 02:06:25 +00:00
return false;
}
IBlockState placed = world.getBlockState(pos);
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
}
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.");
}
IGregTechTileEntity tTileEntity = (IGregTechTileEntity) world.getTileEntity(pos);
2015-10-22 02:06:25 +00:00
if (tTileEntity != null) {
tTileEntity.setInitialValuesAsNBT(tTileEntity.isServerSide() ? stack.getTagCompound() : null, tDamage);
if (player != null) {
tTileEntity.setOwnerName(player.getName());
2015-10-22 02:06:25 +00:00
}
tTileEntity.getMetaTileEntity().initDefaultModes(stack.getTagCompound());
2015-10-22 02:06:25 +00:00
}
} else if (!world.setBlockState(pos, block.getStateFromMeta(tDamage))) {
2015-10-22 02:06:25 +00:00
return false;
}
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
}
2015-06-23 22:29:05 +00:00
}