Solved Help PlayerInteractEvent Drugs

Discussion in 'Spigot Plugin Development' started by _CristianTito_, Sep 13, 2019 at 7:57 PM.

  1. [​IMG]
    Help me :C
    Code (Java):
            if (p.getItemInHand().equals(Drugs.addCocaine())) {
                p.sendMessage(getConfig().getString("Prefix").replace('&', '§')
                        + getConfig().getString("StartCoca").replace('&', '§'));
                p.addPotionEffect(PotionEffectType.SPEED.createEffect(400, 1));
                p.addPotionEffect(PotionEffectType.REGENERATION.createEffect(400, 1));
                p.addPotionEffect(PotionEffectType.INCREASE_DAMAGE.createEffect(400, 1));
                int amount = Drugs.addCocaine().getAmount() - 1;
                  if (amount > 0) {
                      Drugs.addCocaine().setAmount(amount);
                  } else {
                    p.getInventory().setItemInHand(null);
                  }
            }
     
  2. Can you send the code to the PlayerInteractEvent please?

    - Dagen from Gegobyte Developers
     
  3. As first you should post your code, but I'm assumingen you are equaling the item with an ItemStack. Instead of using the ItemStack get the type of the itemstack and check if its the same type as you're item which you want to equal it to.
     
  4. Instead of:
    Code (Text):
    Drugs.addCocaine().setAmount(amount);
    Do:
    Code (Text):
    p.getInventory.getItemInHand().setAmount(p.getInventory.getItemInHand().getAmount() -1);
    And change:
    Code (Text):
    amount > 0
    To:
    Code (Text):
    amount => 1
    - Dagen from Gegobyte Developers
     
    • Optimistic Optimistic x 1
  5. Your issue is that you are comparing itemstacks using #equals. Basically #equals will check for the amount of the itemstacks compared as well. If they are not equal in the amount too, it will not return true. A solution would be to change
    if (p.getItemInHand().equals(Drugs.addCocaine())) { to if (p.getItemInHand().isSimilar(Drugs.addCocaine())) {
    Everything else seems "fine"
     
    #5 Shin1gamiX, Sep 13, 2019 at 8:07 PM
    Last edited: Sep 13, 2019 at 8:20 PM
    • Agree Agree x 1
  6. Code (Text):
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if ((e.getAction() != Action.RIGHT_CLICK_AIR) && (e.getAction() != Action.RIGHT_CLICK_BLOCK)) {
                return;
            }
            if (p.getItemInHand().equals(Drugs.addCocaine())) {
                int amount = Drugs.addCocaine().getAmount() - 1;
                  if (amount > 1) {
                      p.getInventory().getItemInHand().setAmount(p.getInventory().getItemInHand().getAmount() -1);

                  } else {
                    p.getInventory().setItemInHand(null);
                  }
                p.sendMessage(getConfig().getString("Prefix").replace('&', '§')
                        + getConfig().getString("StartCoca").replace('&', '§'));
                p.addPotionEffect(PotionEffectType.SPEED.createEffect(400, 1));
                p.addPotionEffect(PotionEffectType.REGENERATION.createEffect(400, 1));
                p.addPotionEffect(PotionEffectType.INCREASE_DAMAGE.createEffect(400, 1));
            }
        }
    Dont work
     
  7. Also do what @Shin1gamiX said.

    - Dagen from Gegobyte Developers
     
  8. Yeah,its work, why i have the stack, and click on cocaine the plugin remove all stack and dont remove 1 item
     
  9. Not entirely sure what do you mean.

    EDIT: I re-read your reply and now understood. Your issue is that you are checking the amount of items from your already ready custom item from your Drugs class. Assuming it's amount is 1, it will remove the entire item that the player is holding, no matter the amount it has. A solution would be to check the amount of items that the player is holding instead .
     
  10. I give the "cocaine" in my inventory, i click 64 cocaine item the plugin remove all 64 item instead of 1, understand?, sorry for my bad english.
     
  11. I've edited my reply above.
     
  12. Change:
    Code (Text):
    int amount = Drugs.addCocaine().getAmount() - 1;
    To:
    Code (Text):
    int amount = p.getItemInHand().getAmount();
    - Dagen from Gegobyte Developers
     
  13. Make sure to null check p.getItemInHand() as it can obviously be null.
     
  14. thx its worked Shin1gamiX, and thx gego.dev!
    i changed the string Drugs.addCannabis().setAmount(amount); to this p.getItemInHand().setAmount(amount);.

    Code (Java):
            if (p.getItemInHand().isSimilar(Drugs.addCocaine())) {

                int amount = p.getItemInHand ().getAmount() -1;
                  if (amount > 0) {
                      [B]Drugs.addCannabis().setAmount(amount);  [/B]                  
                  } else {
                    p.getInventory().setItemInHand(null);
                  }