Solved Player right click item event

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

Thread Status:
Not open for further replies.
  1. Offline

    Greeniousity

    Hello again bukkit, i am trying to do stuff when right clicking a pie with a specific name but it does nothing. No xp boost, there might also be a problem with the duration of the effect too. Here is the code

    https://paste.myst.rs/dfzi3a8i
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    Greeniousity

    I did now but still doesnt work
    Code:
        public boosters(Leveling_System plugin){
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
     
  4. Online

    timtower Administrator Administrator Moderator

    Then put a debug statement in the first line of the event.
     
  5. Offline

    Greeniousity

    neither the click event nor the boost setup works
     
  6. Online

    timtower Administrator Administrator Moderator

    So, you put a print statement as the very first line of the function?
     
  7. Offline

    Greeniousity

    not exactly, i placed it under the player check
    like the instanceof Player one
     
    Last edited: Apr 14, 2024
  8. Online

    timtower Administrator Administrator Moderator

    Put it above, see if it even triggers.

    And it is always a player
     
  9. Offline

    Greeniousity

    still nothing, and i check if its a player or not because it used to be a different event bu i changed it but i forgot to remove that check[skeleton]
     
  10. Offline

    KarimAKL

    Did you create an instance of this class ("boosters") upon the plugin being enabled (i.e. the onEnable method of your main class)?
    By the way, you should follow the Java naming conventions.
     
  11. Offline

    Greeniousity

    like registering events in the onEnable method?
     
  12. Offline

    KarimAKL

    @Greeniousity Yes, you're currently registering the listener instance in the constructor, but if you never create this instance, it will not be registered.
     
  13. Offline

    Greeniousity

    Well I already have listeners and the class is registered but its still not working
     
  14. Offline

    KarimAKL

    @Greeniousity Assuming you print a message to the console upon the event triggering and that messages isn't coming up (as mentioned earlier), something must have gone wrong with either the listener registration or the plugin initialization. Can you show your main class and the server startup log?
     
  15. Offline

    Greeniousity

    https://paste.myst.rs/rabofyg4 here is the main class
    and i found an error related to this plugin, a null pointer exception
    Code:
    [18:28:34 ERROR]: Could not pass event InventoryClickEvent to RenderiteSB v1.0-SNAPSHOT
    java.lang.NullPointerException: null
    at me.altug.levelingsystem.boosters.InventoryClick(boosters.java:54) ~[?:?]
    at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor36.execute(Unknown Source) ~[?:?]
    at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.16.5.jar:git-Paper-794]
    at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.16.5.jar:git-Paper-794]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.16.5.jar:git-Paper-794]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:624) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:2715) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:32) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$ensureMainThread$1(PlayerConnectionUtils.java:35) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:1271) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:1264) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:1225) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1139) ~[patched_1.16.5.jar:git-Paper-794]
    at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:291) ~[patched_1.16.5.jar:git-Paper-794]
    at java.lang.Thread.run(Thread.java:750) [?:1.8.0_402]
     
  16. Online

    timtower Administrator Administrator Moderator

    @Greeniousity Please post the boosters class as well, your last code does not even have 54 lines.
     
  17. Offline

    Greeniousity

    Code:
    package me.altug.levelingsystem;
    
    import me.altug.levelingsystem.data.playerData;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    
    
    public class boosters implements Listener {
    
        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);
                    }
                }, 20 * duration); /* in seconds */
            }
        }
    
        public static void pie(Player player)
        {
            ItemStack item = new ItemStack(Material.PUMPKIN_PIE);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("§6Booster Pie§8§l (§eI§8§l)");
            item.setItemMeta(meta);
            player.getInventory().addItem(item);
        }
        @EventHandler
        public 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.getType() == Material.PUMPKIN_PIE && item.getItemMeta().getDisplayName().equals("§6Booster Pie§8§l (§eI§8§l)")) {
                boostSetup(2, player, 120);
            }
            }
    
        }
    }
    
    
    
     
  18. Online

    timtower Administrator Administrator Moderator

    @Greeniousity There is no ItemMeta, there is no display name
     
  19. Offline

    Greeniousity

    what do you mean?
     
  20. Offline

    KarimAKL

    @Greeniousity InventoryClickEvent#getCurrentItem() is @Nullable; it returns null when there is no item at the current slot. Make sure you don't access any of its members if it's null; do a null check.
    This error confirms the successful registration of the listener. Now you just have to fix the potential errors that can occur within the code.
     
  21. Offline

    Greeniousity

    if (item != null && item.getType() == Material.PUMPKIN_PIE && item.getItemMeta().getDisplayName().equals("§6Booster Pie§8§l (§eI§8§l)"))
    I did this to make sure that its not null
     
  22. Online

    timtower Administrator Administrator Moderator

  23. Offline

    KarimAKL

    @timtower The ItemMeta#getDisplayName() method is annotated as @NotNull. I went to check the source code, and it should just return an empty string in case there's no display name. Additionally, ItemStack#getItemMeta() only returns null in case of an AIR material, which won't ever be the case here. Therefore, I reason this check should be valid. If you meant to refer to some other issue, please do elaborate.

    Yes, that is a valid null check. Do you still encounter any issues?
     
  24. Offline

    Greeniousity

    I updated it:

    Code:
            if (item.hasItemMeta() && item.getType() == Material.PUMPKIN_PIE && item.getItemMeta().getDisplayName().equals("§6Booster Pie§8§l (§eI§8§l)")) {
                boostSetup(2, player, 120);
    now it checks if item has meta and then checks the name

    I did, is it possible that all this is caused because pies are eatable?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    EDIT by me: I realised it but couldnt manage to merge them myself
     
    Last edited: Apr 15, 2024
  25. Offline

    KarimAKL

    @Greeniousity You reverted the change. You should still check if the item is (not) null. Add that back in (still as the first condition), then post the error here if it still persists.

    No, that is unrelated. You're simply checking if the item clicked in the inventory is a pie.

    This might not be my job to say as a regular user, but you cannot merge posts yourself; just let the bot handle it IF you ALREADY posted. The "EDIT by Moderator" is simply to let you know about the edit button and to avoid double posting.
     
  26. Online

    timtower Administrator Administrator Moderator

    No, think it got changed in the past years, was throwing errors years ago.
     
    KarimAKL likes this.
  27. Offline

    Greeniousity

    Oh wait its a bot? oh
     
  28. Offline

    Greeniousity

    Well its still not working after reverting to item != null, got no idea why
    Code:
    package me.altug.levelingsystem;
    
    import me.altug.levelingsystem.data.playerData;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    
    
    public class boosters implements Listener {
    
        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 */
            }
        }
    
        public static void pie(Player player)
        {
            ItemStack item = new ItemStack(Material.PUMPKIN_PIE);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("§6Booster Pie§8§l (§eI§8§l)");
            item.setItemMeta(meta);
            player.getInventory().addItem(item);
        }
        @EventHandler
        public 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 != null && item.getType() == Material.PUMPKIN_PIE && item.getItemMeta().getDisplayName().equals("§6Booster Pie§8§l (§eI§8§l)")) {
                boostSetup(2, player, 120);
            }
            }
    
        }
    }
    
    
    
    Code:
    package me.altug.levelingsystem;
    
    import me.altug.levelingsystem.data.playerData;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import java.io.File;
    import java.io.IOException;
    
    
    public final class Leveling_System extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            // Plugin startup logic
            saveResource("config.yml", false);
            createCustomConfig();
            getCommand("level").setExecutor(new command(this));
            Bukkit.getPluginManager().registerEvents(this, this);
            Bukkit.getPluginManager().registerEvents(new boosters(), this);
    
        }
    
        private void createCustomConfig()
        {
            File createDataFile = new File(getDataFolder(), "players.yml");
            if(!createDataFile.exists())
            {
                createDataFile.getParentFile().mkdirs();
                saveResource("players.yml", false);
            }
        }
    
        @Override
        public void onDisable() {
            saveConfig();
        }
        @EventHandler
        private void onJoinEvent(PlayerJoinEvent event)
        {
            playerData data = new playerData();
            File dat = new File(getDataFolder().getAbsolutePath() + "/players.yml");
            FileConfiguration conf = YamlConfiguration.loadConfiguration(dat);
            if(conf.contains("players." + event.getPlayer().getUniqueId().toString()))
            {
                data.setLevel(conf.getInt("players." + event.getPlayer().getUniqueId().toString() + ".main.level"));
                data.setReq(conf.getInt("players." + event.getPlayer().getUniqueId().toString() + ".main.required"));
                data.setExp(conf.getInt("players." + event.getPlayer().getUniqueId().toString() + ".main.exp"));
                data.setBoost(conf.getInt("players." + event.getPlayer().getUniqueId().toString() + ".main.boost"));
                System.out.println("§d(Created Data for) §e" + event.getPlayer());
                System.out.println("§dLevel " + data.getLevel());
                System.out.println("§dRequired " + data.getReq());
                System.out.println("§dExperience " + data.getExp());
                System.out.println("§dBoost " + data.getBoost());
            } else {
                data.setLevel(1);
                data.setReq(50);
                data.setExp(0);
                data.setBoost(1);
                System.out.println("§d(Loaded Data for) §e" + event.getPlayer());
                System.out.println("§dLevel " + data.getLevel());
                System.out.println("§dRequired " + data.getReq());
                System.out.println("§dExperience " + data.getExp());
                System.out.println("§dBoost " + data.getBoost());
    
            }
            command.setPlayerData(event.getPlayer(), data);
    
        }
        @EventHandler
        private void onQuit(PlayerQuitEvent event)
        {
            playerData data = command.getPlayerData(event.getPlayer());
            File dat = new File(getDataFolder().getAbsolutePath() + "/players.yml");
            FileConfiguration conf = YamlConfiguration.loadConfiguration(dat);
            conf.set("players." + event.getPlayer().getUniqueId().toString() + ".main.level", data.getLevel());
            conf.set("players." + event.getPlayer().getUniqueId().toString() + ".main.required", data.getReq());
            conf.set("players." + event.getPlayer().getUniqueId().toString() + ".main.exp", data.getExp());
            conf.set("players." + event.getPlayer().getUniqueId().toString() + ".main.boost", 1);
            try {
                conf.save(dat);
                System.out.println("§d(Quit save ran for) §e" + event.getPlayer());
                System.out.println("§dLevel " + data.getLevel());
                System.out.println("§dRequired " + data.getReq());
                System.out.println("§dExperience " + data.getExp());
                System.out.println("§dBoost " + data.getBoost());
            } catch
            (IOException e){
                e.printStackTrace();
            }
            command.resPlayerData(event.getPlayer());
        }
    
    }
    
     
  29. Online

    timtower Administrator Administrator Moderator

  30. Offline

    Greeniousity

Thread Status:
Not open for further replies.

Share This Page