Home > Java > body text

Get json file in bukkit

WBOY
Release: 2024-02-22 13:04:07
forward
1188 people have browsed it

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;
        }
    }
}
Copy after login

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");
    Copy after login
  • Retrieve files in the plug-in jar

    InputStream is = getResource("config.json");
    Copy after login

    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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!