Multiple Classes That Extend JavaPlugin?

Discussion in 'Plugin Development' started by MCJoshua345, Feb 12, 2015.

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

    MCJoshua345

    Hello! Today I'm here with a little problem. I need another class that extends JavaPlugin. But, when I put it in my PluginManager, save, export etc. It comes up with a error. I know that the only way to fix this is to find a solution that kinda connects the main class with the other class. First, some clearups
    Java Knowledge: Not much, more of a Python coder.
    Bukkit Code Knowledge: Pretty High, have developed 10 decent plugins, some in dev to be released, and some probably never to be released and will stay private or used on my server. So now that we have that cleared, thanks for the help and see ya later alligator! (GAY, srry :()
     
  2. Offline

    _Filip

    Please refrain from displaying homophobic behavior on this forum.
    Now that we have that cleared up, you cannot extend JavaPlugin in more than 1 class.
     
  3. Offline

    adam753

    You can only have one class that extends JavaPlugin. The fact that you want another class to magically have all the features of the first one tells me that you don't know how to properly achieve what you want to do.
     
  4. Offline

    CubieX

    Sadly, this is the main problem of most plugin makers here...
    Knowing an API is useless without decent knowledge of the language. One will never be able to solve problems himself this way.

    If you explain in detail what you want to achieve, we probably can help you.
     
  5. Offline

    hexaan

    @MCJoshua345

    If you need an other class that extends JavaPlugin then you might want to rethink your design. You should be passing the only instance of the JavaPlugin to other classes that need to use it.
     
  6. Offline

    MCJoshua345

    Hmm. Okay.
    Here's the main startup... :
    Code:
    package com.infinity.lobby.main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.infinity.lobby.beta.currency.Currency;
    import com.infinity.lobby.commands.CommunicationCommands;
    import com.infinity.lobby.commands.CustomHelpCommand;
    import com.infinity.lobby.commands.FlyCommand;
    import com.infinity.lobby.commands.LobbyComand;
    import com.infinity.lobby.commands.NicknameCommand;
    import com.infinity.lobby.commands.RankListCommand;
    import com.infinity.lobby.commands.SpeedCommand;
    import com.infinity.lobby.commands.TutorialCommand;
    import com.infinity.lobby.listeners.FallListener;
    import com.infinity.lobby.listeners.OnlineStatus;
    import com.infinity.lobby.listeners.PlayerListener;
    import com.infinity.lobby.listeners.SideBarScoreboard;
    import com.infinity.lobby.permissions.BlockPerms;
    import com.infinity.lobby.permissions.PrefixPerms;
    
    public class Main extends JavaPlugin{
    
      public static Main instance;
    
      public void log(String string) {
          System.out.println(string);
       }
     
       public static Main getInstance(){
           return instance;
       }
      
       public void registerCommands(){
           getCommand("broadcast").setExecutor(new CommunicationCommands(this));
           getCommand("help").setExecutor(new CustomHelpCommand(this));
           getCommand("?").setExecutor(new CustomHelpCommand(this));
           getCommand("fly").setExecutor(new FlyCommand(this));
           getCommand("lobby").setExecutor(new LobbyComand(this));
           getCommand("nickname").setExecutor(new NicknameCommand(this));
           getCommand("ranks").setExecutor(new RankListCommand(this));
           getCommand("speed").setExecutor(new SpeedCommand(this));
           getCommand("tutorial").setExecutor(new TutorialCommand(this));
       }
      
       public void registerListeners() {
           PluginManager pm = getServer().getPluginManager();
           //pm.registerEvents(new Pop(this), this);
           //pm.registerEvents(new Cosmetics(this), this);
           //pm.registerEvents(new ServerStories(this), this);
           //pm.registerEvents(new Backup(this), this);
           pm.registerEvents(new Currency(this), this);
           pm.registerEvents(new OnlineStatus(this), this);
           pm.registerEvents(new PlayerListener(this), this);
           pm.registerEvents(new SideBarScoreboard(this), this);
           pm.registerEvents(new BlockPerms(this), this);
           pm.registerEvents(new PrefixPerms(this), this);
           pm.registerEvents(new FallListener(this), this);
           pm.registerEvents(new CommunicationCommands(this), this);
           pm.registerEvents(new CustomHelpCommand(this), this);
           pm.registerEvents(new FlyCommand(this), this);
           pm.registerEvents(new LobbyComand(this), this);
           pm.registerEvents(new NicknameCommand(this), this);
           pm.registerEvents(new RankListCommand(this), this);
           pm.registerEvents(new SpeedCommand(this), this);
           pm.registerEvents(new TutorialCommand(this), this);
       }
      
       //When Plugin Enables
       @Override
       public void onEnable(){
           getLogger().info(" has been successfully enabled without errors! Congratulations, no error fixing for you!");
           instance = this;
           registerListeners();
           registerCommands();
           getConfig().options().copyDefaults(true);
           saveDefaultConfig();
       }
      
       //When Plugin Disables
       @Override
       public void onDisable(){
           getLogger().info(" has been successfully disabled without errors! Have a nice day!");
           saveDefaultConfig();
           instance = null;
       }
    And I don't want to put Main.instance. Before everything like getConfig(), which is why I want to extend from JavaPlugin again. Here it is:
    Code:
    package com.infinity.lobby.beta.currency;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Pop implements Listener
    {
      private ArrayList<Player> justpopped = new ArrayList<Player>();
     
      public void onEnable()
      {
        getConfig().options().copyDefaults(true);
        saveDefaultConfig();
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
      }
     
      public void onDisable()
      {
        saveDefaultConfig();
      }
     
      @EventHandler
      public void onEntityDamage(EntityDamageByEntityEvent e)
      {
        int time = getConfig().getInt("time");
        final Player popped = (Player)e.getEntity();
        final Player popper = (Player)e.getDamager();
        if ((!(e.getEntity() instanceof Player)) ||
          ((e.getDamager() instanceof Player))) {}
        List<String> enabledWorlds = Arrays.asList(new String[] { getConfig().getString("world") });
        if (!enabledWorlds.contains(popped.getWorld().getName())) {
          return;
        }
        popper.hidePlayer(popped);
        popper.playSound(popper.getLocation(), Sound.CHICKEN_EGG_POP, 1.0F, 1.0F);
        popper.getWorld().playEffect(popped.getLocation(), Effect.MOBSPAWNER_FLAMES, 1, 1000);
        popper.getWorld().playEffect(popped.getLocation(), Effect.SMOKE, 1, 1000);
        popper.sendMessage("" + ChatColor.GREEN + "You have been given " + ChatColor.GOLD + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.BLUE + " Credit " + ChatColor.GREEN + "for popping " + ChatColor.AQUA + ChatColor.BOLD + popped + ChatColor.RESET + ChatColor.GREEN + "!");
        this.justpopped.add(popped);
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
        {
          public void run()
          {
            Pop.this.justpopped.remove(popped);
            popped.showPlayer(popper);
          }
        }, time);
      }
    }
    Everything that has to do with the config (and the scheduleSyncDelayedTask())
    Is errored. It wants it to let Pop implement plugin, and wants to make a method for getConfig(), which the method is in JavaPlugin.
     
  7. Offline

    Skionz

    @MCJoshua345 Pass your main class through a constructor. If you don't want to type 'yourInstance.getConfig()' each time write a private method that wraps it.
     
  8. Offline

    adam753

    @MCJoshua345
    You can only have one JavaPlugin instance active. If you try to make a different class or instance that extends JavaPlugin, even if it worked, it would throw an error when you tried to use any method like getConfig because Bukkit makes an instance of your main class and uses that and only that.

    tl;dr: Just put Main.instance before everything, seriously.
     
  9. Offline

    Unica

    @MCJoshua345

    Or make a constructor and don't use a static instance.

    EDIT: @Skionz already mentioned that
     
  10. Offline

    MCJoshua345

    Okay, I'll try to look into that. Thanks to:
    @Skionz
    Notes to:
    @adam753 :
    I tried to put Main.instance before everything that required JavaPlugin, and most of it worked... except this line of code, which is the task:
    Code:
    Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable()
    //NOT COMPLETE CODE
    //Got trouble with this part scheduleAsyncDelayedTask
    I guess since JavaPlugin includes Plugin, which is where it knows what to do with that line of code, it took care of that incident. Even when I tried to implement Plugin it said crap about making the class abstract and stuff or adding a bunch of Auto-Generated methods.
    @CubieX :
    Sorry for the noob pain *sniff sniff* :'(. LOOOOOLZ!
    :D:D:D:D:D:D:D:D:D:D
    @_Filip :
    I was joking, nothing gay was in there. -_-
    @_Filip , @adam753 , @hexaan :
    Sorry, soooo stupid that I thought you could have 2 classes that extend JavaPlugin. You must be like :mad:
    Okay, now that we have that cleared up and everything, thanks for the help everyone and cya guyz later!

    .
     
  11. Offline

    Skionz

     
Thread Status:
Not open for further replies.

Share This Page