GT5-Unofficial/src/main/java/gregtech/api/util/GT_BaseCrop.java

218 lines
8.1 KiB
Java
Raw Normal View History

2015-04-23 16:14:22 +00:00
package gregtech.api.util;
import static gregtech.api.enums.GT_Values.E;
2015-10-22 00:47:13 +00:00
import gregtech.api.GregTech_API;
2015-04-23 16:14:22 +00:00
import gregtech.api.enums.ConfigCategories;
2015-10-22 00:47:13 +00:00
import gregtech.api.enums.Materials;
import gregtech.api.enums.OrePrefixes;
import gregtech.api.objects.ItemData;
import gregtech.common.blocks.GT_Block_Ores;
import gregtech.common.blocks.GT_TileEntity_Ores;
2015-04-23 16:14:22 +00:00
import ic2.api.crops.CropCard;
import ic2.api.crops.Crops;
import ic2.api.crops.ICropTile;
import java.util.ArrayList;
import java.util.Random;
2015-10-22 00:47:13 +00:00
import net.minecraft.block.Block;
2015-04-23 16:14:22 +00:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
2015-10-22 00:47:13 +00:00
import net.minecraft.tileentity.TileEntity;
2015-04-23 16:14:22 +00:00
public class GT_BaseCrop extends CropCard {
private String mName = E, mDiscoveredBy = "Gregorius Techneticies", mAttributes[];
2015-10-22 00:47:13 +00:00
private int mTier = 0, mMaxSize = 0, mAfterHarvestSize = 0, mHarvestSize = 0, mStats[] = new int[5], mGrowthSpeed = 0;
2015-04-23 16:14:22 +00:00
private ItemStack mDrop = null, mSpecialDrops[] = null;
2015-10-22 00:47:13 +00:00
private Materials mBlock = null;
2015-04-23 16:14:22 +00:00
public static ArrayList<GT_BaseCrop> sCropList = new ArrayList<GT_BaseCrop>();
/**
* To create new Crops
* @param aID Default ID
* @param aCropName Name of the Crop
* @param aDiscoveredBy The one who discovered the Crop
* @param aDrop The Item which is dropped by the Crop. must be != null
* @param aBaseSeed Baseseed to plant this Crop. null == crossbreed only
* @param aTier tier of the Crop. forced to be >= 1
* @param aMaxSize maximum Size of the Crop. forced to be >= 3
* @param aGrowthSpeed how fast the Crop grows. if < 0 then its set to Tier*300
* @param aHarvestSize the size the Crop needs to be harvested. forced to be between 2 and max size
*/
public GT_BaseCrop(int aID, String aCropName, String aDiscoveredBy, ItemStack aDrop, ItemStack[] aSpecialDrops, ItemStack aBaseSeed, int aTier, int aMaxSize, int aGrowthSpeed, int aAfterHarvestSize, int aHarvestSize, int aStatChemical, int aStatFood, int aStatDefensive, int aStatColor, int aStatWeed, String[] aAttributes) {
2015-10-22 00:47:13 +00:00
new GT_BaseCrop( aID, aCropName, aDiscoveredBy, aDrop, aSpecialDrops, aBaseSeed, aTier, aMaxSize, aGrowthSpeed, aAfterHarvestSize, aHarvestSize, aStatChemical, aStatFood, aStatDefensive, aStatColor, aStatWeed, aAttributes, null);
}
/**
* To create new Crops
* @param aID Default ID
* @param aCropName Name of the Crop
* @param aDiscoveredBy The one who discovered the Crop
* @param aDrop The Item which is dropped by the Crop. must be != null
* @param aBaseSeed Baseseed to plant this Crop. null == crossbreed only
* @param aTier tier of the Crop. forced to be >= 1
* @param aMaxSize maximum Size of the Crop. forced to be >= 3
* @param aGrowthSpeed how fast the Crop grows. if < 0 then its set to Tier*300
* @param aHarvestSize the size the Crop needs to be harvested. forced to be between 2 and max size
* @param aBlock the block below needed for crop to grow. If null no block needed
* @param aMeta meta of the block below(-1 if no meta)
*/
public GT_BaseCrop(int aID, String aCropName, String aDiscoveredBy, ItemStack aDrop, ItemStack[] aSpecialDrops, ItemStack aBaseSeed, int aTier, int aMaxSize, int aGrowthSpeed, int aAfterHarvestSize, int aHarvestSize, int aStatChemical, int aStatFood, int aStatDefensive, int aStatColor, int aStatWeed, String[] aAttributes, Materials aBlock) {
2015-04-23 16:14:22 +00:00
mName = aCropName;
aID = GT_Config.addIDConfig(ConfigCategories.IDs.crops, mName.replaceAll(" ", "_"), aID);
if (aDiscoveredBy != null && !aDiscoveredBy.equals(E)) mDiscoveredBy = aDiscoveredBy;
if (aDrop != null && aID > 0 && aID < 256) {
mDrop = GT_Utility.copy(aDrop);
mSpecialDrops = aSpecialDrops;
mTier = Math.max(1, aTier);
mMaxSize = Math.max(3, aMaxSize);
mHarvestSize = Math.min(Math.max(aHarvestSize, 2), mMaxSize);
mAfterHarvestSize = Math.min(Math.max(aAfterHarvestSize, 1), mMaxSize-1);
mStats[0] = aStatChemical;
mStats[1] = aStatFood;
mStats[2] = aStatDefensive;
mStats[3] = aStatColor;
mStats[4] = aStatWeed;
mAttributes = aAttributes;
2015-10-22 00:47:13 +00:00
mBlock = aBlock;
2015-04-23 16:14:22 +00:00
if (!Crops.instance.registerCrop(this, aID)) throw new GT_ItsNotMyFaultException("Make sure the Crop ID is valid!");
2015-10-22 00:47:13 +00:00
if (aBaseSeed != null) Crops.instance.registerBaseSeed(aBaseSeed, this, 1, 1, 1, 1);
2015-04-23 16:14:22 +00:00
sCropList.add(this);
}
}
@Override
public byte getSizeAfterHarvest(ICropTile crop) {
return (byte)mAfterHarvestSize;
}
2015-10-22 00:47:13 +00:00
@Override
public int growthDuration(ICropTile aCrop){
if(mGrowthSpeed<200)return super.growthDuration(aCrop);
return tier() * mGrowthSpeed;
}
public int getrootslength(ICropTile crop) {
return 3;
}
2015-04-23 16:14:22 +00:00
@Override
public String[] attributes() {
return mAttributes;
}
@Override
public String discoveredBy() {
return mDiscoveredBy;
}
@Override
public final boolean canGrow(ICropTile aCrop) {
2015-10-22 00:47:13 +00:00
if(mBlock!=null&&aCrop.getSize()==mMaxSize-1){
return isBlockBelow(aCrop);
}
2015-04-23 16:14:22 +00:00
return aCrop.getSize() < maxSize();
}
@Override
public final boolean canBeHarvested(ICropTile aCrop) {
return aCrop.getSize() >= mHarvestSize;
}
@Override
public boolean canCross(ICropTile aCrop) {
return aCrop.getSize() + 2 > maxSize();
}
@Override
public int stat(int n) {
if (n < 0 || n >= mStats.length) return 0;
return mStats[n];
}
@Override
public String name() {
return mName;
}
@Override
public int tier() {
return mTier;
}
@Override
public int maxSize() {
return mMaxSize;
}
@Override
public ItemStack getGain(ICropTile aCrop) {
int tDrop = 0;
if (mSpecialDrops != null && (tDrop = new Random().nextInt(mSpecialDrops.length+4)) < mSpecialDrops.length && mSpecialDrops[tDrop] != null) {
return GT_Utility.copy(mSpecialDrops[tDrop]);
}
return GT_Utility.copy(mDrop);
}
@Override
public boolean rightclick(ICropTile aCrop, EntityPlayer aPlayer) {
if (!canBeHarvested(aCrop)) return false;
return aCrop.harvest(aPlayer==null?false:aPlayer instanceof EntityPlayerMP);
}
@Override
public int getOptimalHavestSize(ICropTile crop) {
return maxSize();
}
2015-10-22 00:47:13 +00:00
public boolean isBlockBelow(ICropTile aCrop)
{
if (aCrop == null) {
return false;
}
for (int i = 1; i < this.getrootslength(aCrop); i++)
{
Block tBlock = aCrop.getWorld().getBlock(aCrop.getLocation().posX, aCrop.getLocation().posY - i, aCrop.getLocation().posZ);
if ((tBlock instanceof GT_Block_Ores))
{
TileEntity tTileEntity = aCrop.getWorld().getTileEntity(aCrop.getLocation().posX, aCrop.getLocation().posY - i, aCrop.getLocation().posZ);
if ((tTileEntity instanceof GT_TileEntity_Ores))
{
Materials tMaterial = GregTech_API.sGeneratedMaterials[(((GT_TileEntity_Ores)tTileEntity).mMetaData % 1000)];
if ((tMaterial != null) && (tMaterial != Materials._NULL))
{
if(tMaterial==mBlock){return true;}else{return false;}
}
}
}
else
{
int tMetaID = aCrop.getWorld().getBlockMetadata(aCrop.getLocation().posX, aCrop.getLocation().posY - i, aCrop.getLocation().posZ);
ItemData tAssotiation = GT_OreDictUnificator.getAssociation(new ItemStack(tBlock, 1, tMetaID));
if ((tAssotiation != null) && (tAssotiation.mPrefix.toString().startsWith("ore"))&&(tAssotiation.mMaterial.mMaterial==mBlock))
{
return true;
}
if ((tAssotiation != null) && (tAssotiation.mPrefix==OrePrefixes.block)&&(tAssotiation.mMaterial.mMaterial==mBlock))
{
return true;
}
}
// Block block = aCrop.getWorld().getBlock(aCrop.getLocation().posX, aCrop.getLocation().posY - i, aCrop.getLocation().posZ);
// if (block.isAir(aCrop.getWorld(), aCrop.getLocation().posX, aCrop.getLocation().posY - i, aCrop.getLocation().posZ)) {
// return false;
// }
// if (block == mBlock) {
// int tMeta = aCrop.getWorld().getBlockMetadata(aCrop.getLocation().posX, aCrop.getLocation().posY - i, aCrop.getLocation().posZ);
// if(mMeta < 0 || tMeta == mMeta){
// return true;}
// }
}
return false;
}
2015-04-23 16:14:22 +00:00
}