plant and harvest works, but getting the first from grass does not yet
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
package de.jottyfan.minecraft.block;
|
package de.jottyfan.minecraft.block;
|
||||||
|
|
||||||
import java.util.List;
|
import de.jottyfan.minecraft.Quickly;
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.core.registries.BuiltInRegistries;
|
import net.minecraft.core.registries.BuiltInRegistries;
|
||||||
import net.minecraft.resources.Identifier;
|
import net.minecraft.resources.Identifier;
|
||||||
@@ -14,7 +12,6 @@ import net.minecraft.world.item.ItemStack;
|
|||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
import net.minecraft.world.level.block.CropBlock;
|
import net.minecraft.world.level.block.CropBlock;
|
||||||
import net.minecraft.world.level.block.state.BlockState;
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
import net.minecraft.world.level.storage.loot.LootParams.Builder;
|
|
||||||
import net.minecraft.world.phys.BlockHitResult;
|
import net.minecraft.world.phys.BlockHitResult;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,37 +21,29 @@ import net.minecraft.world.phys.BlockHitResult;
|
|||||||
*/
|
*/
|
||||||
public class BlockPlant extends CropBlock {
|
public class BlockPlant extends CropBlock {
|
||||||
|
|
||||||
private Identifier seed;
|
private String seedName;
|
||||||
private Identifier fruit;
|
private String fruitName;
|
||||||
|
|
||||||
public BlockPlant(Properties properties, Identifier seed, Identifier fruit) {
|
public BlockPlant(Properties properties, String seedName, String fruitName) {
|
||||||
super(properties);
|
super(properties);
|
||||||
this.seed = seed;
|
this.seedName = seedName;
|
||||||
this.fruit = fruit;
|
this.fruitName = fruitName;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected List<ItemStack> getDrops(BlockState state, Builder builder) {
|
|
||||||
List<ItemStack> list = List.of(new ItemStack(getSeed()));
|
|
||||||
if (isMaxAge(state)) {
|
|
||||||
list.add(new ItemStack(getSeed(), new Random().nextInt(2)));
|
|
||||||
list.add(new ItemStack(getFruit(), new Random().nextFloat() > 0.9f ? 2 : 1));
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Item getFruit() {
|
|
||||||
return BuiltInRegistries.ITEM.getValue(fruit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Item getSeed() {
|
public Item getSeed() {
|
||||||
return BuiltInRegistries.ITEM.getValue(seed);
|
return BuiltInRegistries.ITEM.getValue(Identifier.fromNamespaceAndPath(Quickly.MOD_ID, seedName));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item getFruit() {
|
||||||
|
return BuiltInRegistries.ITEM.getValue(Identifier.fromNamespaceAndPath(Quickly.MOD_ID, fruitName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player,
|
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player,
|
||||||
BlockHitResult hitResult) {
|
BlockHitResult hitResult) {
|
||||||
if (!level.isClientSide() && isMaxAge(state)) {
|
if (!level.isClientSide() && isMaxAge(state)) {
|
||||||
|
int fruitAmount = (level.getRandom().nextFloat() > 0.9f) ? 2 : 1;
|
||||||
|
Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(getFruit(), fruitAmount));
|
||||||
Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(getSeed()));
|
Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(getSeed()));
|
||||||
level.setBlock(pos, state.setValue(AGE, 0), 2);
|
level.setBlock(pos, state.setValue(AGE, 0), 2);
|
||||||
return InteractionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class QuicklyBlocks {
|
|||||||
public static final Block OREDEEPSLATETURQUOISE = registerBlock("oredeepslateturquoise",
|
public static final Block OREDEEPSLATETURQUOISE = registerBlock("oredeepslateturquoise",
|
||||||
p -> new BlockOreDeepslateTurquoise(p));
|
p -> new BlockOreDeepslateTurquoise(p));
|
||||||
public static final Block COTTONPLANT = registerBlock("blockcottonplant", Properties.ofFullCopy(Blocks.WHEAT),
|
public static final Block COTTONPLANT = registerBlock("blockcottonplant", Properties.ofFullCopy(Blocks.WHEAT),
|
||||||
p -> new BlockPlant(p, Identifier.fromNamespaceAndPath(Quickly.MOD_ID, "cottonseed"), Identifier.fromNamespaceAndPath(Quickly.MOD_ID, "cotton")));
|
p -> new BlockPlant(p, "cottonseed", "cotton"));
|
||||||
|
|
||||||
private static final Block registerBlock(String name, Properties properties) {
|
private static final Block registerBlock(String name, Properties properties) {
|
||||||
return QuicklyBlocks.registerBlock(name, properties, p -> new Block(p));
|
return QuicklyBlocks.registerBlock(name, properties, p -> new Block(p));
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.jottyfan.minecraft.item;
|
package de.jottyfan.minecraft.item;
|
||||||
|
|
||||||
|
import de.jottyfan.minecraft.Quickly;
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.core.registries.BuiltInRegistries;
|
import net.minecraft.core.registries.BuiltInRegistries;
|
||||||
import net.minecraft.resources.Identifier;
|
import net.minecraft.resources.Identifier;
|
||||||
@@ -17,11 +18,15 @@ import net.minecraft.world.level.block.state.BlockState;
|
|||||||
*/
|
*/
|
||||||
public class Plant extends Item {
|
public class Plant extends Item {
|
||||||
|
|
||||||
private Identifier identifier;
|
private String plantName;
|
||||||
|
|
||||||
public Plant(Properties properties, Identifier identifier) {
|
public Plant(Properties properties, String plantName) {
|
||||||
super(properties);
|
super(properties);
|
||||||
this.identifier = identifier;
|
this.plantName = plantName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Block getPlant() {
|
||||||
|
return BuiltInRegistries.BLOCK.getValue(Identifier.fromNamespaceAndPath(Quickly.MOD_ID, plantName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -30,8 +35,7 @@ public class Plant extends Item {
|
|||||||
if (this.asItem().equals(context.getItemInHand().getItem())) {
|
if (this.asItem().equals(context.getItemInHand().getItem())) {
|
||||||
BlockState state = context.getLevel().getBlockState(pos);
|
BlockState state = context.getLevel().getBlockState(pos);
|
||||||
if (Blocks.FARMLAND.equals(state.getBlock()) && context.getLevel().getBlockState(pos.above()).isAir()) {
|
if (Blocks.FARMLAND.equals(state.getBlock()) && context.getLevel().getBlockState(pos.above()).isAir()) {
|
||||||
Block plantBlock = BuiltInRegistries.BLOCK.getValue(identifier);
|
context.getLevel().setBlock(pos.above(), getPlant().defaultBlockState(), 2);
|
||||||
context.getLevel().setBlock(pos.above(), plantBlock.defaultBlockState(), 2);
|
|
||||||
context.getItemInHand().shrink(1);
|
context.getItemInHand().shrink(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public class QuicklyItems {
|
|||||||
public static final Item TURQUOISEINGOT = registerItem("turquoiseingot");
|
public static final Item TURQUOISEINGOT = registerItem("turquoiseingot");
|
||||||
public static final Item COTTON = registerItem("cotton");
|
public static final Item COTTON = registerItem("cotton");
|
||||||
public static final Item COTTONPLANT = registerItem("cottonplant");
|
public static final Item COTTONPLANT = registerItem("cottonplant");
|
||||||
public static final Item COTTONSEED = registerItem("cottonseed", properties -> new Plant(properties, Identifier.fromNamespaceAndPath(Quickly.MOD_ID, "cottonplant")));
|
public static final Item COTTONSEED = registerItem("cottonseed", properties -> new Plant(properties, "blockcottonplant"));
|
||||||
|
|
||||||
private static final Item registerItem(String name) {
|
private static final Item registerItem(String name) {
|
||||||
return QuicklyItems.registerItem(name, new Item.Properties());
|
return QuicklyItems.registerItem(name, new Item.Properties());
|
||||||
|
|||||||
Reference in New Issue
Block a user