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

142 lines
4.5 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
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 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;
import de.ellpeck.actuallyadditions.mod.blocks.render.IHasModel;
2016-03-18 18:38:39 +01:00
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2015-05-20 22:39:43 +02:00
import net.minecraft.block.BlockCrops;
import net.minecraft.block.state.IBlockState;
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.NonNullList;
2016-03-18 23:47:22 +01:00
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-01-16 14:14:39 +01:00
import java.util.Random;
public class BlockPlant extends BlockCrops implements ItemBlockBase.ICustomRarity, IHasModel{
2015-05-20 22:39:43 +02:00
2016-05-19 20:05:12 +02:00
private final String name;
private final int minDropAmount;
private final int addDropAmount;
2016-06-01 00:39:35 +02:00
public Item seedItem;
private Item returnItem;
private int returnMeta;
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
public void doStuff(Item seedItem, Item returnItem, int returnMeta){
this.seedItem = seedItem;
this.returnItem = returnItem;
this.returnMeta = returnMeta;
}
private void register(){
2016-03-18 18:38:39 +01:00
ItemUtil.registerBlock(this, this.getItemBlock(), this.getBaseName(), this.shouldAddCreative());
2016-01-08 20:51:03 +01:00
}
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
}
2019-02-27 19:53:05 +01:00
@Override
public void registerRendering(){
2018-05-10 11:38:58 +02:00
ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this), this.getRegistryName(), "inventory");
2016-02-01 17:49:55 +01:00
}
2016-06-27 20:24:45 +02:00
@Override
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.RARE;
2015-05-20 22:39:43 +02:00
}
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-11-19 21:11:17 +01:00
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, 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
NonNullList<ItemStack> drops = NonNullList.create();
this.getDrops(drops, 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(StackUtil.isValid(drop)){
2016-03-18 23:47:22 +01:00
if(drop.getItem() == this.seedItem && !deductedSeedSize){
drop.shrink(1);
2016-02-01 17:49:55 +01:00
deductedSeedSize = true;
}
2015-05-20 22:39:43 +02:00
if(StackUtil.isValid(drop)){
2016-03-18 23:47:22 +01:00
EntityItem entity = new EntityItem(world, pos.getX()+0.5, pos.getY()+0.5, pos.getZ()+0.5, drop);
2016-11-26 21:32:27 +01:00
world.spawnEntity(entity);
2016-02-01 17:49:55 +01:00
}
}
}
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-02-01 20:39:11 +01:00
@Override
public Item getSeed(){
return this.seedItem;
2015-10-03 10:19:40 +02:00
}
@Override
public int quantityDropped(IBlockState state, int fortune, 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
@Override
public Item getCrop(){
return this.returnItem;
}
2016-02-01 17:49:55 +01:00
@Override
public Item getItemDropped(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
}