Skip to content

Commit 8b959aa

Browse files
committed
Make Ancient Cyphers datapackable
1 parent 941f8d6 commit 8b959aa

File tree

22 files changed

+282
-38
lines changed

22 files changed

+282
-38
lines changed

Common/src/main/java/at/petrak/hexcasting/common/items/magic/ItemAncientCypher.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.List;
1515

1616
public class ItemAncientCypher extends ItemCypher {
17-
public static final String TAG_PATTERNS = "patterns";
1817
public static final String TAG_HEX_NAME = "hex_name";
1918

2019
public ItemAncientCypher(Properties pProperties) {
@@ -45,7 +44,7 @@ public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Compo
4544
super.appendHoverText(pStack, pLevel, pTooltipComponents, pIsAdvanced);
4645

4746
// also show contained spell
48-
var patternsTag = NBTHelper.getList(pStack, TAG_PATTERNS, Tag.TAG_COMPOUND);
47+
var patternsTag = NBTHelper.getList(pStack, TAG_PROGRAM, Tag.TAG_COMPOUND);
4948
if (patternsTag != null) {
5049
var storedHex = Component.translatable("hexcasting.tooltip.stored_hex");
5150
for (var iotaTag : patternsTag) {
Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package at.petrak.hexcasting.common.loot;
22

3-
import at.petrak.hexcasting.api.casting.iota.PatternIota;
43
import at.petrak.hexcasting.api.casting.iota.IotaType;
5-
import at.petrak.hexcasting.api.casting.math.HexDir;
6-
import at.petrak.hexcasting.api.casting.math.HexPattern;
7-
import at.petrak.hexcasting.api.mod.HexConfig;
8-
import at.petrak.hexcasting.api.utils.HexUtils;
94
import at.petrak.hexcasting.api.item.VariantItem;
105
import at.petrak.hexcasting.api.misc.MediaConstants;
116
import at.petrak.hexcasting.common.items.magic.ItemAncientCypher;
7+
import at.petrak.hexcasting.common.items.magic.ItemPackagedHex;
128
import at.petrak.hexcasting.common.lib.HexLootFunctions;
139
import com.google.gson.JsonDeserializationContext;
1410
import com.google.gson.JsonObject;
@@ -22,9 +18,6 @@
2218
import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType;
2319
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
2420

25-
import java.util.List;
26-
import com.mojang.datafixers.util.Pair;
27-
2821
/**
2922
* Add a random preset hex to the ancient cypher, and select a random variant.
3023
* <p>
@@ -40,20 +33,18 @@ public AddHexToAncientCypherFunc(LootItemCondition[] lootItemConditions) {
4033
* This doesn't actually have any params so extract behaviour out for the benefit of forge
4134
*/
4235
public static ItemStack doStatic(ItemStack stack, RandomSource rand) {
43-
var hex = LOOT_HEXES.get(rand.nextInt(LOOT_HEXES.size()));
36+
var hex = AncientCypherManager.INSTANCE.randomHex(rand);
4437
var patsTag = new ListTag();
45-
for (var patString : hex.getSecond()){
46-
var pieces = patString.split(" ");
47-
var pat = HexPattern.fromAngles(pieces[1],HexDir.fromString(pieces[0]));
48-
patsTag.add(IotaType.serialize(new PatternIota(pat)));
38+
for (var pat : hex.getSecond()){
39+
patsTag.add(IotaType.serialize(pat));
4940
}
5041

5142
var tag = new CompoundTag();
52-
tag.putString(ItemAncientCypher.TAG_HEX_NAME, hex.getFirst());
43+
tag.putString(ItemAncientCypher.TAG_HEX_NAME, "hexcasting.loot_hex." + hex.getFirst());
5344
tag.putLong(ItemAncientCypher.TAG_MEDIA, 32*MediaConstants.SHARD_UNIT);
5445
tag.putLong(ItemAncientCypher.TAG_MAX_MEDIA, 32*MediaConstants.SHARD_UNIT);
5546
tag.putInt(VariantItem.TAG_VARIANT, rand.nextInt(8));
56-
tag.put(ItemAncientCypher.TAG_PATTERNS, patsTag);
47+
tag.put(ItemPackagedHex.TAG_PROGRAM, patsTag);
5748
stack.getOrCreateTag().merge(tag);
5849

5950
return stack;
@@ -81,24 +72,4 @@ public AddHexToAncientCypherFunc deserialize(JsonObject object, JsonDeserializat
8172
return new AddHexToAncientCypherFunc(conditions);
8273
}
8374
}
84-
85-
// TODO: make this datapackable
86-
private static final List<Pair<String, String[]>> LOOT_HEXES = List.of(
87-
new Pair<>("hexcasting.loot_hex.shatter", new String[] {"NORTH_EAST qaq","EAST aa","NORTH_EAST qaq","NORTH_EAST wa","EAST wqaawdd","EAST qaqqqqq"}),
88-
new Pair<>("hexcasting.loot_hex.kindle", new String[] {"NORTH_EAST qaq","EAST aa","NORTH_EAST qaq","NORTH_EAST wa","EAST wqaawdd","SOUTH_EAST aaqawawa"}),
89-
new Pair<>("hexcasting.loot_hex.illuminate", new String[] {"NORTH_EAST qaq","EAST aa","NORTH_EAST qaq","NORTH_EAST wa","EAST aadadaaw","EAST wqaawdd","NORTH_EAST ddqdd","EAST weddwaa","NORTH_EAST waaw","NORTH_EAST qqd"}),
90-
new Pair<>("hexcasting.loot_hex.growth", new String[] {"NORTH_EAST qaq","EAST aa","NORTH_EAST qaq","NORTH_EAST wa","EAST aadadaaw","EAST wqaawdd","NORTH_EAST ddqdd","EAST weddwaa","NORTH_EAST waaw","SOUTH_EAST aqaaedwd","EAST aadaadaa","NORTH_EAST wqaqwawqaqw","NORTH_EAST wqaqwawqaqw","NORTH_EAST wqaqwawqaqw"}),
91-
new Pair<>("hexcasting.loot_hex.lunge", new String[] {"NORTH_EAST qaq","EAST aadaa","NORTH_EAST wa","SOUTH_EAST aqaawa","SOUTH_EAST waqaw","SOUTH_WEST awqqqwaqw"}),
92-
new Pair<>("hexcasting.loot_hex.sidestep", new String[] {"NORTH_EAST qaq","EAST aadaa","NORTH_EAST wa","NORTH_WEST eqqq","SOUTH_EAST aqaawd","SOUTH_EAST e","NORTH_WEST qqqqqew","SOUTH_WEST eeeeeqw","SOUTH_EAST awdd","NORTH_EAST wdedw","SOUTH_WEST awqqqwaqw"}),
93-
new Pair<>("hexcasting.loot_hex.ascend", new String[] {"NORTH_EAST qaq","SOUTH_EAST aqaae","WEST qqqqqawwawawd"}),
94-
new Pair<>("hexcasting.loot_hex.blink", new String[] {"NORTH_EAST qaq","EAST aadaa","EAST aa","NORTH_EAST qaq","NORTH_EAST wa","EAST wqaawdd","NORTH_EAST qaq","EAST aa","NORTH_WEST wddw","NORTH_EAST wqaqw","SOUTH_EAST aqaaw","NORTH_WEST wddw","SOUTH_WEST awqqqwaq"}),
95-
new Pair<>("hexcasting.loot_hex.blastoff", new String[] {"NORTH_EAST qaq","NORTH_WEST qqqqqew","SOUTH_EAST aqaawaa","SOUTH_EAST waqaw","SOUTH_WEST awqqqwaqw"}),
96-
new Pair<>("hexcasting.loot_hex.radar", new String[] {"WEST qqq","EAST aadaa","EAST aa","SOUTH_EAST aqaawa","SOUTH_WEST ewdqdwe","NORTH_EAST de","EAST eee","NORTH_EAST qaq","EAST aa","SOUTH_EAST aqaaeaqq","SOUTH_EAST qqqqqwdeddwd","NORTH_EAST dadad"}),
97-
new Pair<>("hexcasting.loot_hex.beckon", new String[] {"NORTH_EAST qaq","EAST aa","NORTH_EAST qaq","NORTH_EAST wa","EAST weaqa","EAST aadaa","EAST dd","NORTH_EAST qaq","EAST aa","EAST aawdd","NORTH_WEST wddw","EAST aadaa","NORTH_EAST wqaqw","NORTH_EAST wdedw","SOUTH_EAST aqaawa","SOUTH_EAST waqaw","SOUTH_WEST awqqqwaqw"}),
98-
new Pair<>("hexcasting.loot_hex.detonate", new String[] {"NORTH_EAST qaq","EAST aa","SOUTH_EAST aqaaedwd","EAST ddwddwdd"}),
99-
new Pair<>("hexcasting.loot_hex.shockwave", new String[] {"NORTH_EAST qaq","EAST aa","SOUTH_EAST aqaawaa","EAST aadaadaa","SOUTH_EAST aqawqadaq","SOUTH_EAST aqaaedwd","EAST aawaawaa","NORTH_EAST qqa","EAST qaqqqqq"}),
100-
new Pair<>("hexcasting.loot_hex.heat_wave", new String[] {"WEST qqq","SOUTH_EAST aaqawawa","EAST eee","NORTH_EAST qaq","EAST aa","SOUTH_EAST aqaae","SOUTH_EAST qqqqqwded","SOUTH_WEST aaqwqaa","SOUTH_EAST a","NORTH_EAST dadad"}),
101-
new Pair<>("hexcasting.loot_hex.wither_wave", new String[] {"WEST qqq","SOUTH_EAST aqaae","SOUTH_EAST aqaaw","SOUTH_WEST qqqqqaewawawe","EAST eee","NORTH_EAST qaq","EAST aa","SOUTH_EAST aqaae","SOUTH_EAST qqqqqwdeddwd","SOUTH_WEST aaqwqaa","SOUTH_EAST a","NORTH_EAST dadad"}),
102-
new Pair<>("hexcasting.loot_hex.flight_zone", new String[] {"NORTH_EAST qaq","SOUTH_EAST aqaaq","SOUTH_WEST awawaawq"})
103-
);
10475
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package at.petrak.hexcasting.common.loot;
2+
3+
import at.petrak.hexcasting.api.HexAPI;
4+
import at.petrak.hexcasting.api.casting.iota.Iota;
5+
import at.petrak.hexcasting.api.casting.iota.PatternIota;
6+
import at.petrak.hexcasting.api.casting.math.HexPattern;
7+
import com.google.gson.Gson;
8+
import com.google.gson.JsonElement;
9+
import com.mojang.datafixers.util.Pair;
10+
import com.mojang.serialization.JsonOps;
11+
import net.minecraft.resources.ResourceLocation;
12+
import net.minecraft.server.packs.resources.ResourceManager;
13+
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;
14+
import net.minecraft.util.RandomSource;
15+
import net.minecraft.util.profiling.ProfilerFiller;
16+
17+
import java.util.ArrayList;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
public class AncientCypherManager extends SimpleJsonResourceReloadListener {
23+
public static final AncientCypherManager INSTANCE = new AncientCypherManager();
24+
25+
private Map<ResourceLocation, List<Iota>> data;
26+
27+
private AncientCypherManager() {
28+
super(new Gson(), "loot_cyphers");
29+
}
30+
31+
@Override
32+
protected Map<ResourceLocation, JsonElement> prepare(ResourceManager $$0, ProfilerFiller $$1) {
33+
return super.prepare($$0, $$1);
34+
}
35+
36+
@Override
37+
protected void apply(Map<ResourceLocation, JsonElement> map, ResourceManager resourceManager, ProfilerFiller profilerFiller) {
38+
Map<ResourceLocation, List<Iota>> data = new HashMap<>(map.size());
39+
for (var entry : map.entrySet()) {
40+
var key = entry.getKey();
41+
try {
42+
var value = entry.getValue().getAsJsonArray();
43+
var iotaList = new ArrayList<Iota>(value.size());
44+
for (var elem : value) {
45+
var pattern = HexPattern.CODEC.parse(JsonOps.INSTANCE, elem).resultOrPartial(HexAPI.LOGGER::error).orElseThrow();
46+
iotaList.add(new PatternIota(pattern));
47+
}
48+
data.put(key, iotaList);
49+
} catch (Exception e) {
50+
HexAPI.LOGGER.error("Error loading custom loot cypher {}: {}", key, e.getMessage());
51+
}
52+
}
53+
this.data = data;
54+
}
55+
56+
public Pair<ResourceLocation, List<Iota>> randomHex(RandomSource rand) {
57+
var map = this.data;
58+
var entries = map.entrySet().stream().toList();
59+
var entry = entries.get(rand.nextInt(entries.size()));
60+
return Pair.of(entry.getKey(), entry.getValue());
61+
}
62+
}

Common/src/main/resources/assets/hexcasting/lang/en_us.flatten.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@
523523
infinite_media: "Infinite",
524524
},
525525

526-
loot_hex: {
526+
"loot_hex.hexcasting:": {
527527
"shatter": "Shatter",
528528
"kindle": "Kindle",
529529
"illuminate": "Illuminate",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
3+
{ "start_dir": "SOUTH_EAST", "angles": "aqaae" },
4+
{ "start_dir": "WEST", "angles": "qqqqqawwawawd" }
5+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
3+
{ "start_dir": "EAST", "angles": "aa" },
4+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
5+
{ "start_dir": "NORTH_EAST", "angles": "wa" },
6+
{ "start_dir": "EAST", "angles": "weaqa" },
7+
{ "start_dir": "EAST", "angles": "aadaa" },
8+
{ "start_dir": "EAST", "angles": "dd" },
9+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
10+
{ "start_dir": "EAST", "angles": "aa" },
11+
{ "start_dir": "EAST", "angles": "aawdd" },
12+
{ "start_dir": "NORTH_WEST", "angles": "wddw" },
13+
{ "start_dir": "EAST", "angles": "aadaa" },
14+
{ "start_dir": "NORTH_EAST", "angles": "wqaqw" },
15+
{ "start_dir": "NORTH_EAST", "angles": "wdedw" },
16+
{ "start_dir": "SOUTH_EAST", "angles": "aqaawa" },
17+
{ "start_dir": "SOUTH_EAST", "angles": "waqaw" },
18+
{ "start_dir": "SOUTH_WEST", "angles": "awqqqwaqw" }
19+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
3+
{ "start_dir": "NORTH_WEST", "angles": "qqqqqew" },
4+
{ "start_dir": "SOUTH_EAST", "angles": "aqaawaa" },
5+
{ "start_dir": "SOUTH_EAST", "angles": "waqaw" },
6+
{ "start_dir": "SOUTH_WEST", "angles": "awqqqwaqw" }
7+
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[
2+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
3+
{ "start_dir": "EAST", "angles": "aadaa" },
4+
{ "start_dir": "EAST", "angles": "aa" },
5+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
6+
{ "start_dir": "NORTH_EAST", "angles": "wa" },
7+
{ "start_dir": "EAST", "angles": "wqaawdd" },
8+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
9+
{ "start_dir": "EAST", "angles": "aa" },
10+
{ "start_dir": "NORTH_WEST", "angles": "wddw" },
11+
{ "start_dir": "NORTH_EAST", "angles": "wqaqw" },
12+
{ "start_dir": "SOUTH_EAST", "angles": "aqaaw" },
13+
{ "start_dir": "NORTH_WEST", "angles": "wddw" },
14+
{ "start_dir": "SOUTH_WEST", "angles": "awqqqwaq" }
15+
]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
3+
{ "start_dir": "EAST", "angles": "aa" },
4+
{ "start_dir": "SOUTH_EAST", "angles": "aqaaedwd" },
5+
{ "start_dir": "EAST", "angles": "ddwddwdd" }
6+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{ "start_dir": "NORTH_EAST", "angles": "qaq" },
3+
{ "start_dir": "SOUTH_EAST", "angles": "aqaaq" },
4+
{ "start_dir": "SOUTH_WEST", "angles": "awawaawq" }
5+
]

0 commit comments

Comments
 (0)