ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/base/BlockPlant.java

139 lines
4.3 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("BlockPlant.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks.base;
2015-05-20 22:39:43 +02:00
2016-01-08 20:51:03 +01:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2016-03-18 18:38:39 +01:00
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
2015-05-20 22:39:43 +02:00
import net.minecraft.block.BlockCrops;
import net.minecraft.block.state.IBlockState;
2016-05-03 18:29:02 +02:00
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
2015-05-20 22:39:43 +02:00
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
2015-06-28 21:28:58 +02:00
import net.minecraft.world.IBlockAccess;
2015-05-20 22:39:43 +02:00
import net.minecraft.world.World;
2015-06-28 21:28:58 +02:00
import net.minecraftforge.common.EnumPlantType;
2015-05-20 22:39:43 +02:00
2016-05-19 20:05:12 +02:00
import javax.annotation.Nonnull;
2016-01-16 14:14:39 +01:00
import java.util.List;
import java.util.Random;
2015-12-19 10:30:39 +01:00
public class BlockPlant extends BlockCrops{
2015-05-20 22:39:43 +02:00
public Item seedItem;
2015-06-21 02:28:49 +02:00
public Item returnItem;
public int returnMeta;
2016-05-19 20:05:12 +02:00
private final String name;
private final int minDropAmount;
private final int addDropAmount;
2015-05-20 22:39:43 +02:00
public BlockPlant(String name, int minDropAmount, int addDropAmount){
2015-05-20 22:39:43 +02:00
this.name = name;
2015-06-06 01:25:53 +02:00
this.minDropAmount = minDropAmount;
this.addDropAmount = addDropAmount;
this.register();
}
2016-01-16 14:14:39 +01:00
private void register(){
2016-03-18 18:38:39 +01:00
ItemUtil.registerBlock(this, this.getItemBlock(), this.getBaseName(), this.shouldAddCreative());
2016-01-08 16:52:53 +01:00
2016-01-08 20:51:03 +01:00
this.registerRendering();
}
protected String getBaseName(){
return this.name;
}
2016-04-20 21:39:03 +02:00
protected ItemBlockBase getItemBlock(){
return new ItemBlockBase(this);
}
2015-12-19 10:30:39 +01:00
public boolean shouldAddCreative(){
return false;
2015-12-19 10:30:39 +01:00
}
2016-02-01 17:49:55 +01:00
protected void registerRendering(){
2016-05-03 18:29:02 +02:00
ActuallyAdditions.proxy.addRenderRegister(new ItemStack(this), new ModelResourceLocation(this.getRegistryName(), "inventory"));
2016-02-01 17:49:55 +01:00
}
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.RARE;
2015-05-20 22:39:43 +02:00
}
2016-05-19 20:05:12 +02:00
@Nonnull
2015-06-28 21:28:58 +02:00
@Override
public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos){
2015-06-28 21:28:58 +02:00
return EnumPlantType.Crop;
}
2015-05-20 22:39:43 +02:00
@Override
2016-02-01 17:49:55 +01:00
public int damageDropped(IBlockState state){
return this.getMetaFromState(state) >= 7 ? this.returnMeta : 0;
2015-05-20 22:39:43 +02:00
}
@Override
2016-03-18 23:47:22 +01:00
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack stack, EnumFacing facing, float hitX, float hitY, float hitZ){
2016-05-02 17:46:53 +02:00
if(this.getMetaFromState(state) >= 7){
2016-02-01 17:49:55 +01:00
if(!world.isRemote){
2016-01-16 14:14:39 +01:00
2016-05-02 17:46:53 +02:00
List<ItemStack> drops = this.getDrops(world, pos, state, 0);
2016-02-01 17:49:55 +01:00
boolean deductedSeedSize = false;
2016-03-18 23:47:22 +01:00
for(ItemStack drop : drops){
if(drop != null){
if(drop.getItem() == this.seedItem && !deductedSeedSize){
drop.stackSize--;
2016-02-01 17:49:55 +01:00
deductedSeedSize = true;
}
2015-05-20 22:39:43 +02:00
2016-03-18 23:47:22 +01:00
if(drop.stackSize > 0){
EntityItem entity = new EntityItem(world, pos.getX()+0.5, pos.getY()+0.5, pos.getZ()+0.5, drop);
2016-02-01 17:49:55 +01:00
world.spawnEntityInWorld(entity);
}
}
}
2016-05-02 17:46:53 +02:00
world.setBlockState(pos, this.getStateFromMeta(0));
2016-02-01 17:49:55 +01:00
}
return true;
}
return false;
2015-10-03 10:19:40 +02:00
}
2016-05-19 20:05:12 +02:00
@Nonnull
2016-02-01 20:39:11 +01:00
@Override
public Item getSeed(){
return this.seedItem;
2015-10-03 10:19:40 +02:00
}
@Override
2016-05-19 20:05:12 +02:00
public int quantityDropped(IBlockState state, int fortune, @Nonnull Random random){
2016-05-02 17:46:53 +02:00
return this.getMetaFromState(state) >= 7 ? random.nextInt(this.addDropAmount)+this.minDropAmount : super.quantityDropped(state, fortune, random);
2015-10-03 10:19:40 +02:00
}
2016-02-01 17:49:55 +01:00
2016-05-19 20:05:12 +02:00
@Nonnull
@Override
public Item getCrop(){
return this.returnItem;
}
2016-02-01 17:49:55 +01:00
@Override
2016-05-19 20:05:12 +02:00
public Item getItemDropped(@Nonnull IBlockState state, Random rand, int par3){
return this.getMetaFromState(state) >= 7 ? this.getCrop() : this.getSeed();
}
2016-02-01 17:49:55 +01:00
2015-05-20 22:39:43 +02:00
}