Troubles With Making Custom Methods

Discussion in 'Plugin Development' started by mkezar, Jun 30, 2015.

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

    mkezar

    Hi. Im trying to make a custom method that I can use in my other projects. I successfully made the method, and I added it to one of my projects. I defined it and used it. But when I tried to use it, it told me that it should be accessed in a static way.

    Heres my custom method class:
    Code:
    package plugins.mkezar;
    import org.bukkit.entity.Damageable;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    
    
    public class AbilityHeal {
       
        public AbilityHeal() {
           
        }
        public static void start(Player player) {
           
            for(Entity entity : player.getNearbyEntities(20, 20, 20)){
                Damageable damageable = (Damageable) entity;
               
                damageable.setHealth(damageable.getHealth() +5);
            }
        }
    }
    
    And here is what im trying to do.
    Code:
    AbilityHeal heal = new AbilityHeal();
                     heal.start(player.getPlayer());
    But heal.start(player.getPlayer()); is giving me one of those yellow squiggly lines. It tells me it should be accessed in a static way like i mentioned before.

    I dont know what im doing wrong. Please help. :(
     
  2. You should really learn java because this is a very easy to fix thing, and this isn't even a bukkit question, more on the java side. Basicly the 'static' modifier makes a method accessible without creating an instance of the class. You made the start method static, which makes it unable to be reached from the instance. You should either make it non-static or access the method through a static way.
     
  3. You should learn java too xD
    Because instance (non-static) can access static but static can't access non-static
     
  4. @FisheyLP
    Yeh I kinda said that wrong, it's accessible but it's not the correct way to use it.
     
  5. Offline

    mine-care

    Static is a keyword making a method or field belong to the class so it can be acessed without the creation of an object of the declaring class :/

    As mentioned above, having at least the basic knowledge of Java is requiered for Bukkit Plugin Development :- )

    For reference, i recomend using the Oracle Documentation and tutorials, as well as TutorialsPoint.com
     
  6. Offline

    mkezar

    @tytwining So i removed the static modifier. I got this giant error now whenever i try running it.
    Stacktrace (open)

    [10:06:38 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'mwspider' in plugin MegaWallsKits v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:624) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1058) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:919) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:643) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:598) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NoClassDefFoundError: plugins/mkezar/AbilityHeal
    at plugins.mkezar.Kits.onCommand(Kits.java:105) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    ... 14 more
    Caused by: java.lang.ClassNotFoundException: plugins.mkezar.AbilityHeal
    at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_31]
    at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_31]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_31]
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_31]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:77) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:62) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_31]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_31]
    at plugins.mkezar.Kits.onCommand(Kits.java:105) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
    ... 14 more
     
    Last edited: Jun 30, 2015
  7. Offline

    tytwining

    Eclipse warns you about that. It should be pretty easy to learn that.
     
  8. Offline

    mkezar

    anyone? please?
     
  9. @mkezar Show your updated code.
     
  10. Offline

    mkezar

    Custom Method @CodePlaysMinecraft:
    Code:
    package plugins.mkezar;
    import org.bukkit.entity.Damageable;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    
    
    public class AbilityHeal {
       
        public AbilityHeal() {
           
        }
        public void start(Player player) {
           
            for(Entity entity : player.getNearbyEntities(20, 20, 20)){
                Damageable damageable = (Damageable) entity;
                damageable.setHealth(damageable.getHealth() +5);
            }
        }
    }
    
    Class im trying to use it in:
    Code:
    if(commandLabel.equalsIgnoreCase("mwspider")) {
               
                if(sender instanceof Player) {
                   
                     player.getPlayer().setVelocity(player.getPlayer().getLocation().getDirection().multiply(5));
                     player.getPlayer().setVelocity(new Vector(player.getPlayer().getVelocity().getX(), 1.0D, player.getPlayer().getVelocity().getZ()));
                    
                     player.playSound(player.getLocation(),Sound.SPIDER_DEATH,2,20);
                     AbilityHeal heal = new AbilityHeal();
                     heal.start(player.getPlayer());
                   
               
           
                }
           
            }
     
  11. Are you compiling the plugin correctly? Does it contain the AbilityHeal class and are you importing the correct class?

    Also you don't need to do "player.getPlayer()", you can just use "player".

    Edit:

    You might want to check if the entity is an instanceof Damageable by the way, and check its max health.
    Code:
        for (Entity entity : player.getNearbyEntities(20, 20, 20)) {
            if (entity instanceof Damageable) {
                Damageable damageable = (Damageable) entity;
                damageable.setHealth(Math.min(damageable.getMaxHealth(), damgeable.getHealth() + 5D));
            }
        }
    
     
  12. Offline

    Ruptur

    @mkezar
    Sometimes all it takes is a quick google search

    Java provides a number of non-access modifiers to achieve many other functionality.

    • The static modifier for creating class methods and variables
    Static Variables:
    The static key word is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.

    Static variables are also known as class variables. Local variables cannot be declared static.

    Static Methods:
    The static key word is used to create methods that will exist independently of any instances created for the class.

    Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables.

    Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method.

    Example:
    The static modifier is used to create class methods and variables, as in the following example:
    Code:
    public class InstanceCounter {
    
       private static int numInstances = 0;
    
       protected static int getCount() {
          return numInstances;
       }
    
       private static void addInstance() {
          numInstances++;
       }
    
       InstanceCounter() {
          InstanceCounter.addInstance();
       }
    
       public static void main(String[] arguments) {
          System.out.println("Starting with " +
          InstanceCounter.getCount() + " instances");
          for (int i = 0; i < 500; ++i){
             new InstanceCounter();
         }
          System.out.println("Created " +
          InstanceCounter.getCount() + " instances");
       }
    }
    
    This would produce the following result:
    Code:
    Startedwith0 instances
    Created500 instances
    
    References: http://www.tutorialspoint.com/java/java_nonaccess_modifiers.htm
     
  13. @Ruptur
    That part was already solved...
    @mkezar
    You're using a method from a so called "library" you created, but for it to work you need to put both the library/plugin ánd your plugin in the server, else it gives a ClassNotFoundException because the project the class is in isn't loaded.
     
  14. Offline

    mkezar

    It has always been in my plugins folder. It isnt a JavaPlugin because Libraries cant be JavaPlugins. and because it isn't a JavaPlugin, I get this error:
    Code:
    [17:26:23 ERROR]: Could not load 'plugins\AbilityLib.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: Cannot find main class `plugins.mkezar.AbilityLib'
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:42) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugins(CraftServer.java:287) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.<init>(CraftServer.java:249) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at net.minecraft.server.v1_8_R1.PlayerList.<init>(PlayerList.java:69) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at net.minecraft.server.v1_8_R1.DedicatedPlayerList.<init>(SourceFile:14) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at net.minecraft.server.v1_8_R1.DedicatedServer.init(DedicatedServer.java:133) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:473) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.ClassNotFoundException: plugins.mkezar.AbilityLib
            at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_31]
            at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_31]
            at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_31]
            at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_31]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:77) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:62) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_31]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_31]
            at java.lang.Class.forName0(Native Method) ~[?:1.8.0_31]
            at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_31]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:40) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-1092acb]
            ... 10 more
    I still don't know what to do
     
  15. Offline

    teej107

    Keeping that method as a static method is acceptable since you aren't using anything outside of the method. The reason why Eclipse is giving you that error is because you created an instance and was trying to call that method from the instance when you don't/shouldn't need to. Static modifiers aren't part of the instance but are part of the class instead. That being said, this question really shouldn't be asked on here as it has nothing to do with Bukkit. As for your error, Bukkit can't find the class that you defined in the yml. Make sure it is typed correctly and is in your jar.
     
Thread Status:
Not open for further replies.

Share This Page