Can you check this?

Discussion in 'Plugin Development' started by Bavestry, Jul 17, 2012.

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

    Bavestry

    Here is my main class' code:

    Code:
    package com.bavestry.bukkit.simplefishmessage;
     
    import java.io.File;
    import java.util.Arrays;
    import java.util.logging.Level;
     
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class SimpleFishMessage extends JavaPlugin {
        public static String fishNames;
        public static Player player;
        public static File simpleFolder = new File("plugins/SimpleFishMessage/");
        public static YamlConfiguration configuration = new YamlConfiguration();
       
        public SimpleFishMessage(SimpleFishMessage o){
            o.getConfig().getList(fishNames);
        }
     
        public void onEnable(){
            getLogger().log(Level.INFO, getDescription().getVersion() + " has been enabled.");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
           
            String[] listOfStrings = {"fishNames"};
            this.getConfig().set("fishNames", Arrays.asList(listOfStrings));
           
            if(!simpleFolder.exists()){
                File simpleFolder = new File("plugins/SimpleFishMessage");
                simpleFolder.mkdir();
            }
        }
       
        public void onDisable(){
            getLogger().log(Level.INFO, getDescription().getVersion() + " has been disabled.");
        }
    }
    Here is my listener class' code:

    Code:
    package com.bavestry.bukkit.simplefishmessage;
     
    import com.bavestry.bukkit.simplefishmessage.SimpleFishMessage;
     
    import java.util.Random;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerFishEvent;
     
    public class onPlayerFish implements Listener {
        public static Random random = new Random();
        public static Object simpleObject = new SimpleFishMessage(null);
     
        public onPlayerFish(PlayerFishEvent event){
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GOLD + "You have caught " + ChatColor.GREEN + random.equals(simpleObject) + "!");
        }
    }
    
    Every time I run it on my local server, it returns with a 'NoSuchMethodException' in the main class. I think it's something to do with the constructors, but I have no clue. If you could leave your thoughts on this thread, it would be greatly appreciated. Thanks!
     
  2. remove
    public SimpleFishMessage(SimpleFishMessage o){
    o.getConfig().getList(fishNames);
    }
    as plugins may not have constructors



    why you defining
    public static YamlConfiguration configuration = new YamlConfiguration();
    if you can call getCOnfig?



    do not try to make another instace of your main class inside the listener

    i not recommend to print out lines that you plugin isenabled, as bukkit does this and its seen as spam by admins
     
    Bavestry likes this.
  3. Offline

    Bavestry

    Here is my new code:

    Main Class:

    Code:
    package com.bavestry.bukkit.simplefishmessage;
     
    import java.io.File;
    import java.util.Arrays;
    import java.util.logging.Level;
     
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class SimpleFishMessage extends JavaPlugin {
        public static String[] listOfStrings = {"fishNames"};
        public static Player player;
        public static File simpleFolder = new File("plugins/SimpleFishMessage/");
     
        public void onEnable(){
            getLogger().log(Level.INFO, getDescription().getVersion() + " has been enabled.");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
           
            this.getConfig().set("fishNames", Arrays.asList(listOfStrings));
           
            if(!simpleFolder.exists()){
                File simpleFolder = new File("plugins/SimpleFishMessage");
                simpleFolder.mkdir();
            }
        }
       
        public void onDisable(){
            getLogger().log(Level.INFO, getDescription().getVersion() + " has been disabled.");
        }
    }
    
    Listener class:

    Code:
    package com.bavestry.bukkit.simplefishmessage;
     
    import com.bavestry.bukkit.simplefishmessage.SimpleFishMessage;
     
    import java.util.Random;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerFishEvent;
     
    public class onPlayerFish implements Listener {
        public static Random random = new Random();
     
        public onPlayerFish(PlayerFishEvent event){
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GOLD + "You have caught " + ChatColor.GREEN + random.equals(SimpleFishMessage.listOfStrings) + "!");
        }
    }
    
    Heck, here's my config.yml:

    Code:
    fishNames:
    - a trout
    - an interesting fish
    - a special fish
    - twin fishies
    - two super fish
    - one awesome fish
    - an awesome fish
    Here's my plugin.yml too!:

    Code:
    name: SimpleFishMessage
    main: com.bavestry.bukkit.simplefishmessage.SimpleFishMessage
    version: v0.1
    I don't know why, but there is no message being sent when the player catches a fish. Please help >.<
     
  4. Offline

    r0306

    Bavestry
    You need to register the event listener class in your onEnable and also add the eventhandler annotation.
    Code:
        public void onEnable(){
            getLogger().log(Level.INFO, getDescription().getVersion() + " has been enabled.");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
         
            this.getConfig().set("fishNames", Arrays.asList(listOfStrings));
         
            if(!simpleFolder.exists()){
                File simpleFolder = new File("plugins/SimpleFishMessage");
                simpleFolder.mkdir();
     
            Bukkit.getPluginManager().registerEvents(new onPlayerFish(), this);
            }
    Code:
    @EventHandler   
    public onPlayerFish(PlayerFishEvent event){
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GOLD + "You have caught " + ChatColor.GREEN + random.equals(SimpleFishMessage.listOfStrings) + "!");
    }
     
    Bavestry likes this.
  5. Offline

    Bavestry

    Uhh, now when I fish, it sends me "You have caught false!"
     
  6. Offline

    r0306

    Bavestry
    Replace this:
    Code:
    random.equals(SimpleFishMessage.listOfStrings)
    with:
    Code:
    SimpleFishMessage.listOfStrings[random.nextInt(SimpleFishMessage.listOfStrings.length()]
     
    Bavestry likes this.
  7. Offline

    Bavestry

    r0306
    Now it says "You have caught a fishNames", probably something to do with this line in the main class, right?:

    Code:
        public static String[] listOfStrings = {"fishNames"};
     
  8. Offline

    r0306

    Bavestry
    Yeah. You can't redefine an array once you've initialized it with its value length which in this case is one so you will have initialize it with the config string list.
     
    Bavestry likes this.
  9. Offline

    Bavestry

    r0306
    ferrybig
    So I should add this into my main class:

    Code:
        public static String[] configStringList = {"fishNames"};
    And in my listener, change this:

    Code:
    player.sendMessage(ChatColor.GOLD + "You have caught " + ChatColor.GREEN + SimpleFishMessage.listOfStrings[random.nextInt(SimpleFishMessage.listOfStrings.length)]);
    to this:

    Code:
    player.sendMessage(ChatColor.GOLD + "You have caught " + ChatColor.GREEN + SimpleFishMessage.listOfStrings[random.nextInt(SimpleFishMessage.configStringList.length)]);
    I may be completely wrong. :p

    I was completely wrong. Still no idea what to do...

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

    r0306

    Bavestry
    Change this:
    Code:
    [FONT=Consolas] this.getConfig().set("fishNames", Arrays.asList(listOfStrings));[/FONT]
    to
    Code:
    configStringList =  this.getConfig().get("fishNames");
     
    Bavestry likes this.
  11. Offline

    VeryBIgCorp

    Just use an arraylist and call the Configuration method getStringList to initialize it with. This way, it'll mirror any modifications in the yaml.
     
  12. Offline

    Bavestry

    r0306
    Comes up with an error, fix options are to change it to an Object (doubt that's right) or add cast to String[]
     
  13. Offline

    r0306

    Bavestry
    Actually, you can create a method in your main class to return the list:

    Code:
    public static String[] getFishList()
    {
     
      return Arrays.asList(this.getConfig().getStringList("fishNames"));
     
    }
    So it will be:
    Code:
    player.sendMessage(ChatColor.GOLD + "You have caught " + ChatColor.GREEN + SimpleFishMessage.getFishList()[random.nextInt(SimpleFishMessage.getFishList().length()]);
     
    Bavestry likes this.
  14. Offline

    Bavestry

    r0306
    cough cannot use 'this' in a static context cough

    Really? I don't think you should advertise something like that here. Especially when I've heard from SEVERAL people that it is a virus.

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

    r0306

    Simply remove this and just do getConfig().blahblahblah
     
    Bavestry likes this.
  16. Offline

    Bavestry

    Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin. I already tried that...
     
  17. Offline

    r0306

    Bavestry
    Okay then just try initializing the array directly:

    Code:
    public class SimpleFishMessage extends JavaPlugin {
        public static String[] listOfStrings = this.getConfig().get("fishNames"); //like so
        public static Player player;
        public static File simpleFolder = new File("plugins/SimpleFishMessage/");
        }
     
    Bavestry likes this.
  18. Offline

    Bavestry

    r0306
    Once again it's public STATIC String[] listOfStrings = THIS.getConfig().get("fishNames");
    static + this = slap on wrist by JavaPlugin :/

    EDIT: Once again, my friend Google provides an answer to the impossible question!

    Code:
        public static String[] getFishList(SimpleFishMessage o){
          o.getConfig().set("fishNames", Arrays.asList(listOfStrings));
          return null;
        }
    and:

    Code:
    player.sendMessage(ChatColor.GOLD + "You have caught " + ChatColor.GREEN + SimpleFishMessage.getFishList(null)[random.nextInt(SimpleFishMessage.getFishList(null).length)]);
    Thank you google :)

    Referenced from: http://helpdesk.objects.com.au/java...e-a-static-reference-to-the-non-static-method

    Dang, got an error when I try to fish... I'll update it later, I have to go for around 5 days :|
     
  19. Offline

    r0306

    Bavestry
    Okay. Remove the static declaration in the method and access it like so:
    Code:
        public String[] getFishList()
        {
       
          return (String[]) this.getConfig().getStringList("fishNames").toArray();
       
        }
    Then from the other class:
    Code:
    SimpleFishMessage sp = new SimpleFishMessage();
    String[] list = sp.getFishList();
    list[random.nextInt(list.length()]
     
    Bavestry likes this.
  20. Offline

    Bavestry

    r0306
    My new main class:

    Code:
    1 package com.bavestry.bukkit.simplefishmessage;
    2
    3import java.io.File;
    4import java.util.Arrays;
    5import java.util.logging.Level;
    6
    7import org.bukkit.Bukkit;
    8import org.bukkit.entity.Player;
    9import org.bukkit.plugin.java.JavaPlugin;
    10
    11public class SimpleFishMessage extends JavaPlugin {
    12    public static String[] listOfStrings = {"fishNames"};
    13    public static String[] getFishNames;
    14    public static Player player;
    15    public static File simpleFolder = new File("plugins/SimpleFishMessage/");
    16    public SimpleFishMessage s = this;
    17
    18    public void onEnable(){
    19        getLogger().log(Level.INFO, getDescription().getVersion() + " has been enabled.");
    20        this.getConfig().options().copyDefaults(true);
    21        this.saveConfig();
    22       
    23        this.getConfig().set("fishNames", Arrays.asList(listOfStrings));
    24       
    25        if(!simpleFolder.exists()){
    26            File simpleFolder = new File("plugins/SimpleFishMessage");
    27            simpleFolder.mkdir();
    28        }
    29        Bukkit.getPluginManager().registerEvents(new onPlayerFish(), this);
    30    }
    31   
    32    public String[] getFishList(){
    33     this.getConfig().set("fishNames", Arrays.asList(listOfStrings));
    34      return null;
    35    }
    36   
    37    public void onDisable(){
    38        getLogger().log(Level.INFO, getDescription().getVersion() + " has been disabled.");
    39    }
    40}
    
    My new Listener class:

    Code:
    package com.bavestry.bukkit.simplefishmessage;
     
    1 import com.bavestry.bukkit.simplefishmessage.SimpleFishMessage;
    2
    3 import java.util.Random;
    4
    5 import org.bukkit.ChatColor;
    6 import org.bukkit.entity.Player;
    7 import org.bukkit.event.EventHandler;
    8 import org.bukkit.event.Listener;
    9 import org.bukkit.event.player.PlayerFishEvent;
    10
    11 public class onPlayerFish implements Listener {
    12 public static Random random = new Random();
    13 SimpleFishMessage sp = new SimpleFishMessage();
    14 String[] list = sp.getFishList();
    15 public static PlayerFishEvent.State CAUGHT_FISH;
    16
    17    @EventHandler
    18    public void PlayerFish(PlayerFishEvent event){
    19        Player player = event.getPlayer();
    20        player.sendMessage(ChatColor.GOLD + "You have caught " + ChatColor.GREEN + list[random.nextInt(list.length)]);
        }
    }
    
    My new error:

    Code:
    [SEVERE] Error occured while enabling SimpleFishMessage v0.1
    at org.apache.commands.lang.Validate.notNull<Validate.java:203>
    at org.bukkit.configuration.fil.YamlConfiguration.loadConfiguration<YamlConfiguration.java:170>
    at org.bukkit.plugin.java.JavaPlugin.reloadConfig<JavaPlugin.java:117>
    at org.bukkit.plugin.java.JavaPlugin.getConfig<JavaPlugin.java:111>
    at com.bavestry.bukkit.simplefishmessage.SimpleFishMessage.getFishList<SimpleFishMessage.java:33>
    at com.bavestry.bukkit.simplefishmessage.onPlayerFIsh.<init><onPlayerFish.java:16>
    at com.bavestry.bukkit.simplefishmessage.SimpleFishMessage.onEnabl<SimpleFishMessage.java:29>
    at org.bukkit.plugin.java.JavaPlugin.setEnabled<JavaPlugin.java:215>
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin<JavaPluginLoader.java:337>
    at org.bukkit.plugin.SimplePluginManager.enablePlugin<SimplePluginManager.java:381>
    at org.bukkit.craftbukkit.CraftServer.loadPlugin<CraftServer.java:256>
    at org.bukkit.craftbukkit.CraftServer.enablePlugins<CraftServer.java:238>
    at net.minecraft.server.MinecraftServer.t<MinecraftServer.java:381>
    at net.minecraft.server.MinecraftServer.a<MinecraftServer.java:368>
    at net.minecraft.server.MinecraftServer.init<MinecraftServer.java:197>
    at net.minecraft.server.MinecraftServer.run<MinecraftServer.java:432>
    at net.minecraft.server.ThreadServerApplication.run<SourceFile:492>
    I put line numbers for you :p not much of a clue what's going on here.
     
  21. Offline

    r0306

    Bavestry
    You need to make the following changes to your code. Follow the comments I put in and it should work.
    Code:
    1 package com.bavestry.bukkit.simplefishmessage;
    2
    3import java.io.File;
    4import java.util.Arrays;
    5import java.util.logging.Level;
    6
    7import org.bukkit.Bukkit;
    8import org.bukkit.entity.Player;
    9import org.bukkit.plugin.java.JavaPlugin;
    10
    11public class SimpleFishMessage extends JavaPlugin {
    12    //public static String[] listOfStrings = getFishList(); //MARK FOR REMOVAL
    13    public static String[] getFishNames;
    14    public static Player player;
    15    public static File simpleFolder = new File("plugins/SimpleFishMessage/");
    16    public SimpleFishMessage s = this;
    17
    18    public void onEnable(){
    19        getLogger().log(Level.INFO, getDescription().getVersion() + " has been enabled.");
       
                /* REMOVE THESE THREE LINES
                this.getConfig().options().copyDefaults(true);
    21        this.saveConfig();
    23        this.getConfig().set("fishNames", Arrays.asList(listOfStrings));
    24       */
    25        if(!simpleFolder.exists()){
    26            File simpleFolder = new File("plugins/SimpleFishMessage");
    27            simpleFolder.mkdir();
    28        }
    29        Bukkit.getPluginManager().registerEvents(new onPlayerFish(), this);
    30    }
    31 
    32    public String[] getFishList(){
    33    return (String[]) this.getConfig().getStringList("fishNames").toArray(); //NOTICE CHANGE TO THIS
    35    }
    36 
    37    public void onDisable(){
    38        getLogger().log(Level.INFO, getDescription().getVersion() + " has been disabled.");
    39    }
    40}
    Code:
    package com.bavestry.bukkit.simplefishmessage;
     
    1 import com.bavestry.bukkit.simplefishmessage.SimpleFishMessage;
    2
    3 import java.util.Random;
    4
    5 import org.bukkit.ChatColor;
    6 import org.bukkit.entity.Player;
    7 import org.bukkit.event.EventHandler;
    8 import org.bukkit.event.Listener;
    9 import org.bukkit.event.player.PlayerFishEvent;
    10
    11 public class onPlayerFish implements Listener {
    12 public static Random random = new Random();
    13 SimpleFishMessage sp = new SimpleFishMessage();
    14 String[] list = sp.getFishList();
    15 public static PlayerFishEvent.State CAUGHT_FISH;
    16
    17    @EventHandler
    18    public void PlayerFish(PlayerFishEvent event){
    19        Player player = event.getPlayer();
               
                //ADD THE FOLLOWING LINES IN
                SimpleFishMessage sp = new SimpleFishMessage();
                String[] list = sp.getFishList();
    20       player.sendMessage(ChatColor.GOLD + "You have caught " + ChatColor.GREEN +         list[random.nextInt(list.length()]);
        }
    }
     
    Bavestry likes this.
  22. Offline

    Bavestry

    r0306
    I'll try it on Monday, going camping for the weekend :p I'm sure it will work. Thanks a TON for all your help on this!
     
  23. Offline

    r0306

    Bavestry
    Np. Thanks for all the likes lol.
     
  24. Offline

    Bavestry

    r0306 I figure anything that helps me, even a bit, deserves a like, and someone who helps me like you, deserves a follow.
     
  25. Offline

    Bavestry

    r0306
    I made all the changes, it loads correctly on the server with no problem. Though, this error occurs when I right click with a fishing pole:

    Code:
    [SEVERE] Could not pass event PlayerFishEvent to SimpleFishMessage org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute<JavaPluginLoader.java:304>
    at org.bukkit.plugin.RegisteredListener.callEvent<RegesteredListener.java:62>
    at org.bukkit.plugin.SimplePluginManager.callEvent<SimplePLuginManager.java:460>
    at net.minecraft.server.ItemFishingRod.a<ItemFishingRod.java:22>
    at net.minecraft.server.ItemStack.a<ItemStack.java:97<
    at net.minecraft.server.ItemInWorldManager.useItem<ItemInWorldManager.java:261>
    at net.minecraft.server.NetServerHandler.a<NetServerHandler.java:607>
    at net.minecraft.server.Packet15Place.handle<SourceFile:39>
    at net.minecraft.server.NetworkManager.b<NetworkManager.java:229>
    at net.minecraft.server.NetServerHandler.a<NetServerHandler.java:113>
    at net.minecraft.server.NetworkListenThread.a<NetworkLIstenThread.java:78>
    at net.minecraft.server.MinecraftServer.w<MinecraftServer.java:567>
    at net.minecraft.server.MinecraftServer.run<MinecraftServer.java459>
    at net.minecraft.server.ThreadServerApplication.run<SourceFile:492> Caused by java.lang.IllegalArgumentException: File cannot be null
    at org.apache.commons.lang.Validate.notNull<Validate.java:203>
    at org.bukkit.configuration.file.YamlConfiguration.laodConfiguration<YamlConfiguration.java:170>
    at org.bukkit.plugin.java.JavaPlugin.reloadConfig<JavaPlugin.java:117>
    at org.bukkit.plugin.java.JavaPlugin.getConfig<JavaPlugin.java:111>
    at com.bavestry.bukkit.simplefishmessage.SimpleFIshMessage.getFishList<SimpleFishMessage.java:26>
    at com.bavestry.bukkit.simplefishmessage.onPLayerFishPlayerFish<onPLayerFish.java:21>
    at sun.reflect.NativeMethodAccessorImpl.invoke0<Native Method>
    at sun.reflect.NativeMethodAccessorImpl.invoke<Unknown Source>
    at sun.reflect.DelegatingMethodAccessorImpl.invoke<Unknown Source>
    at java.lang.reflect.Method.invoke<Unknown Source>
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute<JavaPluginLoader.java:301>
    ... 13 more
     
Thread Status:
Not open for further replies.

Share This Page