FireChargeItem .java

net.minecraft.world.item.FireChargeItem (class): members: createDispenseConfig, shoot, asProjectile, useOn. imports: CandleCakeBlock, SmallFireball.

net.minecraft.world.itemclass89 linescommonextends Item3.5 KB

Implements

API Surface (4)

InteractionResult useOn(final UseOnContext context)
Projectile asProjectile(final Level level, final Position position, final ItemStack itemStack, final Direction direction)
void shoot(final Projectile projectile, final double xd, final double yd, final double zd, final float pow, final float uncertainty)
ProjectileItem.DispenseConfig createDispenseConfig()

Full Source

FireChargeItem.java89 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Direction;
5
import net.minecraft.core.Position;
6
import net.minecraft.sounds.SoundEvents;
7
import net.minecraft.sounds.SoundSource;
8
import net.minecraft.util.RandomSource;
9
import net.minecraft.world.InteractionResult;
10
import net.minecraft.world.entity.projectile.Projectile;
11
import net.minecraft.world.entity.projectile.hurtingprojectile.SmallFireball;
12
import net.minecraft.world.item.context.UseOnContext;
13
import net.minecraft.world.level.Level;
14
import net.minecraft.world.level.block.BaseFireBlock;
15
import net.minecraft.world.level.block.CampfireBlock;
16
import net.minecraft.world.level.block.CandleBlock;
17
import net.minecraft.world.level.block.CandleCakeBlock;
18
import net.minecraft.world.level.block.DispenserBlock;
19
import net.minecraft.world.level.block.state.BlockState;
20
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
21
import net.minecraft.world.level.gameevent.GameEvent;
22
import net.minecraft.world.phys.Vec3;
23
24
public class FireChargeItem extends Item implements ProjectileItem {
25
	public FireChargeItem(final Item.Properties properties) {
26
		super(properties);
27
	}
28
29
	@Override
30
	public InteractionResult useOn(final UseOnContext context) {
31
		Level level = context.getLevel();
32
		BlockPos pos = context.getClickedPos();
33
		BlockState blockState = level.getBlockState(pos);
34
		boolean used = false;
35
		if (!CampfireBlock.canLight(blockState) && !CandleBlock.canLight(blockState) && !CandleCakeBlock.canLight(blockState)) {
36
			pos = pos.relative(context.getClickedFace());
37
			if (BaseFireBlock.canBePlacedAt(level, pos, context.getHorizontalDirection())) {
38
				this.playSound(level, pos);
39
				level.setBlockAndUpdate(pos, BaseFireBlock.getState(level, pos));
40
				level.gameEvent(context.getPlayer(), GameEvent.BLOCK_PLACE, pos);
41
				used = true;
42
			}
43
		} else {
44
			this.playSound(level, pos);
45
			level.setBlockAndUpdate(pos, blockState.setValue(BlockStateProperties.LIT, true));
46
			level.gameEvent(context.getPlayer(), GameEvent.BLOCK_CHANGE, pos);
47
			used = true;
48
		}
49
50
		if (used) {
51
			context.getItemInHand().shrink(1);
52
			return InteractionResult.SUCCESS;
53
		} else {
54
			return InteractionResult.FAIL;
55
		}
56
	}
57
58
	private void playSound(final Level level, final BlockPos pos) {
59
		RandomSource random = level.getRandom();
60
		level.playSound(null, pos, SoundEvents.FIRECHARGE_USE, SoundSource.BLOCKS, 1.0F, (random.nextFloat() - random.nextFloat()) * 0.2F + 1.0F);
61
	}
62
63
	@Override
64
	public Projectile asProjectile(final Level level, final Position position, final ItemStack itemStack, final Direction direction) {
65
		RandomSource random = level.getRandom();
66
		double dirX = random.triangle(direction.getStepX(), 0.11485000000000001);
67
		double dirY = random.triangle(direction.getStepY(), 0.11485000000000001);
68
		double dirZ = random.triangle(direction.getStepZ(), 0.11485000000000001);
69
		Vec3 dir = new Vec3(dirX, dirY, dirZ);
70
		SmallFireball fireball = new SmallFireball(level, position.x(), position.y(), position.z(), dir.normalize());
71
		fireball.setItem(itemStack);
72
		return fireball;
73
	}
74
75
	@Override
76
	public void shoot(final Projectile projectile, final double xd, final double yd, final double zd, final float pow, final float uncertainty) {
77
	}
78
79
	@Override
80
	public ProjectileItem.DispenseConfig createDispenseConfig() {
81
		return ProjectileItem.DispenseConfig.builder()
82
			.positionFunction((source, direction) -> DispenserBlock.getDispensePosition(source, 1.0, Vec3.ZERO))
83
			.uncertainty(6.6666665F)
84
			.power(1.0F)
85
			.overrideDispenseEvent(1018)
86
			.build();
87
	}
88
}
89

Decompiled Minecraft 26.3 source of FireChargeItem (net/minecraft/world/item/FireChargeItem.java) with Mojang official mappings, stored verbatim on the site.