2020-01-30 16:04:40 +01:00
|
|
|
package de.ellpeck.naturesaura.items;
|
|
|
|
|
2020-02-25 15:14:56 +01:00
|
|
|
import de.ellpeck.naturesaura.gen.ModFeatures;
|
2023-07-25 22:34:18 +02:00
|
|
|
import net.minecraft.core.Registry;
|
|
|
|
import net.minecraft.core.registries.Registries;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.InteractionResult;
|
|
|
|
import net.minecraft.world.item.context.UseOnContext;
|
|
|
|
import net.minecraft.world.level.block.Blocks;
|
|
|
|
import net.minecraft.world.level.block.NetherWartBlock;
|
2023-07-25 22:34:18 +02:00
|
|
|
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
|
2020-01-30 16:04:40 +01:00
|
|
|
|
|
|
|
public class ItemCrimsonMeal extends ItemImpl {
|
2021-12-15 14:26:42 +01:00
|
|
|
|
2020-01-30 16:04:40 +01:00
|
|
|
public ItemCrimsonMeal() {
|
|
|
|
super("crimson_meal");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 14:26:42 +01:00
|
|
|
public InteractionResult useOn(UseOnContext context) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var level = context.getLevel();
|
|
|
|
var pos = context.getClickedPos();
|
|
|
|
var state = level.getBlockState(pos);
|
2020-01-30 16:04:40 +01:00
|
|
|
if (state.getBlock() == Blocks.NETHER_WART) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!level.isClientSide) {
|
2021-12-15 14:26:42 +01:00
|
|
|
if (level.random.nextInt(5) == 0) {
|
|
|
|
int age = state.getValue(NetherWartBlock.AGE);
|
2020-01-30 16:04:40 +01:00
|
|
|
if (age >= 3) {
|
2023-07-25 22:34:18 +02:00
|
|
|
Registry<ConfiguredFeature<?, ?>> registry = level.registryAccess().registryOrThrow(Registries.CONFIGURED_FEATURE);
|
|
|
|
registry.getHolderOrThrow(ModFeatures.Configured.NETHER_WART_MUSHROOM).value().place((ServerLevel) level, ((ServerLevel) level).getChunkSource().getGenerator(), level.random, pos);
|
2020-01-30 16:04:40 +01:00
|
|
|
} else {
|
2021-12-15 14:26:42 +01:00
|
|
|
level.setBlockAndUpdate(pos, state.setValue(NetherWartBlock.AGE, age + 1));
|
2020-01-30 16:04:40 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-15 14:26:42 +01:00
|
|
|
level.levelEvent(2005, pos, 0);
|
|
|
|
context.getItemInHand().shrink(1);
|
2020-01-30 16:04:40 +01:00
|
|
|
}
|
2021-12-04 15:40:09 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
2021-12-15 14:26:42 +01:00
|
|
|
} else if (level.getBlockState(pos.above()).isAir() && level.getBlockState(pos).getBlock() == Blocks.SOUL_SAND) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!level.isClientSide) {
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = level.random.nextInt(5); i >= 0; i--) {
|
|
|
|
var offset = pos.offset(Mth.nextInt(level.random, -3, 3), 1, Mth.nextInt(level.random, -3, 3));
|
2021-12-15 14:26:42 +01:00
|
|
|
if (level.getBlockState(offset.below()).getBlock() == Blocks.SOUL_SAND && level.getBlockState(offset).isAir()) {
|
|
|
|
level.setBlockAndUpdate(offset, Blocks.NETHER_WART.defaultBlockState());
|
2020-01-30 20:35:25 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-15 14:26:42 +01:00
|
|
|
level.levelEvent(2005, pos, 0);
|
|
|
|
context.getItemInHand().shrink(1);
|
2020-01-30 20:35:25 +01:00
|
|
|
}
|
2021-12-04 15:40:09 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
2020-01-30 16:04:40 +01:00
|
|
|
}
|
2021-12-04 15:40:09 +01:00
|
|
|
return InteractionResult.FAIL;
|
2020-01-30 16:04:40 +01:00
|
|
|
}
|
|
|
|
}
|