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

147 lines
4.5 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("BlockPlant.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 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-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.creative.CreativeTab;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
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.BlockPos;
import net.minecraft.util.EnumFacing;
2016-01-08 20:51:03 +01:00
import net.minecraft.util.ResourceLocation;
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;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.common.registry.GameRegistry;
2015-05-20 22:39:43 +02:00
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;
2015-10-03 10:16:18 +02:00
private String name;
2015-06-06 01:25:53 +02:00
private int minDropAmount;
private 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(){
this.setUnlocalizedName(ModUtil.MOD_ID_LOWER+"."+this.getBaseName());
GameRegistry.registerBlock(this, this.getItemBlock(), this.getBaseName());
if(this.shouldAddCreative()){
this.setCreativeTab(CreativeTab.instance);
}
else{
this.setCreativeTab(null);
}
2016-01-08 16:52:53 +01:00
2016-01-08 20:51:03 +01:00
this.registerRendering();
}
protected String getBaseName(){
return this.name;
}
protected Class<? extends ItemBlockBase> getItemBlock(){
return ItemBlockBase.class;
}
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(){
ActuallyAdditions.proxy.addRenderRegister(new ItemStack(this), new ResourceLocation(ModUtil.MOD_ID_LOWER, this.getBaseName()));
}
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-02-01 17:49:55 +01:00
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing facing, float hitX, float hitY, float hitZ){
if(getMetaFromState(state) >= 7){
if(!world.isRemote){
2016-01-16 14:14:39 +01:00
2016-02-01 17:49:55 +01:00
List<ItemStack> drops = getDrops(world, pos, state, 0);
boolean deductedSeedSize = false;
for(ItemStack stack : drops){
if(stack != null){
if(stack.getItem() == this.seedItem && !deductedSeedSize){
stack.stackSize--;
deductedSeedSize = true;
}
2015-05-20 22:39:43 +02:00
2016-02-01 17:49:55 +01:00
if(stack.stackSize > 0){
EntityItem entity = new EntityItem(world, pos.getX()+0.5, pos.getY()+0.5, pos.getZ()+0.5, stack);
world.spawnEntityInWorld(entity);
}
}
}
world.setBlockState(pos, getStateFromMeta(0));
}
return true;
}
return false;
2015-10-03 10:19:40 +02:00
}
@Override
public int getDamageValue(World world, BlockPos pos){
2015-10-03 10:19:40 +02:00
return 0;
2016-02-01 17:49:55 +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){
return this.getMetaFromState(state) >= 7 ? random.nextInt(addDropAmount)+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
}