2 Class Files

Discussion in 'Plugin Development' started by Linux1337, Aug 24, 2012.

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

    Linux1337

    Hi Guys, I have 2 class files in my plugin and i need to know how to make the connect the 2 class files

    Heres the main class:

    Code:
    package com.plugin.checkinv;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.Listener;
    
    public class CheckInv extends JavaPlugin{
        public void onEnable() {
    
            this.getServer().getPluginManager().registerEvents((Listener) new CheckInv(), this);
            getLogger().info("CheckInv has been enabled!");
        }
        
        public void onDisable() {
            getLogger().info("CheckInv has been disabled!");
        }
        
    }
    
    And Secondary Calss:
    Code:
    package com.plugin.checkinv;
    
    import org.bukkit.Material; 
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class CheckInvMain extends JavaPlugin implements Listener{
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerJoin(PlayerJoinEvent evt) {
            Player player = evt.getPlayer();
            PlayerInventory inventory = player.getInventory();
                
            if (inventory.contains(Material.BEDROCK)) {
                inventory.remove(Material.BEDROCK);
                player.sendMessage("Why do you have bedrock???");
            }
        }
    
    
    And plugin.yml:
    Code:
    name: CheckInv
    main: com.plugin.checkinv.CheckInv
    version: 1.0.7
    
    What other code do i have to put in? Thx in advance!
     
  2. Offline

    sternmin8or

    change: this.getServer().getPluginManager().registerEvents((Listener) new CheckInv(), this);
    to: this.getServer().getPluginManager().registerEvents(this, this);
    change:public class CheckInv extends JavaPlugin{
    to:public class CheckInv extends JavaPlugin implements Listener{


    and simply copy your code :
    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerJoin(PlayerJoinEvent evt) {
    Player player = evt.getPlayer();
    PlayerInventory inventory = player.getInventory();

    if (inventory.contains(Material.BEDROCK)) {
    inventory.remove(Material.BEDROCK);
    player.sendMessage("Why do you have bedrock???");
    }
    }

    over to your main class file
     
  3. Offline

    Linux1337

    ok i guess...

    Any way to make it tho? Since i will be developing larger plugins in the future.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  4. Offline

    sternmin8or

    I dont quite understand what you want to do. Didnt oyu want to consolidate those into one file?
     
  5. Offline

    keensta

    Yes he did. So he didn't answer your question.

    I suggest changing your main class CheckInv to this (Main contain bugs just typing this off top my head *Easy to fix ones*)
    Code:
    package com.plugin.checkinv;
     
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.Listener;
     
    public class CheckInv extends JavaPlugin{
    @SuppressWarnings("unused")
        private CheckInvMain Checkinvmain = null;
        public void onEnable() {
            getLogger().info("CheckInv has been enabled!");
        Checkinvmain = CheckInvMain(this);
    }
       
        public void onDisable() {
            getLogger().info("CheckInv has been disabled!");
        }
       
    }
    I made it call the CheckInvMain if you notice i removed the registar events I moved it into CheckInvMain Better that way (In my eyes) May not be but someone may correct me they may not. :p

    Now for the second class
    Code:
    package com.plugin.checkinv;
     
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class CheckInvMain implements Listener{
     
    public CheckInv plugin;
     
    public CheckInvMain (CheckInv plugin) {
            this.plugin = plugin;
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerJoin(PlayerJoinEvent evt) {
            Player player = evt.getPlayer();
            PlayerInventory inventory = player.getInventory();
               
            if (inventory.contains(Material.BEDROCK)) {
                inventory.remove(Material.BEDROCK);
                player.sendMessage("Why do you have bedrock???");
            }
        }
    
    Here what i have done is made the "public CheckInv plugin;" This just calls the CheckInv then i also added a constructor which in there registars the events.

    Hope this helped any questions just ask.
     
Thread Status:
Not open for further replies.

Share This Page