Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static final RegistryObject<Item> FIRESTONE = ITEMS.register("firestone",
() -> new Firestone(new Item.Properties().group(ModItemGroup.TUTORIAL_GROUP).maxDamage(8)));
public class Firestone extends Item {
public Firestone(Properties properties) {
super(properties);
}
@Override
public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) {
World world = context.getWorld();
if(!world.isRemote) {
PlayerEntity playerEntity = Objects.requireNonNull(context.getPlayer());
BlockState clickedBlock = world.getBlockState(context.getPos());
rightClickOnCertainBlockState(clickedBlock, context, playerEntity);
stack.damageItem(1, playerEntity, player -> player.sendBreakAnimation(context.getHand()));
}
return super.onItemUseFirst(stack, context);
}
private void rightClickOnCertainBlockState(BlockState clickedBlock, ItemUseContext context,
PlayerEntity playerEntity) {
boolean playerIsNotOnFire = !playerEntity.isBurning();
if(random.nextFloat() > 0.5f) {
lightEntityOnFire(playerEntity, 6);
} else if(playerIsNotOnFire && blockIsValidForResistance(clickedBlock)) {
gainFireResistanceAndDestroyBlock(playerEntity, context.getWorld(), context.getPos());
} else {
lightGroundOnFire(context);
}
}
private boolean blockIsValidForResistance(BlockState clickedBlock) {
return clickedBlock.getBlock() == Blocks.OBSIDIAN;
}
public static void lightEntityOnFire(Entity entity, int second) {
entity.setFire(second);
}
private void gainFireResistanceAndDestroyBlock(PlayerEntity playerEntity, World world, BlockPos pos) {
gainFireResistance(playerEntity);
world.destroyBlock(pos, false);
}
public static void gainFireResistance(PlayerEntity playerEntity) {
playerEntity.addPotionEffect(new EffectInstance(Effects.FIRE_RESISTANCE, 200));
}
public static void lightGroundOnFire(ItemUseContext context) {
PlayerEntity playerentity = context.getPlayer();
World world = context.getWorld();
BlockPos blockpos = context.getPos().offset(context.getFace());
if (AbstractFireBlock.canLightBlock(world, blockpos, context.getPlacementHorizontalFacing())) {
world.playSound(playerentity, blockpos, SoundEvents.ITEM_FLINTANDSTEEL_USE, SoundCategory.BLOCKS, 1.0F,
random.nextFloat() * 0.4F + 0.8F);
BlockState blockstate = AbstractFireBlock.getFireForPlacement(world, blockpos);
world.setBlockState(blockpos, blockstate, 11);
}
}
}
{
"parent": "item/generated",
"textures": {
"layer0": "tutorialmod:item/firestone"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment