-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability for addons to provide a datapack
- Loading branch information
1 parent
e140920
commit b22c6ab
Showing
5 changed files
with
165 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...n/java/net/kyrptonaught/serverutils/customMapLoader/addons/AddonResourcePackProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package net.kyrptonaught.serverutils.customMapLoader.addons; | ||
|
||
import net.minecraft.resource.*; | ||
import net.minecraft.resource.fs.ResourceFileSystem; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.path.SymlinkEntry; | ||
import net.minecraft.util.path.SymlinkFinder; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
public class AddonResourcePackProvider implements ResourcePackProvider { | ||
public static final ResourcePackSource LEMADDON = ResourcePackSource.create(name -> name, true); | ||
private static final ResourcePackPosition POSITION = new ResourcePackPosition(false, ResourcePackProfile.InsertionPosition.TOP, false); | ||
|
||
private final HashMap<Identifier, Path> loadedAddons = new HashMap<>(); | ||
|
||
public void loadAddon(Identifier id, Path path) { | ||
loadedAddons.put(id, path.resolve("datapack")); | ||
} | ||
|
||
public void unloadAddon(Identifier id) { | ||
loadedAddons.remove(id); | ||
} | ||
|
||
@Override | ||
public void register(Consumer<ResourcePackProfile> profileAdder) { | ||
loadedAddons.forEach((id, path) -> { | ||
try { | ||
ResourcePackProfile.PackFactory packFactory = new PackOpenerImpl(new SymlinkFinder(path2 -> true)).open(path, List.of()); | ||
|
||
ResourcePackInfo resourcePackInfo = new ResourcePackInfo(getID(id), Text.literal(id.toString()), LEMADDON, Optional.empty()); | ||
ResourcePackProfile resourcePackProfile = ResourcePackProfile.create(resourcePackInfo, packFactory, ResourceType.SERVER_DATA, POSITION); | ||
if (resourcePackProfile != null) { | ||
profileAdder.accept(resourcePackProfile); | ||
} | ||
} catch (IOException iOException) { | ||
System.out.println("Error loading lemaddon data pack: " + id); | ||
} | ||
|
||
}); | ||
|
||
} | ||
|
||
public static String getID(Identifier identifier) { | ||
return "lemaddon/" + identifier; | ||
} | ||
|
||
static class PackOpenerImpl extends ResourcePackOpener<ResourcePackProfile.PackFactory> { | ||
protected PackOpenerImpl(SymlinkFinder symlinkFinder) { | ||
super(symlinkFinder); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public ResourcePackProfile.PackFactory open(Path path, List<SymlinkEntry> foundSymlinks) throws IOException { | ||
return new DirectoryResourcePack.DirectoryBackedFactory(path); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
protected ResourcePackProfile.PackFactory openZip(Path path) { | ||
FileSystem fileSystem = path.getFileSystem(); | ||
if (fileSystem == FileSystems.getDefault() || fileSystem instanceof ResourceFileSystem) { | ||
return new ZipResourcePack.ZipBackedFactory(path); | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
protected ResourcePackProfile.PackFactory openDirectory(Path path) { | ||
return new DirectoryResourcePack.DirectoryBackedFactory(path); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...java/net/kyrptonaught/serverutils/mixin/customMapLoader/VanillaDataPackProviderMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package net.kyrptonaught.serverutils.mixin.customMapLoader; | ||
|
||
import net.kyrptonaught.serverutils.customMapLoader.CustomMapLoaderMod; | ||
import net.minecraft.resource.ResourcePackProvider; | ||
import net.minecraft.resource.VanillaDataPackProvider; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.ModifyArgs; | ||
import org.spongepowered.asm.mixin.injection.invoke.arg.Args; | ||
|
||
import java.util.Arrays; | ||
|
||
@Mixin(VanillaDataPackProvider.class) | ||
public class VanillaDataPackProviderMixin { | ||
|
||
|
||
@ModifyArgs(method = "createManager(Ljava/nio/file/Path;Lnet/minecraft/util/path/SymlinkFinder;)Lnet/minecraft/resource/ResourcePackManager;", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourcePackManager;<init>([Lnet/minecraft/resource/ResourcePackProvider;)V")) | ||
private static void registerAddonProvider(Args args) { | ||
ResourcePackProvider[] packs = Arrays.copyOf(((ResourcePackProvider[]) args.get(0)), ((ResourcePackProvider[]) args.get(0)).length + 1); | ||
packs[packs.length - 1] = CustomMapLoaderMod.ADDON_PROVIDER; | ||
args.set(0, packs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters