Home  >  Article  >  Get json file in bukkit

Get json file in bukkit

WBOY
WBOYforward
2024-02-22 13:04:071189browse

php editor Youzi will help you quickly master the method of obtaining json files in bukkit. When developing Minecraft plug-ins using Java, manipulating json files is a common requirement. This article will introduce in detail how to obtain and parse json files in the bukkit plug-in, allowing you to easily cope with various development scenarios. Whether you are a novice or an experienced developer, you can benefit from it and quickly improve your technical level. Next, let’s dive into this issue and explore solutions.

Question content

I'm trying to get json in my plugin but it doesn't work. This is my code to get it, this is the path: static file json = new file("config.json");. I tried using getdatafoder() method but couldn't because I am using static method.

public class JSONReader extends JavaPlugin {
    static File json = new File("config.JSON");

    static File getJSON() {
        return json;
    }

    static JSONObject setupJson() {
        try {
            JSONParser jsonParser = new JSONParser();
            Object parsed = jsonParser.parse(new FileReader(json.getPath()));
            JSONObject jsonObject = (JSONObject) parsed;
            return jsonObject;
        } catch (ParseException | IOException e) {
            return null;
        }
    }
}

Workaround

All paths are evaluated relative to your working directory, which contains your server executable. bukkit provides convenient file access methods through JavaPlugin:

  • Refer to files in the plug-in data folder

    file f = new file(getdatafolder(), "config.json");
  • Retrieve files in the plug-in jar

    InputStream is = getResource("config.json");

The path is relative to the bukkit server jar, so from there you need to go to the file /plugins/plugin-name/config.json

The above is the detailed content of Get json file in bukkit. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete