|
2 | 2 |
|
3 | 3 | import net.minecraft.block.Block; |
4 | 4 | import net.minecraft.tag.TagKey; |
| 5 | +import net.minecraft.util.registry.Registry; |
5 | 6 | import org.jetbrains.annotations.Nullable; |
6 | 7 |
|
| 8 | +import net.fabricmc.loader.api.FabricLoader; |
| 9 | + |
| 10 | +import java.io.*; |
7 | 11 | import java.util.ArrayList; |
8 | 12 | import java.util.HashMap; |
9 | 13 | import java.util.List; |
| 14 | +import java.util.Map; |
10 | 15 | import java.util.stream.Stream; |
11 | 16 |
|
| 17 | +import org.json.simple.JSONArray; |
| 18 | +import org.json.simple.JSONObject; |
| 19 | +import org.json.simple.parser.JSONParser; |
| 20 | +import org.json.simple.parser.ParseException; |
| 21 | + |
| 22 | +import com.google.gson.*; |
| 23 | + |
12 | 24 | public class Config { |
13 | | - public static String MESSAGE_nomodify = "Этот блок нельзя изменять."; |
14 | | - public static String MESSAGE_select = "Параметр «%s» выбран (%s)"; |
15 | | - public static String MESSAGE_change = "Параметр «%s» изменён (%s)"; |
| 25 | + private static final File configFile = FabricLoader.getInstance().getConfigDir().resolve("SDS.json").toFile(); |
| 26 | + |
| 27 | + public static String MESSAGE_nomodify; |
| 28 | + public static String MESSAGE_select; |
| 29 | + public static String MESSAGE_change; |
| 30 | + |
| 31 | + public static boolean whitelist; |
| 32 | + private static HashMap<String, Boolean> properties = new HashMap<>(); |
| 33 | + |
| 34 | + private static HashMap<String, List<String>> tags_allowed = new HashMap<>(); |
| 35 | + private static HashMap<String, List<String>> blocks_allowed = new HashMap<>(); |
| 36 | + private static HashMap<String, List<String>> tags_forbidden = new HashMap<>(); |
| 37 | + private static HashMap<String, List<String>> blocks_forbidden = new HashMap<>(); |
| 38 | + |
| 39 | + public static void load() { |
| 40 | + JSONParser parser = new JSONParser(); |
| 41 | + try (Reader reader = new FileReader(configFile)) { |
16 | 42 |
|
17 | | - public static boolean whitelist = false; |
18 | | - private static List<String> properties = new ArrayList<>(); |
19 | | - private static List<String> blocks = new ArrayList<>(); |
20 | | - private static List<String> tags = new ArrayList<>(); |
| 43 | + JSONObject jfile = (JSONObject) parser.parse(reader); |
21 | 44 |
|
22 | | - private static HashMap<String, List<String>> blockToProperties = new HashMap<>(); |
| 45 | + whitelist = (boolean) jfile.get("whitelist"); |
23 | 46 |
|
24 | | - public static boolean isInList(String property) { |
25 | | - return properties.contains(property); |
| 47 | + JSONObject messages = (JSONObject) jfile.get("messages"); |
| 48 | + MESSAGE_nomodify = (String) messages.get("nomodify"); |
| 49 | + MESSAGE_select = (String) messages.get("select"); |
| 50 | + MESSAGE_change = (String) messages.get("change"); |
| 51 | + |
| 52 | + JSONObject allowed = (JSONObject) jfile.get("allowed"); |
| 53 | + JSONArray properties_allowed = (JSONArray) allowed.get("properties"); |
| 54 | + JSONArray tags_allowed_js = (JSONArray) allowed.get("tags"); |
| 55 | + JSONArray blocks_allowed_js = (JSONArray) allowed.get("blocks"); |
| 56 | + |
| 57 | + JSONObject forbidden = (JSONObject) jfile.get("forbidden"); |
| 58 | + JSONArray properties_forbidden = (JSONArray) forbidden.get("properties"); |
| 59 | + JSONArray tags_forbidden_js = (JSONArray) forbidden.get("tags"); |
| 60 | + JSONArray blocks_forbidden_js = (JSONArray) forbidden.get("blocks"); |
| 61 | + |
| 62 | + for (String property : (Iterable<String>) properties_allowed) { |
| 63 | + properties.put(property, true); |
| 64 | + } |
| 65 | + for (String property : (Iterable<String>) properties_forbidden) { |
| 66 | + properties.put(property, false); |
| 67 | + } |
| 68 | + |
| 69 | + populate(tags_allowed, tags_allowed_js); |
| 70 | + populate(tags_forbidden, tags_forbidden_js); |
| 71 | + populate(blocks_allowed, blocks_allowed_js); |
| 72 | + populate(blocks_forbidden, blocks_forbidden_js); |
| 73 | + |
| 74 | + } catch (IOException | ParseException | ClassCastException e) { |
| 75 | + SDSMod.LOGGER.warn("Error while loading config: " + e); |
| 76 | + factorySettings(); |
| 77 | + } |
26 | 78 | } |
27 | 79 |
|
28 | | - public static boolean isInList(Block block) { |
| 80 | + private static void factorySettings() { |
| 81 | + whitelist = false; |
| 82 | + MESSAGE_nomodify = "This block is not modifiable."; |
| 83 | + MESSAGE_select = "Property «%s» was selected (%s)."; |
| 84 | + MESSAGE_change = "Property «%s» was modified (%s)."; |
| 85 | + properties = new HashMap<>(); |
| 86 | + tags_allowed = new HashMap<>(); |
| 87 | + tags_forbidden = new HashMap<>(); |
| 88 | + blocks_allowed = new HashMap<>(); |
| 89 | + blocks_forbidden = new HashMap<>(); |
| 90 | + save(); |
| 91 | + } |
| 92 | + |
| 93 | + public static void save() { |
| 94 | + JSONObject jfile = new JSONObject(); |
| 95 | + jfile.put("whitelist", whitelist); |
| 96 | + |
| 97 | + JSONObject messages = new JSONObject(); |
| 98 | + messages.put("nomodify", MESSAGE_nomodify); |
| 99 | + messages.put("select", MESSAGE_select); |
| 100 | + messages.put("change", MESSAGE_change); |
| 101 | + jfile.put("messages", messages); |
| 102 | + |
| 103 | + JSONObject allowed = new JSONObject(); |
| 104 | + JSONArray properties_allowed = new JSONArray(); |
| 105 | + for (Map.Entry<String, Boolean> entry : properties.entrySet()) { |
| 106 | + if (entry.getValue()) properties_allowed.add(entry.getKey()); |
| 107 | + } |
| 108 | + allowed.put("properties", properties_allowed); |
| 109 | + |
| 110 | + JSONArray tags_allowed_js = new JSONArray(); |
| 111 | + for (Map.Entry<String, List<String>> entry : tags_allowed.entrySet()) { |
| 112 | + JSONObject tag = new JSONObject(); |
| 113 | + tag.put("id", entry.getKey()); |
| 114 | + |
| 115 | + List<String> list = entry.getValue(); |
| 116 | + if (!list.isEmpty()) { |
| 117 | + JSONArray props = new JSONArray(); |
| 118 | + for (String property : list) { |
| 119 | + props.add(property); |
| 120 | + } |
| 121 | + tag.put("properties", props); |
| 122 | + } |
| 123 | + tags_allowed_js.add(tag); |
| 124 | + } |
| 125 | + allowed.put("tags", tags_allowed_js); |
| 126 | + |
| 127 | + JSONArray blocks_allowed_js = new JSONArray(); |
| 128 | + for (Map.Entry<String, List<String>> entry : blocks_allowed.entrySet()) { |
| 129 | + JSONObject block = new JSONObject(); |
| 130 | + block.put("id", entry.getKey()); |
| 131 | + |
| 132 | + List<String> list = entry.getValue(); |
| 133 | + if (!list.isEmpty()) { |
| 134 | + JSONArray props = new JSONArray(); |
| 135 | + for (String property : list) { |
| 136 | + props.add(property); |
| 137 | + } |
| 138 | + block.put("properties", props); |
| 139 | + } |
| 140 | + blocks_allowed_js.add(block); |
| 141 | + } |
| 142 | + allowed.put("blocks", blocks_allowed_js); |
| 143 | + |
| 144 | + jfile.put("allowed", allowed); |
| 145 | + |
| 146 | + JSONObject forbidden = new JSONObject(); |
| 147 | + JSONArray properties_forbidden = new JSONArray(); |
| 148 | + for (Map.Entry<String, Boolean> entry : properties.entrySet()) { |
| 149 | + if (!entry.getValue()) properties_forbidden.add(entry.getKey()); |
| 150 | + } |
| 151 | + forbidden.put("properties", properties_forbidden); |
| 152 | + |
| 153 | + JSONArray tags_forbidden_js = new JSONArray(); |
| 154 | + for (Map.Entry<String, List<String>> entry : tags_forbidden.entrySet()) { |
| 155 | + JSONObject tag = new JSONObject(); |
| 156 | + tag.put("id", entry.getKey()); |
| 157 | + |
| 158 | + List<String> list = entry.getValue(); |
| 159 | + if (!list.isEmpty()) { |
| 160 | + JSONArray props = new JSONArray(); |
| 161 | + for (String property : list) { |
| 162 | + props.add(property); |
| 163 | + } |
| 164 | + tag.put("properties", props); |
| 165 | + } |
| 166 | + tags_forbidden_js.add(tag); |
| 167 | + } |
| 168 | + forbidden.put("tags", tags_forbidden_js); |
| 169 | + |
| 170 | + JSONArray blocks_forbidden_js = new JSONArray(); |
| 171 | + for (Map.Entry<String, List<String>> entry : blocks_forbidden.entrySet()) { |
| 172 | + JSONObject block = new JSONObject(); |
| 173 | + block.put("id", entry.getKey()); |
| 174 | + |
| 175 | + List<String> list = entry.getValue(); |
| 176 | + if (!list.isEmpty()) { |
| 177 | + JSONArray props = new JSONArray(); |
| 178 | + for (String property : list) { |
| 179 | + props.add(property); |
| 180 | + } |
| 181 | + block.put("properties", props); |
| 182 | + } |
| 183 | + blocks_forbidden_js.add(block); |
| 184 | + } |
| 185 | + forbidden.put("blocks", blocks_forbidden_js); |
| 186 | + |
| 187 | + jfile.put("forbidden", forbidden); |
| 188 | + |
| 189 | + Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| 190 | + JsonElement je = JsonParser.parseString(jfile.toJSONString()); |
| 191 | + String prettyJsonString = gson.toJson(je); |
| 192 | + |
| 193 | + try { |
| 194 | + FileWriter writer = new FileWriter(configFile); |
| 195 | + writer.write(prettyJsonString); |
| 196 | + writer.close(); |
| 197 | + SDSMod.LOGGER.error("Saved new config file."); |
| 198 | + } catch (IOException e) {SDSMod.LOGGER.error("Error while saving:" + e.getMessage());} |
| 199 | + } |
| 200 | + |
| 201 | + private static void populate(HashMap<String, List<String>> map, JSONArray source) { |
| 202 | + for (JSONObject entry : (Iterable<JSONObject>) source) { |
| 203 | + String id = (String) entry.get("id"); |
| 204 | + if (!id.contains(":")) { |
| 205 | + id = "minecraft:" + id; |
| 206 | + } |
| 207 | + List<String> list = new ArrayList<>(); |
| 208 | + if (entry.containsKey("properties")) { |
| 209 | + JSONArray props = (JSONArray) entry.get("properties"); |
| 210 | + for (String property : (Iterable<String>) props) { |
| 211 | + list.add(property); |
| 212 | + } |
| 213 | + } |
| 214 | + map.put(id, list); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public static boolean isBlockAllowed(Block block) { |
| 219 | + // 1) check if block has been mentioned in BLOCKS part |
| 220 | + String blockName = Registry.BLOCK.getId(block).toString(); |
| 221 | + |
| 222 | + if (blocks_allowed.containsKey(blockName)) return true; |
| 223 | + if (blocks_forbidden.containsKey(blockName)) { |
| 224 | + return !blocks_forbidden.get(blockName).isEmpty(); |
| 225 | + } |
| 226 | + // 2) if block is not stated in config, check tags |
29 | 227 | Stream<TagKey<Block>> tagStream = block.getRegistryEntry().streamTags(); |
30 | 228 | List<TagKey<Block>> tagArray = tagStream.toList(); |
31 | 229 | for (TagKey<Block> tag : tagArray) { |
32 | | - if (tags.contains(tag.toString())) return true; |
| 230 | + String tagName = tag.id().toString(); |
| 231 | + if (tags_allowed.containsKey(tagName)) return true; |
33 | 232 | } |
34 | | - |
35 | | - return blocks.contains(block.toString()); |
| 233 | + for (TagKey<Block> tag : tagArray) { |
| 234 | + String tagName = tag.id().toString(); |
| 235 | + if (tags_forbidden.containsKey(tagName)) { |
| 236 | + return !tags_forbidden.get(tagName).isEmpty(); |
| 237 | + } |
| 238 | + } |
| 239 | + // 3) if block and its tags were not clarified in config, check whitelist mode |
| 240 | + return !whitelist; |
36 | 241 | } |
37 | 242 |
|
38 | 243 | public static boolean isPropertyAllowed(String propertyName, @Nullable Block block) { |
39 | | - if (isInList(propertyName)) {return whitelist;} |
40 | | - if (block == null) {return !whitelist;} |
41 | | - List<String> propertiesOfBlock = blockToProperties.get(block.toString()); |
42 | | - boolean bl = propertiesOfBlock == null || propertiesOfBlock.contains(propertyName); |
43 | | - return whitelist == bl; |
44 | | - } |
45 | | - |
46 | | - public static boolean isPropertyBanned(String propertyName, @Nullable Block block) { |
47 | | - return !isPropertyAllowed(propertyName, block); |
| 244 | + if (block != null) { |
| 245 | + String blockName = Registry.BLOCK.getId(block).toString(); |
| 246 | + // 1) Check for exactly this block |
| 247 | + if (blocks_forbidden.containsKey(blockName)) { |
| 248 | + List<String> forbidden_props = blocks_forbidden.get(blockName); |
| 249 | + if (forbidden_props.contains(propertyName)) return false; |
| 250 | + } |
| 251 | + if (blocks_allowed.containsKey(blockName)) { |
| 252 | + List<String> allowed_props = blocks_allowed.get(blockName); |
| 253 | + if (allowed_props.contains(propertyName)) return true; |
| 254 | + if (allowed_props.isEmpty()) { |
| 255 | + if (properties.containsKey(propertyName)) return properties.get(propertyName); |
| 256 | + return true; |
| 257 | + } |
| 258 | + } |
| 259 | + // 2) Either block is not stated in config or |
| 260 | + // allowed by itself, but does not speak about property. Check its tags |
| 261 | + Stream<TagKey<Block>> tagStream = block.getRegistryEntry().streamTags(); |
| 262 | + List<TagKey<Block>> tagArray = tagStream.toList(); |
| 263 | + for (TagKey<Block> tag : tagArray) { |
| 264 | + String tagName = tag.id().toString(); |
| 265 | + if (tags_forbidden.containsKey(tagName)) { |
| 266 | + List<String> forbidden_props = tags_forbidden.get(tagName); |
| 267 | + if (forbidden_props.contains(propertyName)) return false; |
| 268 | + } |
| 269 | + } |
| 270 | + for (TagKey<Block> tag : tagArray) { |
| 271 | + String tagName = tag.id().toString(); |
| 272 | + if (tags_allowed.containsKey(tagName)) { |
| 273 | + List<String> allowed_props = tags_allowed.get(tagName); |
| 274 | + if (allowed_props.contains(propertyName)) return true; |
| 275 | + if (allowed_props.isEmpty()) { |
| 276 | + if (properties.containsKey(propertyName)) return properties.get(propertyName); |
| 277 | + return true; |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + // 3) If tags do not speak about property, check global list |
| 283 | + if (properties.containsKey(propertyName)) return properties.get(propertyName); |
| 284 | + // 4) if property was not clarified in config, check whitelist mode |
| 285 | + return !whitelist; |
48 | 286 | } |
49 | 287 | } |
0 commit comments