aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hype/bbsentials/client/ToDisplayConfig.java
blob: 2c513d0a640d9e7ce4e716bddbf9b7277257b2f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package de.hype.bbsentials.client;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.loader.api.FabricLoader;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

public class ToDisplayConfig {
    private boolean disableAll = true;
    private boolean prehistoricEgg = true;
    private boolean pickonimbus2000 = true;
    private boolean controlSwitch = true;
    private boolean electronTransmitter = true;
    private boolean ftx3070 = true;
    private boolean robotronReflector = true;
    private boolean superliteMotor = true;
    private boolean syntheticHeart = true;
    private boolean flawlessGemstone = true;
    private boolean allRoboPartCustomChChestItem = true;
    private boolean allChChestItem = true;
    private boolean dwBetterTogether = true;
    private boolean dwDoublePowder = true;
    private boolean dwGoneWithTheWind = true;
    private boolean dwGoblinRaid = true;
    private boolean dwMithrilGourmand = true;
    private boolean dwRaffle = true;
    private boolean dwEvents = true;
    private boolean chBetterTogether = true;
    private boolean chDoublePowder = true;
    private boolean chGoneWithTheWind = true;
    private boolean chEvents = true;
    private boolean allBetterTogether = true;
    private boolean allDoublePowder = true;
    private boolean allGoneWithTheWind = true;
    private boolean allEvents = true;

    // Serialize the object to JSON and save to file
    public void saveToFile() {
        File configFile = new File(FabricLoader.getInstance().getConfigDirectory(), "BBsentials_display_Config.json");
        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        try (FileWriter writer = new FileWriter(configFile)) {
            gson.toJson(this, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Deserialize the object from JSON file
    public static ToDisplayConfig loadFromFile() {
        File configFile = new File(FabricLoader.getInstance().getConfigDirectory(), "BBsentials_display_Config.json");
        Gson gson = new Gson();

        try (FileReader reader = new FileReader(configFile)) {
            return gson.fromJson(reader, ToDisplayConfig.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // If file doesn't exist or there's an error, return a new instance
        return new ToDisplayConfig();
    }

    public void setValueAndSave(String propertyName, Object newValue) {
        String lowerCasePropertyName = propertyName.toLowerCase();

        try {
            Field field = getClass().getDeclaredField(lowerCasePropertyName);
            field.setAccessible(true);

            field.set(this, newValue);
            saveToFile();
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // Method to get a value based on property name
    public boolean getValue(String propertyName) {
        if (disableAll) return false;
        String lowerCasePropertyName = propertyName.toLowerCase();
        try {
            Field field = getClass().getDeclaredField(lowerCasePropertyName);
            field.setAccessible(true);
            return field.getBoolean(this);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return false;
    }
}