tweak datapack loading

This commit is contained in:
asie 2018-11-29 09:18:19 +01:00
parent df2790c68d
commit cc48548ff9
5 changed files with 95 additions and 6 deletions

View file

@ -16,7 +16,11 @@
package net.fabricmc.fabric.mixin.resources;
import net.fabricmc.fabric.resources.ModDataPackSupplier;
import net.fabricmc.fabric.resources.ModResourcePack;
import net.fabricmc.fabric.resources.ModResourcePackUtil;
import net.minecraft.class_3283;
import net.minecraft.class_3288;
import net.minecraft.client.MinecraftGame;
import net.minecraft.resource.ReloadableResourceManager;
import net.minecraft.resource.ResourcePack;
@ -30,15 +34,26 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@Mixin(MinecraftServer.class)
public class MixinMinecraftServer {
@Shadow
private ReloadableResourceManager dataManager;
private class_3283<class_3288> field_4595;
@Inject(method = "reloadDataPacks", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ReloadableResourceManager;reload(Ljava/util/List;)V", ordinal = 0), locals = LocalCapture.CAPTURE_FAILHARD)
public void reloadDataPacks(LevelProperties properties, CallbackInfo info, List<ResourcePack> list) {
ModResourcePackUtil.appendModResourcePacks(list, ResourceType.DATA);
@Inject(method = "method_3800", at = @At(value = "INVOKE", target = "Lnet/minecraft/class_3283;method_14443(Lnet/minecraft/class_3285;)V", ordinal = 1))
public void method_3800(File file, LevelProperties properties, CallbackInfo info) {
// TODO: "vanilla" does not emit a message; neither should a modded datapack
List<ResourcePack> packs = new ArrayList<>();
ModResourcePackUtil.appendModResourcePacks(packs, ResourceType.DATA);
for (ResourcePack pack : packs) {
if (!(pack instanceof ModResourcePack)) {
throw new RuntimeException("Not a ModResourcePack!");
}
field_4595.method_14443(new ModDataPackSupplier((ModResourcePack) pack));
}
}
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2016, 2017, 2018 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.resources;
import net.minecraft.class_3285;
import net.minecraft.class_3288;
import java.util.Map;
public class ModDataPackSupplier implements class_3285 {
private final ModResourcePack pack;
public ModDataPackSupplier(ModResourcePack pack) {
this.pack = pack;
}
@Override
public <T extends class_3288> void method_14453(Map<String, T> map, class_3288.class_3290<T> class_3290) {
T var3 = class_3288.method_14456("fabric:" + pack.getModInfo().getId(),
false, () -> this.pack, class_3290, class_3288.class_3289.BOTTOM);
if (var3 != null) {
map.put(var3.method_14463(), var3);
}
}
}

View file

@ -22,7 +22,7 @@ import net.minecraft.resource.ResourceNotFoundException;
import java.io.*;
public class ModDirectoryResourcePack extends DirectoryResourcePack {
public class ModDirectoryResourcePack extends DirectoryResourcePack implements ModResourcePack {
private final ModInfo info;
public ModDirectoryResourcePack(ModInfo info, File file) {
@ -52,4 +52,9 @@ public class ModDirectoryResourcePack extends DirectoryResourcePack {
protected boolean containsFilename(String filename) {
return super.containsFilename(filename) || ModResourcePackUtil.containsDefault(info, filename);
}
@Override
public ModInfo getModInfo() {
return info;
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2016, 2017, 2018 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.resources;
import net.fabricmc.loader.ModInfo;
import net.minecraft.resource.ResourcePack;
public interface ModResourcePack extends ResourcePack {
ModInfo getModInfo();
}

View file

@ -26,7 +26,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class ModZipResourcePack extends ZipResourcePack {
public class ModZipResourcePack extends ZipResourcePack implements ModResourcePack {
private final ModInfo info;
public ModZipResourcePack(ModInfo info, File file) {
@ -56,4 +56,9 @@ public class ModZipResourcePack extends ZipResourcePack {
public boolean containsFilename(String filename) {
return super.containsFilename(filename) || ModResourcePackUtil.containsDefault(info, filename);
}
@Override
public ModInfo getModInfo() {
return info;
}
}