WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 3af7f5f

Browse files
committed
get most of the virtual pack stuff working
1 parent 106ec1a commit 3af7f5f

File tree

5 files changed

+231
-15
lines changed

5 files changed

+231
-15
lines changed

src/main/java/gay/debuggy/architecture_extensions/ArchitectureExtensions.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import gay.debuggy.architecture_extensions.api.ArchExIntegration;
66
import gay.debuggy.architecture_extensions.api.BlockGroup;
77
import gay.debuggy.architecture_extensions.api.BlockType;
8-
import gay.debuggy.architecture_extensions.resource.DataGeneration;
8+
import gay.debuggy.architecture_extensions.resource.MapBackedPack;
99
import gay.debuggy.architecture_extensions.staticdata.BlockGroupSchema;
1010
import gay.debuggy.staticdata.api.StaticData;
1111
import gay.debuggy.staticdata.api.StaticDataItem;
@@ -19,18 +19,16 @@
1919
import net.minecraft.registry.Registry;
2020
import net.minecraft.registry.RegistryKey;
2121
import net.minecraft.registry.RegistryKeys;
22-
import net.minecraft.resource.ResourceType;
2322
import net.minecraft.text.Text;
2423
import net.minecraft.util.Identifier;
25-
import org.jetbrains.annotations.NotNull;
2624
import org.slf4j.Logger;
2725
import org.slf4j.LoggerFactory;
2826

2927
import java.io.IOException;
3028
import java.util.List;
3129
import java.util.Set;
3230

