Running some code after an event

Discussion in 'Plugin Development' started by InnohVateur, Oct 21, 2022.

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

    InnohVateur

    Hello !
    I just began plugin development today and I'm trying to execute some code after an event :
    Code:
    public class GunEvents implements Listener {
        @EventHandler
        public void onRightClickWithGun(PlayerInteractEvent evt){
            Player p = evt.getPlayer();
            ItemStack itemInHand = evt.getItem();
            if(itemInHand.getType()== Material.IRON_HORSE_ARMOR){
                ItemStack goldGun = itemInHand;
                goldGun.setType(Material.GOLDEN_HORSE_ARMOR);
                p.getInventory().setItemInMainHand(goldGun);
            }
            new BukkitRunnable(){
                @Override
                public void run() {
                    p.getInventory().setItemInMainHand(itemInHand);
                }
            }.runTaskLater(SandboxPlugin.getPlugin(SandboxPlugin.class), 1L);
        }
    }
    Any suggest ? I already found some threads about BukkitRunnable, but it doesn't work and I don't know if I use it properly...
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    InnohVateur

    Yes the event runs. The only part that doesn't run is the bukkitrunnable
     
  4. Offline

    timtower Administrator Administrator Moderator

    Can you add a println to it and see what it does?
    Anything in the server log?
     
  5. Offline

    Kars

    It's probably this
    PHP:
    SandboxPlugin.getPlugin(SandboxPlugin.class)
    Try passing your main SandboxPlugin class into your eventhandler and accessing it that way.

    I could be wrong.
     
  6. Offline

    InnohVateur

    Yep it logged the println in the bukkitrunnable.
    Means that the problem is my p.getInventory().setItemInMainHand(itemInHand)...
    tysm !
     
Thread Status:
Not open for further replies.

Share This Page