-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
263 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
# Datapack Installer | ||
A Minecraft (Fabric) mod that allows you to put your datapacks in a folder and | ||
be able to apply them at world creation without copying them to the temporary | ||
folder or the world folder. | ||
The mod should be installed on the client. | ||
A Minecraft (Fabric) mod. When installed on the client, data packs put into the | ||
data pack folder will be available for selection during world creation as well | ||
as while paused in Singleplayer. When installed on the server, data packs put | ||
into the data pack folder will be copied to the active world when the server | ||
starts. | ||
|
||
### How does it work? | ||
It's very simple, all it does is copy the files and folders from the | ||
installed_datapacks folder inside the game data directory to the temporary | ||
folder for the datapack selector when creating the world. | ||
### Advantages | ||
On the client, this mod works with the ingame resource and data pack selector. | ||
While creating a world, or while paused in Singleplayer, you can choose which | ||
data packs to enable or disable with an interactive screen. | ||
|
||
### Why should I use this over other "global datapack" mods? | ||
This mod supports both compressed datapacks (.zip) and uncompressed datapacks | ||
(folders), so you won't have trouble with multiple types of datapacks. You also | ||
get to choose what datapacks to apply during world creation instead of having | ||
all of the datapacks in the folder applied when you create the world. | ||
On the server, all that happens is that the data packs are copied from the | ||
mod's data pack folder to the world data pack folder whenever the server | ||
starts. If you are switching worlds or deleting them to regenerate often, all | ||
you will need to do is switch or delete the world, and the data packs will | ||
carry on if you leave the mod's folder intact. | ||
|
||
### Disadvantages | ||
This mod cannot keep data packs that require feature flags enabled or disabled. | ||
For example, if you did not create a world with the built-in bundle data pack | ||
enabled, and you enable the data pack with the mod, the data pack will only be | ||
temporarily enabled until you quit the world. | ||
|
||
### Usage | ||
Step-by-step instructions to use this mod. | ||
1. Insert your desired datapack(s) into .minecraft/installed_datapacks | ||
1. If you have started the game with this mod once, it should already be | ||
in the game+launcher data directory (.minecraft). | ||
#### Client | ||
1. Insert your desired data pack(s) into `datapacks` in the game's data folder | ||
1. If you have started the game with this mod installed, it should already | ||
be present. | ||
2. Begin world creation | ||
1. In the game, click Singleplayer, then click Create New World. | ||
3. Manage datapacks | ||
1. Click the Data Packs button. If you've done everything correctly, the | ||
datapacks will appear on the list on the left! | ||
2. Alternatively, if you already have a world, open and pause the game. | ||
3. Manage data packs | ||
1. Click the Data Packs button. On later versions, you may need to check | ||
the 'More' tab. | ||
2. The data packs should be available on the selector! | ||
#### Server | ||
1. Insert your desired data pack(s) into `datapacks` in the server data folder. | ||
1. This is probably the root, where eula.txt and server.properties are | ||
located. | ||
2. Start the server | ||
1. The data packs will be copied to the active world. |
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
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
40 changes: 0 additions & 40 deletions
40
src/main/java/ml/unbreakinggold/datapackinstaller/mixin/CreateWorldScreenMixin.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/main/java/ml/unbreakinggold/datapackinstaller/mixin/MinecraftServerAccessor.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,12 @@ | ||
package ml.unbreakinggold.datapackinstaller.mixin; | ||
|
||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.world.level.storage.LevelStorage; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
@Mixin(MinecraftServer.class) | ||
public interface MinecraftServerAccessor { | ||
@Accessor | ||
LevelStorage.Session getSession(); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/ml/unbreakinggold/datapackinstaller/mixin/client/CreateWorldScreenMixin.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,43 @@ | ||
package ml.unbreakinggold.datapackinstaller.mixin.client; | ||
|
||
import ml.unbreakinggold.datapackinstaller.client.DatapackInstallerClient; | ||
import net.minecraft.client.gui.screen.world.CreateWorldScreen; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Overwrite; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
|
||
import java.nio.file.Path; | ||
|
||
@Mixin(CreateWorldScreen.class) | ||
public abstract class CreateWorldScreenMixin { | ||
@Unique | ||
private static final Logger LOGGER = LogManager.getLogger(CreateWorldScreenMixin.class); | ||
@Shadow | ||
@Nullable | ||
private Path dataPackTempDir; | ||
|
||
/** | ||
* @author Jomar Milan - July 31st, 2024 - Minecraft 1.20.5 | ||
* @reason Use persistent mod directory instead of vanilla temporary directory | ||
*/ | ||
@Overwrite | ||
@Nullable | ||
private Path getDataPackTempDir() { | ||
this.dataPackTempDir = DatapackInstallerClient.MAIN_PATH; | ||
|
||
return this.dataPackTempDir; | ||
} | ||
|
||
/** | ||
* @author Jomar Milan - July 31st, 2024 - Minecraft 1.20.5 | ||
* @reason Because dataPackTempDir is now persistent and not temporary, the directory should no longer be cleared. | ||
*/ | ||
@Overwrite | ||
private void clearDataPackTempDir() { | ||
LOGGER.info("Suppressing attempt to clear data pack directory"); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/ml/unbreakinggold/datapackinstaller/mixin/client/GameMenuScreenMixin.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,63 @@ | ||
package ml.unbreakinggold.datapackinstaller.mixin.client; | ||
|
||
import ml.unbreakinggold.datapackinstaller.client.DatapackInstallerClient; | ||
import ml.unbreakinggold.datapackinstaller.mixin.MinecraftServerAccessor; | ||
import net.minecraft.client.gui.screen.GameMenuScreen; | ||
import net.minecraft.client.gui.screen.Screen; | ||
import net.minecraft.client.gui.screen.pack.PackScreen; | ||
import net.minecraft.client.gui.widget.ButtonWidget; | ||
import net.minecraft.client.gui.widget.GridWidget; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.WorldSavePath; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
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.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.function.Supplier; | ||
|
||
@Mixin(GameMenuScreen.class) | ||
public abstract class GameMenuScreenMixin extends Screen { | ||
@Unique private final Logger LOGGER = LogManager.getLogger(GameMenuScreenMixin.class); | ||
@Unique private static final Text DATA_PACK_TEXT = Text.translatable("dataPack.title"); | ||
|
||
@Shadow private ButtonWidget createButton(Text text, Supplier<Screen> screenSupplier) { return null; }; | ||
|
||
private GameMenuScreenMixin(Text title) { | ||
super(title); | ||
} | ||
|
||
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/MinecraftClient;isInSingleplayer()Z"), method = "initWidgets", locals = LocalCapture.CAPTURE_FAILHARD) | ||
private void onInitWidgets(CallbackInfo ci, GridWidget gridWidget, GridWidget.Adder adder) { | ||
if (!this.client.isIntegratedServerRunning() || this.client.getServer().isRemote()) return; | ||
|
||
adder.add(this.createButton(DATA_PACK_TEXT, () -> { | ||
Path worldDataPackPath = ((MinecraftServerAccessor)this.client.getServer()).getSession().getDirectory(WorldSavePath.DATAPACKS); | ||
File modDatapackDir = DatapackInstallerClient.MAIN_PATH.toFile(); | ||
File worldDataPackDir = worldDataPackPath.toFile(); | ||
try { | ||
FileUtils.copyDirectory(modDatapackDir, worldDataPackDir); | ||
} catch(IOException exception) { | ||
LOGGER.error("Unable to install new data packs to world. Data packs not yet seen by this world may be missing.", exception); | ||
} | ||
|
||
return new PackScreen(this.client.getServer().getDataPackManager(), (dataPackManager) -> { | ||
Collection<String> enabledProfiles = dataPackManager.getEnabledIds(); | ||
|
||
this.client.getServer().reloadResources(enabledProfiles); | ||
|
||
this.client.setScreen(this); | ||
}, DatapackInstallerClient.MAIN_PATH, DATA_PACK_TEXT); | ||
})); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/ml/unbreakinggold/datapackinstaller/mixin/server/ServerMainMixin.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,40 @@ | ||
package ml.unbreakinggold.datapackinstaller.mixin.server; | ||
|
||
import com.llamalad7.mixinextras.sugar.Local; | ||
import ml.unbreakinggold.datapackinstaller.server.DatapackInstallerServer; | ||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.Environment; | ||
import net.minecraft.server.Main; | ||
import net.minecraft.util.WorldSavePath; | ||
import net.minecraft.world.level.storage.LevelStorage; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
@Mixin(Main.class) | ||
@Environment(EnvType.SERVER) | ||
public class ServerMainMixin { | ||
@Unique | ||
private static final Logger LOGGER = LogManager.getLogger(ServerMainMixin.class); | ||
|
||
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/VanillaDataPackProvider;createManager(Lnet/minecraft/world/level/storage/LevelStorage$Session;)Lnet/minecraft/resource/ResourcePackManager;"), method = "main") | ||
private static void onMain(CallbackInfo ci, @Local LevelStorage.Session session) { | ||
Path worldDataPackPath = session.getDirectory(WorldSavePath.DATAPACKS); | ||
File worldDataPackDir = worldDataPackPath.toFile(); | ||
File modDataPackDir = DatapackInstallerServer.MAIN_PATH.toFile(); | ||
try { | ||
FileUtils.copyDirectory(modDataPackDir, worldDataPackDir); | ||
} catch(IOException exception) { | ||
LOGGER.error("Failed to copy data packs to the world.", exception); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/ml/unbreakinggold/datapackinstaller/server/DatapackInstallerServer.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,21 @@ | ||
package ml.unbreakinggold.datapackinstaller.server; | ||
|
||
import net.fabricmc.api.DedicatedServerModInitializer; | ||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.Environment; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
|
||
@Environment(EnvType.SERVER) | ||
public class DatapackInstallerServer implements DedicatedServerModInitializer { | ||
public static final Path MAIN_PATH = FabricLoader.getInstance().getGameDir().resolve("datapacks"); | ||
|
||
@Override | ||
public void onInitializeServer() { | ||
File mainFile = MAIN_PATH.toFile(); | ||
|
||
if (!mainFile.exists()) mainFile.mkdirs(); | ||
} | ||
} |
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
Oops, something went wrong.