Posted October 13, 20205 yr Hello Guys, I want to do when a player right click to certain item do something. But When I do PlayerInteractEvent.LeftClickEmpty it works. Can anyone help me ? (FORGE 1.15.2) public static void itemUpgrade(PlayerInteractEvent.RightClickEmpty event) { PlayerEntity player = event.getPlayer(); String msg = TextFormatting.RED + "Success!"; String msg2 = TextFormatting.RED + "Failed!"; if (player.getHeldItemMainhand().getItem() == RegistryHandler.DRAGON_STEEL_SWORD.get() && player.getHeldItemOffhand().getItem() == RegistryHandler.DRAGON_STEEL.get()) { gblnDiablo.LOGGER.info("Player tried upgrade"); if (Math.random() < 0.7D) { ItemStack giveItem = new ItemStack(RegistryHandler.DRAGON_STEEL_SWORD_1.get(), 1); player.sendMessage(new StringTextComponent(msg)); giveItem.setCount(1); ItemHandlerHelper.giveItemToPlayer(player, giveItem); } else { ((PlayerEntity) player).inventory.clearMatchingItems(p -> new ItemStack(RegistryHandler.DRAGON_STEEL_SWORD.get(), (int) (1)).getItem() == p.getItem(), (int) 1); ((PlayerEntity) player).inventory.clearMatchingItems(p -> new ItemStack(RegistryHandler.DRAGON_STEEL.get(), (int) (1)).getItem() == p.getItem(), (int) 1); /* player.inventory.clearMatchingItems((p) -> { return (new ItemStack(player.getHeldItemMainhand().getItem(), 1)).getItem() == p.getItem(); }, 1); */ /* player.inventory.clearMatchingItems((p) -> { return (new ItemStack(player.getHeldItemOffhand().getItem(), 1)).getItem() == p.getItem(); }, 1); */ player.sendMessage(new StringTextComponent(msg2)); } } } Edited October 13, 20205 yr by mbkgr
October 13, 20205 yr Author 12 minutes ago, ChampionAsh5357 said: Read the javadoc on the event. You're gonna need to send a packet. Thank you for reply. Where can I find it ? And also how can I find the other event usages ?
October 13, 20205 yr 35 minutes ago, mbkgr said: Where can I find it ? And also how can I find the other event usages ? You can locate the class in the source or open it directly by clicking on the class name using whatever method your IDE specifies.
October 13, 20205 yr Author 27 minutes ago, ChampionAsh5357 said: You can locate the class in the source or open it directly by clicking on the class name using whatever method your IDE specifies. Thank you I found it but I cant figured it out... How can I solve it ?
October 14, 20205 yr Author 20 hours ago, ChampionAsh5357 said: https://mcforge.readthedocs.io/en/latest/networking/simpleimpl/ Thank you again for answer.I understand that I should work java more and harder...
October 14, 20205 yr Author 22 hours ago, ChampionAsh5357 said: https://mcforge.readthedocs.io/en/latest/networking/simpleimpl/ I solved it.But there is a another problem.When player does event, event triggers 4 time.How can I solve that ? @SubscribeEvent public static void rightClick(PlayerInteractEvent.RightClickItem event) { PlayerEntity player = event.getPlayer(); if (player.getHeldItemMainhand().getItem() == Items.DIAMOND_ORE && player.getHeldItemOffhand().getItem() == Items.COAL) { gblnDiablo.LOGGER.info("Player tried to upgrade"); ItemStack giveItem = new ItemStack(Items.DIAMOND, 1); player.inventory.addItemStackToInventory(giveItem); } }
October 14, 20205 yr 'This event is fired on both sides before the player triggers'. Since it holds logic, it should be properly delegated to only execute on the server.
October 14, 20205 yr Author 48 minutes ago, ChampionAsh5357 said: 'This event is fired on both sides before the player triggers'. Since it holds logic, it should be properly delegated to only execute on the server. I have two classes, one ServerEvent and the other one is ClientEvent.So I need to use ServerEvent and OnlyIn(DIST.DEDICATED_SERVER) Is that right ?
October 15, 20205 yr 16 minutes ago, mbkgr said: So I need to use ServerEvent and OnlyIn(DIST.DEDICATED_SERVER) Is that right ? No, those refer to the physical side. You should only have one instance of an event (except in very specific cases). Since this code is not specific to a physical side, there should be no sided annotations. That falls under Code Style 6. The linked doc page I sent you earlier was supposed to be for you to understand about World#isRemote which is held in every entity instance and then implement.
October 15, 20205 yr Author 26 minutes ago, ChampionAsh5357 said: No, those refer to the physical side. You should only have one instance of an event (except in very specific cases). Since this code is not specific to a physical side, there should be no sided annotations. That falls under Code Style 6. The linked doc page I sent you earlier was supposed to be for you to understand about World#isRemote which is held in every entity instance and then implement. I understand my mistake on that document It's OK now.Thank you so much.
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.