Jump to content

PlayerInteractEvent.RightClickEmpty Won't Work


mbkgr

Recommended Posts

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 by mbkgr
Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

22 hours ago, ChampionAsh5357 said:

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);
            }
        }

 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.



×
×
  • Create New...

Important Information

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