Solved Player right click item event

Discussion in 'Plugin Development' started by Greeniousity, Apr 14, 2024.

  1. Online

    timtower Administrator Administrator Moderator

    And you never do that on the itemmeta, there is also a hasItemMeta method for this.
     
  2. Offline

    Greeniousity

    So you are suggesting the hasItemMeta()
     
  3. Online

    timtower Administrator Administrator Moderator

    Yes
     
  4. Offline

    Greeniousity

    @EventHandlerpublic void InventoryClick(InventoryClickEvent event)
    {
    System.out.println(event.getWhoClicked());
    Inventory inventory = event.getClickedInventory();
    ItemStack item = event.getCurrentItem();
    if (event.getWhoClicked() instanceof Player) {
    System.out.println(event.getWhoClicked());
    Player player = (Player) event.getWhoClicked();
    if (item.hasItemMeta() && item.getType() == Material.PUMPKIN_PIE && item.getItemMeta().getDisplayName().equals("§6Booster Pie§8§l (§eI§8§l)")) {
    boostSetup(2, player, 120);
    }
    }

    }
    did this but still not working
     
  5. Online

    timtower Administrator Administrator Moderator

    Not working how?
    Errors? Just not doing the boost?
    Did you add debug lines to see which if statement is failing?
     
  6. Offline

    Greeniousity

    no errors, boost just doesnt apply, both prints work
     
  7. Online

    timtower Administrator Administrator Moderator

    Show your prints
     
  8. Offline

    Greeniousity

    no its the name of the player printed twice. Its in the code
     
  9. Online

    timtower Administrator Administrator Moderator

    if (item.hasItemMeta() && item.getType() == Material.PUMPKIN_PIE && item.getItemMeta().getDisplayName().equals("§6Booster Pie§8§l (§eI§8§l)")) {
    But that doesn't check this line.
     
  10. Offline

    KarimAKL

    Considering this, the condition is either false or the #boostSetup method does nothing.
    Assuming the condition is true and the #boostSetup simply does nothing, the cause could be that the value of 'current' is not 1. Does a new instance of the 'playerData' class set the value returned by playerData#getBoost() to 1 or is it left uninitialized (in which case the value would be 0)?
     
  11. Offline

    Greeniousity

    getBoost() value cannot be 0 because even if it is not set, its set to 1 on join
     
  12. Offline

    KarimAKL

    You're creating a new instance in the local scope; the join event does nothing here. I assume you want a single instance of the playerData class for each player upon join. In that case, you'll have to save it in the global scope upon join and retrieve it, rather than creating it, in the #boostSetup method.
    Edit: After taking a quick look at your main class, it seems you're passing the playerData instance from the join event to the 'command' class through a static method. You should be able to retrieve it from there.
    Also, you should really be following the Java naming conventions. On top of avoiding future problems and name conflicts, you'll also be helping us help you.
     
    Last edited: Apr 21, 2024
  13. Offline

    Greeniousity

    what i realised is that the debug that is supposed to print test is never printed because curerrent boost is never 1 like you said.
    Code:
        public void boostSetup(int boost, Player player, int duration){
            Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("Leveling_System");
            playerData data = new playerData();
    
            int current = data.getBoost();
            if(current == 1)
            {
                System.out.println("§etest");
                data.setBoost(boost);
                player.sendTitle("§d§l" + boost + "x Booster", "§8Applied to§e " + player, 1, 20, 1);
    
                Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
                    public void run(){
                    data.setBoost(1);
                    }
                }, 20L * duration); /* in seconds */
            }
            command.setPlayerData(player, data);
        }
    And I used the hashmap method i created before from the command class but still no bueno, rightclick is detected but boostsetup is not working
    And about the naming part, well alright i will be careful from now on
     
  14. Offline

    KarimAKL

    @Greeniousity This line:
    Code:Java
    1. playerData data = new playerData();

    is creating a new instance. This is where you'll have to retrieve the instance stored in the commands class from the join event. Otherwise, you'll keep creating a new instance with a #getBoost() value of 0. You also don't have to store it in the commands class again in this method if you modify the same instance. If you don't have any public way to retrieve it, you should create a method for that. Something like commands#getPlayerData(Player).
     
  15. Offline

    Greeniousity

  16. Offline

    KarimAKL

  17. Offline

    Greeniousity

    well its fixed i dont get the error anymore but now the right click once works and then poof nothing happens but it applied the boost once
     
  18. Offline

    KarimAKL

    @Greeniousity You're setting the boost value to 2 using #setBoost(int). After doing this once, the condition "current == 1" is no longer true; hence nothing happens.
     
  19. Offline

    Greeniousity

    but the delayed event sets it to 1 again after 2 minutes
     
  20. Offline

    KarimAKL

    @Greeniousity Are you waiting the full two minutes? Try printing a message to make sure when that happens.
     
  21. Offline

    Greeniousity

    Lol i think i found our issue when i open my inventory and click it, it works but if i right click it doesnt. How do i do that:)
     
  22. Offline

    KarimAKL

  23. Offline

    Greeniousity

    i did something before your reply, i changed the event to PlayerInteract Event and replaced getCurrentItem with getItem and removed the getWhoClicked's. It works well. I also had a minor issue that i was facing with this plugin but should i create a new thread for that?
     
  24. Offline

    KarimAKL

    Oh, I misunderstood your intention. I'm glad you got it working.
    If it's unrelated to this particular issue (right clicking an item), that would probably be preferable.
     
  25. Offline

    Greeniousity

    Alright then, thank you @KarimAKL and @timtower. I really appreciate both of your support:)
     
    KarimAKL and timtower like this.

Share This Page