Save variables on server reload

Discussion in 'Plugin Development' started by fabiothomas, Feb 18, 2019.

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

    fabiothomas

    Hey, i am quite new to coding plugins but i have been working on a plugin that allows you to place down pre-build buildings and when you do that it sets a HashMap with the location as key to the name of the player to "claim" the area.
    This all works fine but i have one problem.

    When i reload the plugin all buildings loose there information.
    I think this is because the variables do not save but i couldn't find a way to make the variables save. Do i need any config files or anything like that for this cause i did not make a config file yet (and don't know how to do this).
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    fabiothomas

    Yeah i was afraid of that. but i have no idea how to set one up.
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    Chr0mosom3

    @fabiothomas, I remember being like you, it's hard at first but then it gets easier, let me show you the ropes

    First of all, in whatever class you need the variables to save I would recommend to create a config variable, like so:
    Code:
     FileConfiguration config = /*InsanceOfYourPlugin*/.getConfig(); 
    Once you have a config variable set you can do so many things from now on.

    You can set a value in the config:
    Code:
     config.set("Name", "Value"); 
    In the config it would look like this:
    Code:
     Name:
      Value 
    For example if you wanted to save the player's balance you would do it like this

    Code:
    Code:
    config.set(player.getName() + ".Balance", CurrencyHandler.getBalance(player));
    
    Config:
    Code:
    ExamplePlayerName:
      Balance: 100
    
    Also, don't forget to always at the end do at then end of your code
    Code:
     /*InstanceOfYourPlugin*/.saveConfig(); 

    Now if you want to get some data to the config you would do this
    Code:
     CurrencyHandler.setBalance(player, config.getInt(player.getName() + ".Balance")); 
    If you want to save a location then I would be happy to show you a way that @timtower showed me


    I hoped this help you, if you don't understand me feel free to @ me.



    @timtower, did I explain it well for a beginner to the forums?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @MrDaniel You did pretty much copy the tutorial though to a user that hasn't been online the past two weeks nor responded that he didn't understand the link
     
Thread Status:
Not open for further replies.

Share This Page