Solved Calling a method in a different class/package

Discussion in 'Spigot Plugin Development' started by Qruet, Jun 9, 2017.

Thread Status:
Not open for further replies.
  1. I understand how to call other classes in the same package, and I've figured it's just as easy to call it in a different package as long as I make the method in the class in the different package static. However now I've run into a problem where in a certain class can't be static since I'm trying to run a bukkit runnable to test to see when a certain entity has touched the ground after they've been launched up in the air. I tried calling the method like I would for any other non static method for a different class, however it doesn't appear to call that method at all. I'm not sure exactly what I should do differently to call a method in a class that isn't static, and isn't in the same package from where I'm calling the class from.
    ~Geekles
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    You may want to learn a bit more Java; it looks like you may not fully understand how static and non-static classes work.

    Basically, though, static means any class can use that same method, and that class will be the same (static). Non-static will allow you to have objects out of the class. For example, if the Bukkit Player class was static, how would it know which username to return if you used Player.getName()? As an object, it has it's own set of data.

    In your case, you probably want to save the reference to the Runnable somewhere, so you can access it later and call non-static methods.
     
    • Agree Agree x 2
  3. Packages don't really make any difference if your methods are public. It doesn't need to be static, and probably shouldn't be- just have an instance of whatever class you're trying to call. You can use variables directly in a runnable if you mark them "final".
     
  4. I've Attempted to use non static methods at first since I've learned that it's best to stay away from using static, I'll admit I never went too indepth in the past with static since I've never found the need for them, however usually when I create an instance of a class and then call a public method from the class it works fine. But in this case, I'm having trouble calling the method from the class in the different package.

    Code (Text):
        private pixie.abyssalcraft.us.powers.Blast blast;
        public Listeners(pixie.abyssalcraft.us.powers.Blast blast){
            this.blast = blast;
            }
     
    //Stuff
                    if(loops.random(2) == true){
                        Bukkit.broadcastMessage("true");
                        blast.blast(pixie1);
                        ActionBarAPI.sendActionBar(p1, ChatColor.AQUA + "Your pixie pet is using their secret power, Blast!" ,(20*5));
                    }
                    else{
                        Bukkit.broadcastMessage("false");
                    }
    Code (Text):

    public class Blast extends JavaPlugin{
     
        private Entity e;
     
        public void blast(Entity entity){
            e = entity;
              entity.getWorld().spawnParticle(Particle.EXPLOSION_NORMAL, entity.getLocation(), 4, 0.01, 0.01, 0.01, 0.01);
            entity.getWorld().playSound(entity.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 6, 1);
                Vector dir = entity.getLocation().getDirection();
              Vector vec = new Vector(dir.getX()*0.1, 2.0, dir.getZ()*0.1);
              entity.setVelocity(vec);
              entity.setFallDistance(-1);
              checkforlanding();
           
        }
     
        int timer;
        public void checkforlanding(){
        timer = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
           public void run() {
                  if(e.isOnGround() == true){
                      Bukkit.getScheduler().cancelTask(timer);
                      //Stuff
                  }
         
           }
        }, 0, 1);

        }
     
    }
       
    When this is run, nothing happens. When the blast method is static, it runs.
     
  5. What's your problem with the code posted?
     
  6. Doesn't run the method that I called....
    Code (Text):
                    if(loops.random(2) == true){
                        Bukkit.broadcastMessage("true");
                        blast.blast(pixie1);
                        ActionBarAPI.sendActionBar(p1, ChatColor.AQUA + "Your pixie pet is using their secret power, Blast!" ,(20*5));
                    }
     
  7. Okay, so loops#random isn't returning true then.
     
  8. It returns true, I have it broadcast true or false. 1/2 of the time it returns true, then broadcasts "true" into chat. After that it doesn't do anything. It should send an ActionBar message (Which it use to before I needed to make the method nonstatic so that I could run a bukkit runnable method).
     
  9. Majority of methods do not need to be static.


    Sent from my iPhone using Tapatalk
     
  10. I understand that,
     
  11. Could be the api not working then. Check your console for errors (on startup too). Also debug inside the blaster code so that you can see what is being executed (I would assume it's executing that part fine).
     
  12. I'm working on now trying to change all the other classes that are using static methods to simply use non static methods. Again, when I remove static from another class that is in another package, I get issues when I click the snow ball item.

    Main Class:
    Code (Text):
    import pixie.abyssalcraft.us.pets.Cloud;

      public void Cloud(Player player){
            Cloud cloud = new Cloud(); //constructor
            cloud.createPet(player);
        }
    Cloud Class:
    Code (Text):
    public class Cloud extends JavaPlugin {

        public void createPet(Player player){
       
             //Stuff
               
        }
    }
     
    GUI Class:
    Code (Text):
        Main plugin;
        public GUI(Main plugin) {
            this.plugin = plugin;
        }


        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked(); // The player that clicked the item
        ItemStack clicked = event.getCurrentItem(); // The item that was clicked
        Inventory inventory = event.getInventory(); // The inventory that was clicked in
        if (inventory.getName().equals(plugin.myInventory.getName())) {
                if (clicked.getType() == Material.SNOW_BALL) {
                    event.setCancelled(true);
                    player.closeInventory();

                        if(plugin.withdraw(player, 100) == true){
                            plugin.Cloud(player);
                            player.sendMessage(ChatColor.GREEN + "Successfully purchased pixie pet, Cloudy!");
                    }
                        else{
                            player.sendMessage(ChatColor.RED + "You can't afford that!");
                        }
                }
    Code (Text):
    10.06 17:40:44 [Server] ERROR Could not pass event InventoryClickEvent to PixiePet v1.0
    10.06 17:40:44 [Server] INFO org.bukkit.event.EventException
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1849) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_111]
    10.06 17:40:44 [Server] INFO Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:67) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at pixie.abyssalcraft.us.pets.Cloud.<init>(Cloud.java:10) ~[?:?]
    10.06 17:40:44 [Server] INFO at pixie.abyssalcraft.us.Main.Cloud(Main.java:167) ~[?:?]
    10.06 17:40:44 [Server] INFO at pixie.abyssalcraft.us.GUI.onInventoryClick(GUI.java:40) ~[?:?]
    10.06 17:40:44 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO ... 15 more
    10.06 17:40:44 [Server] INFO Caused by: java.lang.IllegalStateException: Initial initialization
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:67) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at pixie.abyssalcraft.us.Main.<init>(Main.java:27) ~[?:?]
    10.06 17:40:44 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_111]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.craftbukkit.v1_11_R1.CraftServer.loadPlugins(CraftServer.java:301) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.craftbukkit.v1_11_R1.CraftServer.reload(CraftServer.java:748) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.Bukkit.reload(Bukkit.java:540) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchServerCommand(CraftServer.java:636) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.DedicatedServer.aM(DedicatedServer.java:437) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:400) ~[spigot-1.11.2-20170429.jar:git-Spigot-28463de-a2af8f0]
    10.06 17:40:44 [Server] INFO ... 3 more
     
    #12 Qruet, Jun 10, 2017
    Last edited: Jun 10, 2017
  13. *faceslap*
    Well, embarrassingly I realized that there is no point to extend JavaPlugin. Once removing the extension from the Cloud class, the method was successfully called.
    ~Geekles
     
  14. Lmao. If you posted a stack trace this wouldve been solved a while back ;p
     
  15. I'd recommend just moving your #Cloud method to being Cloud's constructor.
     
Thread Status:
Not open for further replies.