Help a noob

Discussion in 'Spigot Plugin Help' started by cjmaciaz, May 2, 2017.

  1. First time here so maybe this is just a stupid question but...
    How can I run this as Static?

    public void main (String args[]){
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }

    (I need to make it Static so i can call it from Main code)
     
  2. public static void main (String args[])
    {
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }
     
  3. This what your looking for? xD
     
    • Like Like x 1
  4. Yeah, I tried that :p but get a error when making it Static.
    Trying to call that code from another code (?)
     

    Attached Files:

    • 123.png
      123.png
      File size:
      14.3 KB
      Views:
      107
  5. Use public void onEnable()
     
    • Agree Agree x 1
  6. Here is my Main Class:

    Code (Text):

    public class Main extends JavaPlugin implements  Listener{

        private static Plugin plugin;

        @Override
        public void onEnable(){ /**shit server does when plugin starts**/

            plugin =this;
            getServer().getPluginManager().registerEvents(new Lvl(), this);

        }

        public void onDisable(){ /**shit server does when plugin stops**/
        }
    }
     
    And im trying to load this one through it.

    Code (Text):
    public class Lvl extends JavaPlugin implements Listener {

        public void onEnable (String args[])
        {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
        int lvl = 0;
        @EventHandler
        public void PlayerDeath(final PlayerDeathEvent deathEvent) { /**when player dies...**/
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                @Override
                public void run() {
                    Player p = deathEvent.getEntity().getPlayer();
                    lvl = p.getLevel(); /** stores player level in "lvl".**/
                }
            });
        }
        @EventHandler
        public void PlayerRespawn(final PlayerRespawnEvent respawnEvent) { /**when player respawn...**/
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                @Override
                public void run() {
                    Player p = respawnEvent.getPlayer();
                    p.setLevel(lvl); /** sets stored player level.**/
                }
            });
        }

    }

     
    It works fine when in the Main class (its supposed to make you don't lose your exp lvls when you die)
    But I get this error:

    Code (Text):

    java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:67) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at mac.lvlz.Lvl.<init>(Lvl.java:14) ~[?:?]
            at mac.lvlz.Main.onEnable(Main.java:19) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:376) [spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:326) [spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at net.minecraft.server.v1_11_R1.MinecraftServer.t(MinecraftServer.java:421) [spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at net.minecraft.server.v1_11_R1.MinecraftServer.l(MinecraftServer.java:382) [spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at net.minecraft.server.v1_11_R1.MinecraftServer.a(MinecraftServer.java:337) [spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at net.minecraft.server.v1_11_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:544) [spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:67) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at mac.lvlz.Main.<init>(Main.java:11) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_131]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_131]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_131]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_131]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_131]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.loadPlugins(CraftServer.java:301) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            at net.minecraft.server.v1_11_R1.DedicatedServer.init(DedicatedServer.java:204) ~[spigot.jar:git-Spigot-3fb9445-fecf4b4]
            ... 2 more
     
  7. Why you use "String[] args[]"
     
    • Informative Informative x 1
  8. Remove extends javaplugin from the Lvl class
     
    • Agree Agree x 1
  9. It works now (Thanks for the help)
    But I cant make it reset the level when you respawn.

    Code (Text):
    public final class Lvl implements Listener {
        public Lvl(Main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
        int plvl;
     
        @EventHandler
        public void PlayerDeath( PlayerDeathEvent deathEvent) {             /**when player dies...**/
            Player p = deathEvent.getEntity().getPlayer();
            p.sendMessage(ChatColor.RED + "YOU DIED");
            plvl = p.getLevel();                                            /** stores player level in "lvl".**/
        }

        @EventHandler
        public void PlayerRespawn( PlayerRespawnEvent respawnEvent) {       /**when player respawn...**/
            Player p = respawnEvent.getPlayer();
            p.sendMessage(ChatColor.GREEN + " respawn message ");
            p.setLevel(plvl);                                               /** sets stored player level.**/
        }

    }
    Chat Messages work tho.
     
  10. When writing multi-line comments, use only one asterisk, two symbolise Javadocs