Skip to content

Commit d720360

Browse files
committed
rewrote everything + added config
1 parent f9f6815 commit d720360

File tree

4 files changed

+268
-29
lines changed

4 files changed

+268
-29
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ dependencies {
3030
// Uncomment the following line to enable the deprecated Fabric API modules.
3131
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.
3232

33+
implementation "com.googlecode.json-simple:json-simple:1.1.1"
34+
3335
// modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}"
3436
}
3537

src/main/java/net/just_s/sds/Config.java

Lines changed: 261 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,286 @@
22

33
import net.minecraft.block.Block;
44
import net.minecraft.tag.TagKey;
5+
import net.minecraft.util.registry.Registry;
56
import org.jetbrains.annotations.Nullable;
67

8+
import net.fabricmc.loader.api.FabricLoader;
9+
10+
import java.io.*;
711
import java.util.ArrayList;
812
import java.util.HashMap;
913
import java.util.List;
14+
import java.util.Map;
1015
import java.util.stream.Stream;
1116

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+
1224
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)) {
1642

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);
2144

22-
private static HashMap<String, List<String>> blockToProperties = new HashMap<>();
45+
whitelist = (boolean) jfile.get("whitelist");
2346

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+
}
2678
}
2779

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
29227
Stream<TagKey<Block>> tagStream = block.getRegistryEntry().streamTags();
30228
List<TagKey<Block>> tagArray = tagStream.toList();
31229
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;
33232
}
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;
36241
}
37242

38243
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;
48286
}
49287
}

src/main/java/net/just_s/sds/SDSMod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class SDSMod implements ModInitializer {
99

1010
@Override
1111
public void onInitialize() {
12+
Config.load();
1213
LOGGER.info("SDS initialized successfully!");
1314
}
1415
}

src/main/java/net/just_s/sds/mixin/DebugStickMixin.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.just_s.sds.mixin;
22

33
import net.just_s.sds.Config;
4-
import net.just_s.sds.SDSMod;
54
import net.minecraft.block.Block;
65
import net.minecraft.block.BlockState;
76
import net.minecraft.entity.player.PlayerEntity;
@@ -45,7 +44,7 @@ private void onUSE(PlayerEntity player, BlockState state, WorldAccess world, Blo
4544
StateManager<Block, BlockState> stateManager = block.getStateManager();
4645
Collection<Property<?>> collection = stateManager.getProperties();
4746

48-
if (!isBlockModifiable(state.getBlock()) || collection.isEmpty()) {
47+
if (!isBlockAllowedToModify(state.getBlock()) || collection.isEmpty()) {
4948
sendMessage(player, Text.of(Config.MESSAGE_nomodify));
5049
cir.setReturnValue(false);
5150
return;
@@ -63,7 +62,6 @@ private void onUSE(PlayerEntity player, BlockState state, WorldAccess world, Blo
6362
// select next property
6463
property = getNextProperty(collection, property, block);
6564
nbtCompound.putString(blockName, property.getName());
66-
SDSMod.LOGGER.warn(nbtCompound.toString());
6765

6866
sendMessage(player, Text.of(
6967
String.format(
@@ -76,7 +74,7 @@ private void onUSE(PlayerEntity player, BlockState state, WorldAccess world, Blo
7674
} else {
7775
// change value of property
7876
if (property == null) {
79-
property = getNextProperty(collection, null, null);
77+
property = getNextProperty(collection, null, block);
8078
}
8179

8280
BlockState blockState = cycle(state, property, false);
@@ -105,8 +103,8 @@ private Property<?> getNextProperty(Collection<Property<?>> collection, @Nullabl
105103
/**
106104
* Check via config if chosen block is able to be modified in survival
107105
* */
108-
private boolean isBlockModifiable(Block block) {
109-
return Config.whitelist == Config.isInList(block);
106+
private boolean isBlockAllowedToModify(Block block) {
107+
return Config.isBlockAllowed(block);
110108
}
111109

112110
/**

0 commit comments

Comments
 (0)