Jump to content

[SOLVED] setBlockState In World onItemUse (1.14.4 Beta)


Blu_Nighttime

Recommended Posts

Just starting updating my mod to Forge Minecraft 1.14 from 1.12 and all is good, however, I'm trying to implement an onItemUse and depending on the direction the player is looking in, set blocks around the player if they are air to fire - I'm trying to keep the code similar to my 1.12 version and make changes to the code where I need to for 1.14. The issue is that when the player right clicks with the item, nothing more than what the item extends happens (the item extends the flint & steel) so I'm not sure if onItemUse is even triggering. Here's the code listed below.

package com.github.theonepath.bfs.items;

import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.FlintAndSteelItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class CustomFlintAndSteelItem extends FlintAndSteelItem {
	public static BlockPos pos1, pos2, pos3, pos4;	// variables to change the location of the blocks origin, i.e. offset 1 block to the left

	// The constructure that is asked to be created
	public CustomFlintAndSteelItem(Properties builder) {
		super(builder);
	}

  	// The onItemUse method. When I add the @Override annotation similar to 1.12 then an error is thrown and '@Override' is asked to be removed. I also don't know if the method is being tiggered or not
	public ActionResultType onItemUse(PlayerEntity player, World worldIn, BlockPos pos, Hand handIn, Direction facing, float hitX, float hitY, float hitZ) {
		pos = pos.offset(facing);	
		pos1 = pos.add(1, 0, 0);	// pos1 and pos2 for left and right facing North or South
		pos2 = pos.add(-1, 0, 0);
	    pos3 = pos.add(0, 0, 1);	// pos3 and pos4 for left and right facing East or West
	    pos4 = pos.add(0, 0, -1);
	    
        ItemStack itemstack = player.getHeldItem(handIn);	// get Item being held
        if (!player.canPlayerEdit(pos, facing, itemstack))
        {
            return ActionResultType.FAIL;	// return FAIL if condition is true
        }
        else
        {
            if (worldIn.isAirBlock(pos))	// Otherwise play the sound and set the block in front of the player to fire if the bloack is air
            {
                worldIn.playSound(player, pos, SoundEvents.ITEM_FLINTANDSTEEL_USE, SoundCategory.BLOCKS, 1.0F, random.nextFloat() * 0.4F + 0.8F);
                worldIn.setBlockState(pos, Blocks.FIRE.getDefaultState(), 11);
                
              	// Set the blocks left and right of the origin to fire depending if the player is Facing North or South, or East or West
                if(looking[0] == Direction.NORTH || looking[0] == Direction.SOUTH) {
                	if(worldIn.isAirBlock(pos1)) {
	                	worldIn.setBlockState(pos1, Blocks.FIRE.getDefaultState(), 11);
	                }
                }
                
                if(player.getHorizontalFacing() == Direction.NORTH || player.getHorizontalFacing() == Direction.SOUTH) {
	                
                	if(worldIn.isAirBlock(pos1)) {
	                	worldIn.setBlockState(pos1, Blocks.FIRE.getDefaultState(), 11);
	                }
	                if(worldIn.isAirBlock(pos2)) {
	                	worldIn.setBlockState(pos2, Blocks.FIRE.getDefaultState(), 11);
	                }
                }
                if(player.getHorizontalFacing() == Direction.EAST || player.getHorizontalFacing() == Direction.WEST) {
	                if(worldIn.isAirBlock(pos3)) {
	                	worldIn.setBlockState(pos3, Blocks.FIRE.getDefaultState(), 11);
	                }
	                if(worldIn.isAirBlock(pos4)) {
	                	worldIn.setBlockState(pos4, Blocks.FIRE.getDefaultState(), 11);
	                }
                }
            }

            if (player instanceof ServerPlayerEntity)	// server-side handling similar to 1.12
            {
                CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayerEntity)player, pos, itemstack);
            }

            
            itemstack.damageItem(1, player, p_219999_1_ -> p_219999_1_.sendBreakAnimation(player.getActiveHand())); // Damage the item which works and return SUCCESS
            return ActionResultType.SUCCESS;
        }
	}

}

If the code seems a little janky, then I can make it more efficient later but for now I need to figure out what's not working properly. And the code to call the class for when the item is being created is as followed (I don't believe it to be the issue but here it is anyways).

@Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
	public static class RegistryEvents{
		
		@SubscribeEvent
		public static void registerItems(final RegistryEvent.Register<Item> event) {
			event.getRegistry().registerAll(
				ItemList.customflintandsteelitem = new CustomFlintAndSteelItem(new Item.Properties().group(bfscreativetab).maxStackSize(1).maxDamage(85)).setRegistryName(new ResourceLocation(MODID, "flintandalumbrass")),
              			// ... more items follow ...
		};

 

Thanks for any advice in advance.

Edited by Blu_Nighttime
title edit
Link to comment
Share on other sites

Well. Its not being called because you're not actually overriding a method.

You'd know this if you used the @Override annotation. The method only takes a single Context parameter now.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.