use of net.minecraft.util.ActionResultType in project AgriCraft by AgriCraft.
the class ItemDynamicAgriSeed method onItemUse.
@Nonnull
@Override
public ActionResultType onItemUse(@Nonnull ItemUseContext context) {
World world = context.getWorld();
BlockPos pos = context.getPos();
TileEntity tile = world.getTileEntity(pos);
ItemStack stack = context.getItem();
PlayerEntity player = context.getPlayer();
// If crop sticks were clicked, attempt to plant the seed
if (tile instanceof TileEntityCropSticks) {
return this.attemptSeedPlant((TileEntityCropSticks) tile, stack, player);
}
// If a soil was clicked, check the block on top of the soil and handle accordingly
return AgriApi.getSoil(world, pos).map(soil -> {
BlockPos up = pos.up();
TileEntity above = world.getTileEntity(up);
// There are currently crop sticks on the soil, attempt to plant on the crop sticks
if (above instanceof TileEntityCropSticks) {
return this.attemptSeedPlant((TileEntityCropSticks) above, stack, player);
}
// There are currently no crop sticks, check if the place is suitable and plant the plant directly
if (above == null && AgriCraft.instance.getConfig().allowPlantingOutsideCropSticks()) {
BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getStateForPlacement(world, up);
if (newState != null && world.setBlockState(up, newState, 11)) {
boolean success = AgriApi.getCrop(world, up).map(crop -> this.getGenome(context.getItem()).map(genome -> crop.plantGenome(genome, player)).map(result -> {
if (result) {
// consume item
if (player == null || !player.isCreative()) {
stack.shrink(1);
}
}
return result;
}).orElse(false)).orElse(false);
if (success) {
return ActionResultType.SUCCESS;
} else {
world.setBlockState(up, Blocks.AIR.getDefaultState());
}
}
}
// Neither alternative option was successful, delegate the call to the super method
return super.onItemUse(context);
}).orElse(super.onItemUse(context));
}
Aggregations