Help with static references/new instances

Discussion in 'Plugin Development' started by Rufus5, Mar 16, 2016.

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

    Rufus5

    Ok, I'm writing a method to get a file and return the YamlConfiguration. In my main JavaPlugin class, I have this method.

    Code:
    public YamlConfiguration getTownFile(String townName) throws IOException {   
            File f = null;
                f = new File(getDataFolder(), "/towns/" + townName + ".yml");
            if (!f.exists()) {
                f.createNewFile();
            }
            YamlConfiguration yaml = YamlConfiguration.loadConfiguration(f);
            return yaml;
        }
    In another class, I need to reference this method because getDataFolder is a method of JavaPlugin. However, I can't make a static reference to a non-static method, but if I make it a static YamlConfiguration method, I can't make a static reference to the non-static JavaPlugin. I heard about a way to make a new instance of the class to reference non statically, but I get some confusing errors whenever I try that. Any help with class.newInstance() methods or static references would be a huge help.
     
  2. Offline

    Zombie_Striker

    Why can't you convert that into
    Code:
    File f = new File(....
    That would be abusing static. You should instead pass the instance of the yml file through the other classes constructors.
     
    Konato_K likes this.
  3. Offline

    Rufus5

    I hadn't converted that back because I deleted some try/catch statements before and had to use the variable before them. Will fix now and try your suggestion.

    How can I pass the file through the other classes when it would have to be referenced statically?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 16, 2016
  4. @Rufus5 If I know what you're talking about, then you can either
    1) Pass a reference of the class through a constructor
    Code:
    MainClass plugin;
    File file = new File(plugin.getDataFolder(), "fileName.yml");
    
    public ThisClass(MainClass pl) {
        plugin = pl;
    }
    2) And this one isn't isn't really recommended, but it's still an option that works.
    Code:
    File file = new File("plugins/PluginName/FileName.yml");
     
  5. Offline

    Zombie_Striker

    @Rufus5
    But it shouldn't have to be referenced statically. Rule of thumb when it comes to using static: If you use it, you are either too lazy to pass the instance around or you just want there to be only one instance of that object. With what you're saying, it sounds like the first case.
     
  6. Offline

    Rufus5

    @Zombie_Striker I think I understand what you mean now by passing the instance around.


    @CodePlaysMinecraft I'll try that. Thanks.

    None of this is working. I'm getting errors trying to pass the event, then a NullPointerError before the stack trace cuts off? So I assume the game can't find the file.
    Why is this returning errors and not creating the file??
    Code:
    ServerMain sm;
      
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) throws IOException{
            if(Town.playerIsInATown(e.getPlayer())){
                File f = new File(sm.getDataFolder(), "/towns/" + Town.playerGetTown(e.getPlayer()) + ".yml");
                if(!f.exists()){
                    f.createNewFile();
                }
                YamlConfiguration yaml = YamlConfiguration.loadConfiguration(f);
                if(yaml.getList("allowed").contains(e.getPlayer().getUniqueId().toString())){
                    return;
                }else{
                    e.getPlayer().sendMessage("You do not have permission to build in " + ServerMain.properFormatString(Town.playerGetTown(e.getPlayer())));
                }
            }
            //TODO Check player location if inside plot, has permission accordingly
        }
    @Zombie_Striker @CodePlaysMinecraft

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 16, 2016
  7. @Rufus5 Do you even have anything in the config to take from? Why are you creating it there?
     
  8. Offline

    mcdorli

    Don't abuse static
     
  9. Offline

    PresentingTulip

    Couldn't you just use a public getter?

    Code:
    public static Main instance;
    
    //then in onenable
    this.instance = this;
    
    //then make a getter
    
    public static Main getInstance(){
    
    return instance;
    }
    
    Then use Main.getInstance()?
     
    Last edited: Mar 17, 2016
  10. Offline

    mcdorli

    Yeah, this technique is called "singleton". It can be useful, but in this situation, it would be just pure laziness. Also, getInstance needs to be static
     
    CodePlaysMinecraft and WolfMage1 like this.
  11. Offline

    PresentingTulip

    my bad forgot about that
     
  12. Offline

    Konato_K

    @CodePlaysMinecraft I just wanna say that code will throw a NPE since the field initializer is called before the constructor, so "plugin" is null by then.
     
  13. Offline

    mcdorli

    Yeah, also, put the this.instance in the constructor, so he can use it for normal projects too, not just bukkit based.
     
Thread Status:
Not open for further replies.

Share This Page