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

123 lines
4.3 KiB
Java
Raw Normal View History

2019-05-02 10:47:21 +02:00
/*
2021-02-26 22:15:48 +01: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
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 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
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
2024-03-03 01:20:53 +01:00
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.material.PushReaction;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.phys.BlockHitResult;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.common.PlantType;
import net.neoforged.neoforge.items.ItemHandlerHelper;
2015-05-20 22:39:43 +02:00
import java.util.List;
2021-11-21 22:03:07 +01:00
import java.util.function.Supplier;
2024-03-02 21:23:08 +01:00
public class BlockPlant extends CropBlock {
2021-11-21 22:03:07 +01:00
public Supplier<Item> seedItem;
2016-01-16 14:14:39 +01:00
// Stolen from potato for now
// PotatoBlock(AbstractBlock.Properties.create(Material.PLANTS).doesNotBlockMovement().tickRandomly().zeroHardnessAndResistance().sound(SoundType.CROP)));
2021-11-21 22:03:07 +01:00
public BlockPlant(Supplier<Item> seedItem) {
2024-03-03 01:20:53 +01:00
super(Properties.of().mapColor(MapColor.PLANT).pushReaction(PushReaction.DESTROY).noCollission().randomTicks().instabreak().sound(SoundType.CROP));
this.seedItem = seedItem;
}
// Remove
@Deprecated
2021-11-21 22:03:07 +01:00
public void doStuff(Supplier<Item> seedItem, Item returnItem, int returnMeta) {
this.seedItem = seedItem;
// this.returnItem = returnItem;
// this.returnMeta = returnMeta;
2016-02-01 17:49:55 +01:00
}
2016-06-27 20:24:45 +02:00
@Override
2024-03-02 21:23:08 +01:00
public PlantType getPlantType(BlockGetter world, BlockPos pos) {
return PlantType.CROP;
2015-05-20 22:39:43 +02:00
}
//
// @Override
// public int damageDropped(BlockState state) {
// return this.getMetaFromState(state) >= 7
// ? this.returnMeta
// : 0;
// }
2015-05-20 22:39:43 +02:00
2015-06-28 21:28:58 +02:00
2015-05-20 22:39:43 +02:00
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit) {
if (this.getAge(state) < 7) {
2024-03-02 21:23:08 +01:00
return InteractionResult.PASS;
}
2015-05-20 22:39:43 +02:00
if (!world.isClientSide) {
2024-03-02 21:23:08 +01:00
List<ItemStack> drops = Block.getDrops(state, (ServerLevel) world, pos, null);
boolean deductedSeedSize = false;
for (ItemStack drop : drops) {
if (StackUtil.isValid(drop)) {
2024-03-03 01:20:53 +01:00
if (drop.getItem() == this.seedItem.get() && !deductedSeedSize) {
drop.shrink(1);
deductedSeedSize = true;
}
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(drop)) {
ItemHandlerHelper.giveItemToPlayer(player, drop);
2016-02-01 17:49:55 +01:00
}
}
}
world.setBlockAndUpdate(pos, this.defaultBlockState().setValue(AGE, 0));
2016-02-01 17:49:55 +01:00
}
return super.use(state, world, pos, player, handIn, hit);
2015-10-03 10:19:40 +02:00
}
2016-02-01 20:39:11 +01:00
@Override
2024-03-02 21:23:08 +01:00
protected ItemLike getBaseSeedId() {
2021-11-21 22:03:07 +01:00
return this.seedItem.get();
2015-10-03 10:19:40 +02:00
}
// @Override
// public int quantityDropped(BlockState state, int fortune, Random random) {
// return this.getMetaFromState(state) >= 7
// ? random.nextInt(this.addDropAmount) + this.minDropAmount
// : super.quantityDropped(state, fortune, random);
// }
2016-02-01 17:49:55 +01:00
// TODO: [port] move to data table
2016-02-01 17:49:55 +01:00
// @Override
// public Item getCrop() {
// return this.returnItem;
// }
// TODO: [port] move to data table
2016-02-01 17:49:55 +01:00
// @Override
// public Item getItemDropped(BlockState state, Random rand, int par3) {
// return this.getMetaFromState(state) >= 7
// ? this.getCrop()
// : this.getSeed();
// }
2021-02-26 22:15:48 +01:00
}