Solved player inventory join event

Discussion in 'Plugin Development' started by BizeaxPvP, Jul 31, 2019.

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

    BizeaxPvP

    I want it whenever players join it opens an inventory.. i didnt get any errors so idk why it isn't opening the inventory.

    Event class:
    Code:
    package me.Zxoir.OnlyzAntiBot.EventClass;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    
    public class JoinEvent implements Listener{
       
              @EventHandler
              public void onJoin(PlayerJoinEvent event) {
                  Player player = event.getPlayer();
                  if(player.hasPlayedBefore()) {
                      Inventory inv = Bukkit.createInventory(null , 9, "test");
                      player.openInventory(inv);
                  }
              }
    
    }
    Main class:
    Code:
    package me.Zxoir.OnlyzAntiBot;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.Zxoir.OnlyzAntiBot.EventClass.JoinEvent;
    
    public class Main extends JavaPlugin{
        public void onEnable() {
           
            getServer().getConsoleSender().sendMessage("\nOnlyzAntiBot Enabled\n");
            getServer().getPluginManager().registerEvents(new JoinEvent(), this);
        }
        public void onDisable() {
            getServer().getConsoleSender().sendMessage("\nOnlyzAntiBot Disabled\n");
        }
    
    }
    
    
     
  2. Offline

    Machine Maker

    Huh, I literally copied your exact code into a brand new event, and it worked for me. Only thing I can think of is that your plugin.yml isn't setup correctly but that would give you errors I think.

    What version of the game are you using and what server type (aka Craftbukkit or Spigot)?
     
  3. Offline

    BizeaxPvP

    what haha. im using spigot 1.8.8

    are you sure it "worked"? so when you join it opened a GUI for you?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting. (sorry mod, forgot i posted that other one
     
    Last edited: Jul 31, 2019
  4. Offline

    Machine Maker

    Aha! So it was working fine in my 1.14.4. When I moved to 1.8.8, I get an error, now you said you don't get an error, but I think you need to delay opening the inventory by 1 tick. So just create a BukkitRunnable and use runTaskLater() with a 1 tick value for the delay.

    If you need to learn about Runnables/BukkitRunnables, read this.
     
  5. Offline

    BizeaxPvP

    i read what you sent and im still not familiar on how to do this with the join event? can you please show me a short example?
     
  6. Offline

    Machine Maker

    Yeah, so in the event handler you can create a new BukkitRunnable anonymously like this
    Code:java
    1.  
    2. new BukkitRunnable() {
    3.  
    4. @Override
    5. public void run() {
    6. // your code
    7. }
    8. }.runTaskLater(/*YourPluginInstance*/,/* Delay (Long) */);
    9.  
     
  7. Offline

    BizeaxPvP

    where would i use
    Code:
     public void onJoin(PlayerJoinEvent event) 
     
  8. Offline

    Machine Maker

    What I posted goes inside of the event handler (aka the method that is called when the event is triggered)
     
  9. Offline

    BizeaxPvP

    so like this? this toke me so long xD
    Code:
    public class JoinEvent implements Listener {{
      
            new BukkitRunnable() {
              
                public void run() {
                    @EventHandler
                    public void onJoin(PlayerJoinEvent event) {
                        Player player = event.getPlayer();
                        if(player.hasPlayedBefore()) {
                            Inventory inv = Bukkit.createInventory(null , 9, "test");
                            player.openInventory(inv);
                        }
              
                }
                }}.runTaskLater(this, 1);
          
          
            }
    }
    im getting error on () from (PlayerJoinEvent event) saying ; expected

    OR like this?
    Code:
    public class JoinEvent implements Listener {
       
               
                    @EventHandler
                    public void onJoin(PlayerJoinEvent event) {
                        Player player = event.getPlayer();
                        new BukkitRunnable() {
                             
                            @Override
                            public void run() {
                                if(player.hasPlayedBefore()) {
                                    Inventory inv = Bukkit.createInventory(null , 9, "test");
                                    player.openInventory(inv);
    
                            }
                        }
                        }.runTaskLater(this, 1);
               
                }
    
           
           
            }
     
    Last edited: Jul 31, 2019
  10. Offline

    timtower Administrator Administrator Moderator

    @BizeaxPvP You need a function. You don't have the event in the runnable.
     
  11. Offline

    BizeaxPvP

    edited, i got a error at run task later
     
  12. Offline

    timtower Administrator Administrator Moderator

  13. Offline

    BizeaxPvP

    i did... but here ill post it again.
    Code:
    package me.Zxoir.OnlyzAntiBot.EventClass;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class JoinEvent implements Listener {
     
             
                    @EventHandler
                    public void onJoin(PlayerJoinEvent event) {
                        Player player = event.getPlayer();
                        new BukkitRunnable() {
                           
                            @Override
                            public void run() {
                                if(player.hasPlayedBefore()) {
                                    Inventory inv = Bukkit.createInventory(null , 9, "test");
                                    player.openInventory(inv);
    
                            }
                        }
                        }.runTaskLater(this, 1);
             
                }
    
         
         
            }
    
    
     
  14. Offline

    timtower Administrator Administrator Moderator

    @BizeaxPvP plugin = JoinEvent() won't work.
    You need a plugin instance there.
    Probably gotten there with a constructor.
     
  15. Offline

    Machine Maker

    @BizeaxPvP To expand on what timtower said, you need to pass runTaskLater the instance of the class that extends JavaPlugin (aka your main class). Passing "this" passes the instance of the Listener which isnt the same thing.

    You probably need to add a constructor to your Listener that takes a plugin instance as a parameter.
    Take a look at this.
     
  16. Offline

    BizeaxPvP

    so is this correct? but i got an error in main class in registering the event saying the constructor is undefined
    event class:
    Code:
     
    package me.Zxoir.OnlyzAntiBot.EventClass;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class JoinEvent implements Listener {
        private Plugin plugin;
         
        public JoinEvent(Plugin plugin) {
        this.plugin = plugin;
        }
               
                    @EventHandler
                    public void onJoin(PlayerJoinEvent event) {
                        Player player = event.getPlayer();
                        new BukkitRunnable() {
                             
                            @Override
                            public void run() {
                                if(player.hasPlayedBefore()) {
                                    Inventory inv = Bukkit.createInventory(null , 9, "test");
                                    player.openInventory(inv);
    
                            }
                        }
                        }.runTaskLater(plugin, 1);
               
                }
    
           
           
            }
    
    
    Main:
    Code:
     
    package me.Zxoir.OnlyzAntiBot;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.Zxoir.OnlyzAntiBot.EventClass.JoinEvent;
    
    public class Main extends JavaPlugin{
        public void onEnable() {
           
            getServer().getConsoleSender().sendMessage("\nOnlyzAntiBot Enabled\n");
            getServer().getPluginManager().registerEvents(new JoinEvent(), this);
        }
        public void onDisable() {
            getServer().getConsoleSender().sendMessage("\nOnlyzAntiBot Disabled\n");
        }
    
    }
    
    
    i did that, just having problems with my main. i get an error when i wanna register
    Code:
        getServer().getPluginManager().registerEvents(new JoinEvent(), this); 
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 31, 2019
  17. Offline

    Machine Maker

    Well, you need to pass the plugin to the constructor now when you create the new instance of JoinEvent

    Code:java
    1. new JoinEvent(this)

    is what it should read when you register the events in the main class.

    In the constructor in the event class you have a required argument, the plugin, so you have to pass the plugin to it when you construct it
     
  18. Offline

    BizeaxPvP

    w
    FINALLY worked!! tysm guys
     
  19. Offline

    Machine Maker

    Awesome!
    Please mark this thread as Solved. You can do so by clicking the Thread Tools link at the top of the thread.
     
Thread Status:
Not open for further replies.

Share This Page