Posted May 18, 20196 yr 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.
May 18, 20196 yr 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.
May 18, 20196 yr Author 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?
May 18, 20196 yr You don't "prevent" a call of a method, that makes no sense. Check the side you are on before performing the operations. You need to do them on the server anyway. https://mcforge.readthedocs.io/en/latest/concepts/sides/
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.