Skip to content

Commit cff80c4

Browse files
committed
schema experiments
1 parent 47375b6 commit cff80c4

18 files changed

Lines changed: 766 additions & 99 deletions

File tree

CHANGELOG.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Changelog of recent updates: https://github.com/RobertSkalko/Mine-And-Slash-Rework/blob/1.20-Forge/changelogs/v.6.2.txt
22

3-
v.6.3.1
3+
v.6.3.5
44

55
- Merged PRs

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
org.gradle.jvmargs=-Xmx2G
33
# --- COMMON GRADLE START ---
44
# Mod Properties
5-
mod_version=6.3.1
5+
mod_version=6.3.5
66
# Minecraft Versions
77
minecraft_version=1.20.1
88
forgeversion=47.1.43

src/main/java/com/robertx22/mine_and_slash/mmorpg/MMORPG.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.robertx22.mine_and_slash.uncommon.effectdatas.rework.action.StatEffect;
5050
import com.robertx22.mine_and_slash.uncommon.effectdatas.rework.condition.StatCondition;
5151
import com.robertx22.mine_and_slash.uncommon.interfaces.data_items.VanillaRarities;
52+
import com.robertx22.test.test2.SchemaTest;
5253
import net.minecraft.ChatFormatting;
5354
import net.minecraft.resources.ResourceLocation;
5455
import net.minecraftforge.api.distmarker.Dist;
@@ -86,6 +87,7 @@ public class MMORPG {
8687
public static ModRequiredRegisterInfo REGISTER_INFO = new ModRequiredRegisterInfo(SlashRef.MODID);
8788

8889
public static String formatNumber(float num) {
90+
8991
if (num < ClientConfigs.getConfig().SHOW_DECIMALS_ON_NUMBER_SMALLER_THAN.get()) {
9092
return DECIMAL_FORMAT.format(num);
9193
} else {
@@ -122,6 +124,9 @@ public static String formatBigNumber(float num) {
122124
);
123125

124126
public MMORPG() {
127+
128+
SchemaTest.run();
129+
125130
final IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
126131
OrderedModConstructor.register(new MnsConstructor(SlashRef.MODID), bus);
127132

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.robertx22.test;
2+
3+
import com.google.common.base.Supplier;
4+
5+
public abstract class MainSchema<T> extends ModSchema<T> {
6+
7+
public MainSchema(Supplier<ModSchema> sup) {
8+
super(null, "main_schemas_have_no_id", sup);
9+
}
10+
11+
public SchemaPart.ID id = new SchemaPart.ID(this);
12+
13+
public void errorOut(String reason) {
14+
try {
15+
throw new RuntimeException("Datapack " + id.get() + " is broken!");
16+
} catch (RuntimeException e) {
17+
e.printStackTrace();
18+
}
19+
throw new RuntimeException(reason);
20+
}
21+
22+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.robertx22.test;
2+
3+
import com.google.common.base.Preconditions;
4+
import com.google.common.base.Supplier;
5+
import com.google.gson.JsonObject;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public abstract class ModSchema<T> {
11+
12+
public String id;
13+
14+
public Supplier<ModSchema> sup;
15+
16+
public List<SchemaPart> parts = new ArrayList<>();
17+
public List<ModSchema> schemas = new ArrayList<>();
18+
19+
public ModSchema<T> createNewInstance() {
20+
return sup.get();
21+
}
22+
23+
public ModSchema(ModSchema parent, String id, Supplier<ModSchema> sup) {
24+
this.id = id;
25+
this.sup = sup;
26+
if (parent == null && this instanceof MainSchema) {
27+
// main schemas dont have a parent
28+
} else {
29+
Preconditions.checkArgument(parent != null);
30+
parent.schemas.add(this);
31+
}
32+
}
33+
34+
35+
public JsonObject toJson() {
36+
JsonObject json = new JsonObject();
37+
for (SchemaPart part : parts) {
38+
part.save(json);
39+
}
40+
for (ModSchema part : schemas) {
41+
JsonObject inner = part.toJson();
42+
json.add(part.id, inner);
43+
}
44+
45+
return json;
46+
}
47+
48+
public void load(MainSchema main, JsonObject json) {
49+
for (SchemaPart part : parts) {
50+
if (!json.has(part.id)) {
51+
if (part.isOptional()) {
52+
part.object = part.optionalValue;
53+
} else {
54+
throw new RuntimeException("Datapack error: " + part.id + " field inside " + main.id + " is missing and not optional!");
55+
}
56+
}
57+
var loaded = json.get(part.id);
58+
part.load(main, loaded);
59+
}
60+
for (ModSchema part : schemas) {
61+
JsonObject inner = json.getAsJsonObject(part.id);
62+
part.load(main, inner);
63+
}
64+
}
65+
66+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.robertx22.test;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.robertx22.library_of_exile.registry.serialization.MyGSON;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
public class PartList<RAW, OBJECT> extends SchemaPart<PartList<RAW, OBJECT>, List<RAW>, List<OBJECT>> {
12+
13+
public SchemaPart<?, RAW, OBJECT> verifierSchema;
14+
15+
public void add(RAW obj) {
16+
object.add(obj);
17+
}
18+
19+
@Override
20+
public List<RAW> toRaw(List<OBJECT> t) {
21+
return object;
22+
}
23+
24+
@Override
25+
public List<OBJECT> get() {
26+
return object.stream().map(x -> {
27+
var obj = verifierSchema.sup.get();
28+
obj.setRaw(x);
29+
return obj.get();
30+
}).collect(Collectors.toList());
31+
}
32+
33+
public PartList(ModSchema holder, String id, SchemaPart verifierSchema) {
34+
super(holder, id, null);
35+
this.verifierSchema = verifierSchema;
36+
}
37+
38+
@Override
39+
public List<RAW> fromJsonElement(MainSchema main, JsonElement element) {
40+
List<RAW> list = new ArrayList<>();
41+
for (JsonElement ele : element.getAsJsonArray()) {
42+
var obj = verifierSchema.sup.get();
43+
obj.load(main, ele);
44+
list.add((obj.getRaw()));
45+
}
46+
return list;
47+
}
48+
49+
@Override
50+
public void save(JsonObject json) {
51+
json.add(id, MyGSON.GSON.toJsonTree(object));
52+
}
53+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.robertx22.test;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.robertx22.library_of_exile.registry.serialization.MyGSON;
6+
7+
import java.util.Map;
8+
9+
// if they're all primitives and i anyway just serialize them all with gson, then i need to just do that and bundle some verifier?
10+
11+
public class PartMap<Key, T> extends SchemaPart<PartMap<Key, T>, Map<Key, T>, Map<Key, T>> {
12+
13+
public SchemaPart<?, ?, T> verifierSchema;
14+
15+
16+
public void put(Key key, T obj) {
17+
object.put(key, obj);
18+
}
19+
20+
@Override
21+
public Map<Key, T> toRaw(Map<Key, T> t) {
22+
return object;
23+
}
24+
25+
@Override
26+
public Map<Key, T> get() {
27+
return object;
28+
}
29+
30+
public PartMap(ModSchema holder, String id, SchemaPart verifierSchema) {
31+
super(holder, id, null);
32+
this.verifierSchema = verifierSchema;
33+
}
34+
35+
// todo if i save like this i cant verify stuff
36+
@Override
37+
public Map<Key, T> fromJsonElement(MainSchema main, JsonElement element) {
38+
return MyGSON.GSON.fromJson(element, object.getClass());
39+
}
40+
41+
@Override
42+
public void save(JsonObject json) {
43+
json.add(id, MyGSON.GSON.toJsonTree(object));
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.robertx22.test;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.robertx22.library_of_exile.registry.serialization.MyGSON;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
// used for lists, maps etc
11+
public class PrimitiveList<T> extends SchemaPart<PrimitiveList<T>, List<T>, List<T>> {
12+
13+
public PrimitiveList(ModSchema holder, String id) {
14+
super(holder, id, null);
15+
this.object = new ArrayList<>();
16+
}
17+
18+
@Override
19+
public List<T> toRaw(List<T> t) {
20+
return t;
21+
}
22+
23+
@Override
24+
public List<T> get() {
25+
return object;
26+
}
27+
28+
@Override
29+
public List<T> fromJsonElement(MainSchema main, JsonElement element) {
30+
return (List<T>) MyGSON.GSON.fromJson(element, object.getClass());
31+
}
32+
33+
@Override
34+
public void save(JsonObject json) {
35+
json.add(id, MyGSON.GSON.toJsonTree(object));
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.robertx22.test;
2+
3+
import java.util.List;
4+
5+
public class RegType<T> {
6+
7+
// todo
8+
9+
10+
public T getObject(String id) {
11+
return null; // todo
12+
}
13+
14+
public List<T> getAll() {
15+
return null; // todo
16+
}
17+
18+
public boolean hasObject(String id) {
19+
return true; // todo
20+
}
21+
22+
public String getRegistryName() {
23+
return "testreg"; // todo
24+
}
25+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.robertx22.test;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public class SchemaList<T extends ModSchema> extends SchemaPart<SchemaList<T>, List<T>, List<T>> {
11+
12+
public T emptySchema;
13+
14+
15+
public void add(T obj) {
16+
object.add(obj);
17+
}
18+
19+
@Override
20+
public List<T> toRaw(List<T> t) {
21+
return object;
22+
}
23+
24+
@Override
25+
public List<T> get() {
26+
return object;
27+
}
28+
29+
public SchemaList(ModSchema holder, String id, T emptySchema) {
30+
super(holder, id, null); // todo
31+
this.emptySchema = emptySchema;
32+
this.object = new ArrayList<>();
33+
}
34+
35+
36+
@Override
37+
public List<T> fromJsonElement(MainSchema main, JsonElement element) {
38+
List<T> list = new ArrayList<>();
39+
for (JsonElement ele : element.getAsJsonArray()) {
40+
var s = emptySchema.createNewInstance();
41+
s.load(main, ele.getAsJsonObject());
42+
list.add((T) s);
43+
}
44+
return list;
45+
}
46+
47+
@Override
48+
public void save(JsonObject json) {
49+
JsonArray array = new JsonArray();
50+
for (T t : object) {
51+
array.add(t.toJson());
52+
}
53+
json.add(id, array);
54+
}
55+
}

0 commit comments

Comments
 (0)