Jump to content

Recommended Posts

Posted

I override the method onItemUse() for my own item. This is what it looks like:  

@Override
	/**
     * Called when a Block is right-clicked with this Item
     */
    public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
		if (imageName != null && colorMatrix != null) {
				for (int i = 0; i < colorMatrix.length; i++) {
					// build each line of the blockMatrix
					for (int j = 0; j < colorMatrix[0].length; j++) {
					
						if (colorMatrix[i][j] != null)
							worldIn.setBlockState(new BlockPos(pos.getX() + j, pos.getY() + 1, pos.getZ() + i), Blocks.WOOL.getDefaultState().withProperty(COLOR, colorMatrix[i][j]));
						else
							worldIn.setBlockState(new BlockPos(pos.getX() + j, pos.getY() + 1, pos.getZ() + i), Blocks.TNT.getDefaultState());
					}
				}
                    		System.out.println("TEST TEST TEST");
				player.sendMessage(new TextComponentString("Image build successfully!"));
		} else {
			player.sendMessage(new TextComponentString("There was an error building your image. Image name: " + imageName + "; colorMatrix: " + colorMatrix));
		}
			
		return EnumActionResult.PASS;
    }

Whenever I right click with my Item, some blocks are placed after which the message "Image build successfully" is displayed twice in the in game chat.  

If there is an error, the error message is also displayed twice.  

The console output looks like this:  

[16:53:44] [main/INFO] [STDOUT]: [me.opkekz.imagespawner.item.ImageSpawnerItem:onItemUse:91]: TEST TEST TEST
[16:53:44] [main/INFO] [minecraft/GuiNewChat]: [CHAT] Image build successfully!
[16:53:44] [Server thread/INFO] [STDOUT]: [me.opkekz.imagespawner.item.ImageSpawnerItem:onItemUse:91]: TEST TEST TEST
[16:53:44] [main/INFO] [minecraft/GuiNewChat]: [CHAT] Image build successfully!

This means that the onItemUse() method is being called twice. What could cause this?  

If you need any more information please let me know.

 

Thanks in advance.

Posted

The method is called twice indeed, once on the server, once on the client as you can see from your console output.

 

Also:

39 minutes ago, Kekz said:

if (imageName != null && colorMatrix != null) {

If these are fields stored in your items then you can't do that since items are singletons. 

Posted
4 minutes ago, V0idWa1k3r said:

The method is called twice indeed, once on the server, once on the client as you can see from your console output.

How can I prevent that? The method spawns a lot of blocks and having that done twice would be very costly. Or does it have to be called twice?

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.