33-
public class ArchitectureExtensions implements ModInitializer, PackRegistrationContext.Callback {
31+
public class ArchitectureExtensions implements ModInitializer {
3432
public static final String MOD_ID = "architecture_extensions";
3533
public static final Logger LOGGER = LoggerFactory.getLogger("Architecture Extensions");
3634

@@ -43,7 +41,7 @@ public class ArchitectureExtensions implements ModInitializer, PackRegistrationC
4341
public static final BlockCreationCallback CALLBACK_ADD_TO_ITEM_GROUP = (group, blockType, baseBlock, derivedBlock) ->
4442
ItemGroupUtil.pull(ArchitectureExtensions.ITEM_GROUP, blockType, baseBlock, derivedBlock.asItem());
4543

46-
public static final InMemoryPack RESOURCE_PACK = new InMemoryPack.Named("Architecture Extensions");
44+
public static final MapBackedPack RESOURCE_PACK = new MapBackedPack();
4745

4846
@Override
4947
public void onInitialize() {
@@ -93,15 +91,17 @@ public void onInitialize() {
9391
DeferredRegistration.init();
9492

9593
ItemGroupUtil.push();
96-
97-
ResourceLoader.get(ResourceType.SERVER_DATA).getRegisterDefaultPackEvent().register(this);
94+
95+
//ResourceManagerHelper.get(ResourceType.SERVER_DATA).getRegisterDefaultPackEvent().register(this);
9896
}
9997

98+
/*
10099
@Override
101100
public void onRegisterPack(@NotNull PackRegistrationContext context) {
102101
DataGeneration.generate(ResourceType.SERVER_DATA);
103102
context.addResourcePack(RESOURCE_PACK);
104103
}
104+
*/
105105

106106
public static Identifier id(String path) {
107107
return Identifier.of("architecture_extensions", path);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
/*
8+
* <a href="https://github.com/Sweet-Berry-Collective/Wandering-Wizardry/blob/1.21/common/src/main/java/dev/sweetberry/wwizardry/mixin/Mixin_ReloadableResourceManager.java">
9+
* Borrowed from Wandering Wizardry with Octal's permission.
10+
* </a>
11+
*/
12+
package gay.debuggy.architecture_extensions.mixin;
13+
14+
import gay.debuggy.architecture_extensions.ArchitectureExtensions;
15+
import net.minecraft.resource.MultiPackResourceManager;
16+
import net.minecraft.resource.ResourceReload;
17+
import net.minecraft.resource.pack.ResourcePack;
18+
import net.minecraft.util.Unit;
19+
import net.minecraft.resource.ResourceType;
20+
import org.spongepowered.asm.mixin.Final;
21+
import org.spongepowered.asm.mixin.Shadow;
22+
import org.spongepowered.asm.mixin.injection.At;
23+
import org.spongepowered.asm.mixin.injection.ModifyArg;
24+
import org.spongepowered.asm.mixin.injection.Inject;
25+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
26+
27+
import java.io.IOException;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import java.util.concurrent.CompletableFuture;
31+
import java.util.concurrent.Executor;
32+
33+
public class ReloadableResourceManagerMixin {
34+
@Shadow
35+
@Final
36+
ResourceType type;
37+
38+
39+
@Inject(
40+
method = "createReload",
41+
at = @At(
42+
value = "INVOKE",
43+
target = "Lnet/minecraft/resource/MultiPackResourceManager;<init>(Lnet/minecraft/resource/ResourceType;Ljava/util/List;)V",
44+
shift = At.Shift.BEFORE
45+
)
46+
)
47+
private void archEx$reloadGeneratedData(Executor prepareExecutor, Executor applyExecutor, CompletableFuture<Unit> initialStage, List<ResourcePack> packs, CallbackInfoReturnable<ResourceReload> cir) {
48+
var temp = new MultiPackResourceManager(type, packs);
49+
try {
50+
// (re)load arch-ex's virtual resource pack
51+
} catch (IOException ex) {}
52+
temp.close();
53+
}
54+
55+
@ModifyArg(
56+
method = "createReload",
57+
at = @At(
58+
value = "INVOKE",
59+
target = "Lnet/minecraft/server/packs/resources/MultiPackResourceManager;<init>(Lnet/minecraft/server/packs/PackType;Ljava/util/List;)V"
60+
)
61+
)
62+
private List<ResourcePack> archEx$getPacks(List<ResourcePack> old) {
63+
var packs = new ArrayList<>(old);
64+
packs.add(ArchitectureExtensions.RESOURCE_PACK); // add arch-ex's virtual resource pack
65+
return packs;
66+
}
67+
}

src/main/java/gay/debuggy/architecture_extensions/resource/DataGeneration.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,44 +90,44 @@ private static void generateModels(TypedGroupedBlock block) {
9090
if (rawModel.contains(textureId)) model.addTexture(textureId, textureConfiguration.apply(block.type(), textureId));
9191
}
9292

93-
ArchitectureExtensions.RESOURCE_PACK.putTextAsync(ResourceType.CLIENT_RESOURCES, Identifier.of(modelId.getNamespace(), modelId.getPath() + variant + ".json"), path -> model.serialize().toString());
93+
ArchitectureExtensions.RESOURCE_PACK.put(ResourceType.CLIENT_RESOURCES, Identifier.of(modelId.getNamespace(), modelId.getPath() + variant + ".json"), model.serialize().toString());
9494
}
9595

9696
var itemModel = "{\"parent\":\"" + modelId.toString().replaceFirst("models/", "") + "\"}";
97-
ArchitectureExtensions.RESOURCE_PACK.putText(ResourceType.CLIENT_RESOURCES, Identifier.of(modelId.getNamespace(), modelId.getPath().replaceFirst("block", "item") + ".json"), itemModel);
97+
ArchitectureExtensions.RESOURCE_PACK.put(ResourceType.CLIENT_RESOURCES, Identifier.of(modelId.getNamespace(), modelId.getPath().replaceFirst("block", "item") + ".json"), itemModel);
9898
}
9999

100100
private static void generateBlockState(TypedGroupedBlock block) {
101101
var rawBlockState = getBlockStateTemplate(block.type());
102102
if (rawBlockState == null) return;
103103
var blockState = new BlockStateTemplate(rawBlockState);
104104
blockState.addConstant(MODEL_PLACEHOLDER, Identifier.of(block.id().getNamespace(), "block/" + block.id().getPath()).toString());
105-
ArchitectureExtensions.RESOURCE_PACK.putTextAsync(ResourceType.CLIENT_RESOURCES, Identifier.of(block.id().getNamespace(), "blockstates/" + block.id().getPath() + ".json"), path -> blockState.serialize().toString());
105+
ArchitectureExtensions.RESOURCE_PACK.put(ResourceType.CLIENT_RESOURCES, Identifier.of(block.id().getNamespace(), "blockstates/" + block.id().getPath() + ".json"), blockState.serialize().toString());
106106
}
107107

108108
private static void generateMineableByPickaxeTag() {
109109
var tag = TagTemplate.DEFAULT.get();
110110
BLOCKS.forEach(block -> { if (BlockContentRegistries.STRIPPABLE.get(block.groupedBlock().baseBlock().get()).isEmpty()) tag.addValue(block.id().toString()); });
111-
ArchitectureExtensions.RESOURCE_PACK.putTextAsync(ResourceType.SERVER_DATA, Identifier.of("tags/blocks/mineable/pickaxe.json"), path -> tag.serialize().toString());
111+
ArchitectureExtensions.RESOURCE_PACK.put(ResourceType.SERVER_DATA, Identifier.of("tags/blocks/mineable/pickaxe.json"), tag.serialize().toString());
112112
}
113113

114114
private static void generateMineableByAxeTag() {
115115
var tag = TagTemplate.DEFAULT.get();
116116
BLOCKS.forEach(block -> { if (BlockContentRegistries.STRIPPABLE.get(block.groupedBlock().baseBlock().get()).isPresent()) tag.addValue(block.id().toString()); });
117-
ArchitectureExtensions.RESOURCE_PACK.putTextAsync(ResourceType.SERVER_DATA, Identifier.of("tags/blocks/mineable/axe.json"), path -> tag.serialize().toString());
117+
ArchitectureExtensions.RESOURCE_PACK.put(ResourceType.SERVER_DATA, Identifier.of("tags/blocks/mineable/axe.json"), tag.serialize().toString());
118118
}
119119

120120
private static void generateNeedsStoneToolTag() {
121121
var tag = TagTemplate.DEFAULT.get();
122122
BLOCKS.forEach(block -> tag.addValue(block.id().toString()));
123-
ArchitectureExtensions.RESOURCE_PACK.putTextAsync(ResourceType.SERVER_DATA, Identifier.of("tags/blocks/needs_stone_tool.json"), path -> tag.serialize().toString());
123+
ArchitectureExtensions.RESOURCE_PACK.put(ResourceType.SERVER_DATA, Identifier.of("tags/blocks/needs_stone_tool.json"), tag.serialize().toString());
124124
}
125125

126126
private static void generateLootTables() {
127127
for (TypedGroupedBlock block : BLOCKS) {
128128
var lootTable = LootTableTemplate.BLOCK_BREAK.get();
129129
lootTable.addPool(JPool.ofItems(block.id()).addCondition(JCondition.SURVIVES_EXPLOSION.get()));
130-
ArchitectureExtensions.RESOURCE_PACK.putTextAsync(ResourceType.SERVER_DATA, Identifier.of(block.id().getNamespace(), "loot_tables/blocks/" + block.id().getPath() + ".json"), path -> lootTable.serialize().toString());
130+
ArchitectureExtensions.RESOURCE_PACK.put(ResourceType.SERVER_DATA, Identifier.of(block.id().getNamespace(), "loot_tables/blocks/" + block.id().getPath() + ".json"), lootTable.serialize().toString());
131131
}
132132
}
133133

@@ -146,7 +146,7 @@ private static void generateRecipes() {
146146

147147
final var path = template.tablesaw() ? "custom_recipes/tablesaw/" : "recipes/";
148148
final var prefix = template.simple() ? "" : block.id().getPath() + "_";
149-
ArchitectureExtensions.RESOURCE_PACK.putTextAsync(ResourceType.SERVER_DATA, Identifier.of(block.id().getNamespace(), path + prefix + template.id() + ".json"), blah -> recipe.serialize().toString());
149+
ArchitectureExtensions.RESOURCE_PACK.put(ResourceType.SERVER_DATA, Identifier.of(block.id().getNamespace(), path + prefix + template.id() + ".json"), recipe.serialize().toString());
150150
}
151151
templates.clear();
152152
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
/*
8+
* <a href="https://github.com/Sweet-Berry-Collective/Wandering-Wizardry/blob/1.21/common/src/main/java/dev/sweetberry/wwizardry/api/resource/MapBackedPack.java">
9+
* Borrowed from Wandering Wizardry with Octal's permission.
10+
* </a>
11+
*/
12+
package gay.debuggy.architecture_extensions.resource;
13+
14+
import gay.debuggy.architecture_extensions.ArchitectureExtensions;
15+
import net.minecraft.resource.ResourceIoSupplier;
16+
import net.minecraft.resource.ResourceType;
17+
import net.minecraft.resource.pack.PackLocationInfo;
18+
import net.minecraft.resource.pack.PackSource;
19+
import net.minecraft.resource.pack.ResourcePack;
20+
import net.minecraft.resource.pack.metadata.ResourceMetadataSectionReader;
21+
import net.minecraft.text.Text;
22+
import net.minecraft.util.Identifier;
23+
import org.jetbrains.annotations.Nullable;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.nio.charset.StandardCharsets;
29+
import java.util.Map;
30+
import java.util.Optional;
31+
import java.util.Set;
32+
import java.util.concurrent.ConcurrentHashMap;
33+
import java.util.stream.Collectors;
34+
35+
public class MapBackedPack implements ResourcePack {
36+
private final Map<Identifier, byte[]> assets = new ConcurrentHashMap<>();
37+
private final Map<Identifier, byte[]> data = new ConcurrentHashMap<>();
38+
private final Map<String, byte[]> root = new ConcurrentHashMap<>();
39+
40+
public void put(ResourceType type, Identifier id, byte[] bytes) {
41+
getForType(type).put(id, bytes);
42+
}
43+
44+
public void put(String path, byte[] bytes) {
45+
root.put(path, bytes);
46+
}
47+
48+
public void put(ResourceType type, Identifier id, String text) {
49+
put(type, id, text.getBytes(StandardCharsets.UTF_8));
50+
}
51+
52+
public void put(String path, String text) {
53+
put(path, text.getBytes(StandardCharsets.UTF_8));
54+
}
55+
56+
@Override
57+
public @Nullable ResourceIoSupplier<InputStream> openRoot(String... path) {
58+
return open(root, String.join("/", path));
59+
}
60+
61+
@Override
62+
public @Nullable ResourceIoSupplier<InputStream> open(ResourceType type, Identifier id) {
63+
return open(getForType(type), id);
64+
}
65+
66+
@Override
67+
public void listResources(ResourceType type, String namespace, String startingPath, ResourceConsumer consumer) {
68+
getForType(type)
69+
.entrySet()
70+
.stream()
71+
.filter(entry ->
72+
entry.getKey().getNamespace().equals(namespace)
73+
&& entry.getKey().getPath().startsWith(startingPath)
74+
).forEach(entry -> {
75+
var bytes = entry.getValue();
76+
if (bytes == null)
77+
return;
78+
consumer.accept(entry.getKey(), () -> new ByteArrayInputStream(bytes));
79+
});
80+
}
81+
82+
@Override
83+
public Set<String> getNamespaces(ResourceType type) {
84+
return getForType(type)
85+
.keySet()
86+
.stream()
87+
.map(Identifier::getNamespace)
88+
.collect(Collectors.toUnmodifiableSet());
89+
}
90+
91+
@Override
92+
public @Nullable <T> T parseMetadata(ResourceMetadataSectionReader<T> metaReader) throws IOException {
93+
return null;
94+
}
95+
96+
@Override
97+
public PackLocationInfo getLocationInfo() {
98+
return new PackLocationInfo(ArchitectureExtensions.MOD_ID, Text.empty(), PackSource.PACK_SOURCE_BUILTIN, Optional.empty());
99+
}
100+
101+
@Nullable
102+
public <T> T getMetadataSection(ResourceMetadataSectionReader<T> metaReader) throws IOException {
103+
return null;
104+
}
105+
106+
public String packId() {
107+
return "Arch-Ex Resources";
108+
}
109+
110+
@Override
111+
public void close() {
112+
113+
}
114+
115+
public void clear() {
116+
root.clear();
117+
assets.clear();
118+
data.clear();
119+
}
120+
121+
public void clear(ResourceType type) {
122+
getForType(type).clear();
123+
}
124+
125+
private Map<Identifier, byte[]> getForType(ResourceType type) {
126+
if (type == ResourceType.CLIENT_RESOURCES)
127+
return assets;
128+
return data;
129+
}
130+
131+
private <T> ResourceIoSupplier<InputStream> open(Map<T, byte[]> map, T path) {
132+
var bytes = map.get(path);
133+
if (bytes == null)
134+
return null;
135+
return () -> new ByteArrayInputStream(bytes);
136+
}
137+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"required": true,
3+
"minVersion": "0.8",
4+
"package": "gay.debuggy.architecture_extensions.mixin",
5+
"compatibilityLevel": "JAVA_21",
6+
"mixins": [
7+
"ReloadableResourceManagerMixin"
8+
],
9+
"injectors": {
10+
"defaultRequire": 1
11+
}
12+
}

0 commit comments

Comments
 (0)