Skip to content

Commit

Permalink
feat: add command to reload resource+ datapacks
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Jan 9, 2024
1 parent 54fe427 commit 82a18ce
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ public static class Command {
public static final String SUCCESS_SAVE_PROGRESS = SUCCESS_PREFIX + "save_progress";
public static final String SUCCESS_LOAD_PROGRESS = SUCCESS_PREFIX + "load_progress";

public static final String RELOAD_REQUESTED = PREFIX + "reload_requested";
public static final String RELOAD_SUCCESS = SUCCESS_PREFIX + "reload_requested";


public static final String DEFAULT_FAILURE_MESSAGE = PREFIX + "failure";
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-FileCopyrightText: 2022 klikli-dev
*
* SPDX-License-Identifier: MIT
*/

package com.klikli_dev.modonomicon.command;

import com.klikli_dev.modonomicon.api.ModonomiconConstants;
import com.klikli_dev.modonomicon.networking.ReloadResourcesOnClientMessage;
import com.klikli_dev.modonomicon.platform.Services;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;

public class ReloadBooksCommand implements com.mojang.brigadier.Command<CommandSourceStack> {

private static final ReloadBooksCommand CMD = new ReloadBooksCommand();


public static ArgumentBuilder<CommandSourceStack, ?> register(CommandDispatcher<CommandSourceStack> dispatcher) {
return Commands.literal("reload")
.requires(cs -> cs.hasPermission(2))
.executes(CMD);
}


@Override
public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Services.NETWORK.sendTo(context.getSource().getPlayer(), new ReloadResourcesOnClientMessage());
context.getSource().sendSuccess(() -> Component.translatable(ModonomiconConstants.I18n.Command.RELOAD_REQUESTED), true);
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ private void addMisc() {
this.add(Command.SUCCESS_RESET_BOOK, "Successfully reset book: %s");
this.add(Command.SUCCESS_SAVE_PROGRESS, "Saved progress for book: %s. The unlock code has been copied to your clipboard.");
this.add(Command.SUCCESS_LOAD_PROGRESS, "Successfully loaded progress for book: %s.");
this.add(Command.RELOAD_REQUESTED, "Requested reload of resource- and datapacks.");
this.add(Command.RELOAD_SUCCESS, "Successfully reloaded resource- and datapacks.");
this.add(Command.ERROR_LOAD_PROGRESS, "Invalid unlock code!");
this.add(Command.ERROR_LOAD_PROGRESS_CLIENT, "Failed to decode unlock code. Make sure to have a valid unlock code in your clipboard! Current Clipboard content: \"%s\"");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* SPDX-FileCopyrightText: 2022 klikli-dev
*
* SPDX-License-Identifier: MIT
*/

package com.klikli_dev.modonomicon.networking;

import com.google.common.collect.Lists;
import com.klikli_dev.modonomicon.Modonomicon;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.repository.PackRepository;
import net.minecraft.world.level.storage.WorldData;

import java.util.Collection;

import static com.klikli_dev.modonomicon.api.ModonomiconConstants.I18n.Command.RELOAD_SUCCESS;

public class ReloadResourcesDoneMessage implements Message {

public static final ResourceLocation ID = new ResourceLocation(Modonomicon.MOD_ID, "reload_resources_done");

public ReloadResourcesDoneMessage() {
}

public ReloadResourcesDoneMessage(FriendlyByteBuf buf) {
this.decode(buf);
}

@Override
public void encode(FriendlyByteBuf buf) {
}

@Override
public void decode(FriendlyByteBuf buf) {
}

@Override
public ResourceLocation getId() {
return ID;
}

@Override
public void onServerReceived(MinecraftServer minecraftServer, ServerPlayer player) {

if (!player.hasPermissions(2)) //same leve las the reload command
return;

//below is copied from ReloadCommand, modified not to need a command source and to only post messages after reload is complete
PackRepository packrepository = minecraftServer.getPackRepository();
WorldData worlddata = minecraftServer.getWorldData();
Collection<String> selectedIds = packrepository.getSelectedIds();
Collection<String> packs = discoverNewPacks(packrepository, worlddata, selectedIds);
reloadPacks(packs, player, minecraftServer);
}

private static Collection<String> discoverNewPacks(PackRepository pPackRepository, WorldData pWorldData, Collection<String> pSelectedIds) {
pPackRepository.reload();
Collection<String> collection = Lists.newArrayList(pSelectedIds);
Collection<String> collection1 = pWorldData.getDataConfiguration().dataPacks().getDisabled();

for(String s : pPackRepository.getAvailableIds()) {
if (!collection1.contains(s) && !collection.contains(s)) {
collection.add(s);
}
}

return collection;
}

public static void reloadPacks(Collection<String> pSelectedIds, ServerPlayer player, MinecraftServer server) {
server.reloadResources(pSelectedIds).exceptionally((error) -> {
Modonomicon.LOG.warn("Failed to execute reload", error);
player.sendSystemMessage(Component.translatable("commands.reload.failure").withStyle(ChatFormatting.RED));
return null;
}).thenRun(() -> {
player.sendSystemMessage(Component.translatable(RELOAD_SUCCESS).withStyle(ChatFormatting.GREEN));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-FileCopyrightText: 2022 klikli-dev
*
* SPDX-License-Identifier: MIT
*/

package com.klikli_dev.modonomicon.networking;

import com.klikli_dev.modonomicon.Modonomicon;
import com.klikli_dev.modonomicon.platform.Services;
import com.klikli_dev.modonomicon.platform.services.PlatformHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;

public class ReloadResourcesOnClientMessage implements Message {

public static final ResourceLocation ID = new ResourceLocation(Modonomicon.MOD_ID, "reload_resources_on_client");

public ReloadResourcesOnClientMessage() {
}

public ReloadResourcesOnClientMessage(FriendlyByteBuf buf) {
this.decode(buf);
}

@Override
public void encode(FriendlyByteBuf buf) {
}

@Override
public void decode(FriendlyByteBuf buf) {
}

@Override
public ResourceLocation getId() {
return ID;
}

@Override
public void onClientReceived(Minecraft minecraft, Player player) {
minecraft.reloadResourcePacks().thenRun(() -> {
Services.NETWORK.sendToServer(new ReloadResourcesDoneMessage());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.klikli_dev.modonomicon.Modonomicon;
import com.klikli_dev.modonomicon.command.LoadUnlocksCommand;
import com.klikli_dev.modonomicon.command.ReloadBooksCommand;
import com.klikli_dev.modonomicon.command.ResetBookUnlocksCommand;
import com.klikli_dev.modonomicon.command.SaveUnlocksCommand;
import com.mojang.brigadier.CommandDispatcher;
Expand All @@ -22,9 +23,10 @@ public static void registerCommands(CommandDispatcher<CommandSourceStack> dispat
Commands.literal(Modonomicon.MOD_ID)
.then(ResetBookUnlocksCommand.register(dispatcher))
.then(SaveUnlocksCommand.register(dispatcher))
.then(ReloadBooksCommand.register(dispatcher))
);

dispatcher.register(Commands.literal("modonomicon").redirect(modonomiconCommand));
dispatcher.register(Commands.literal(Modonomicon.MOD_ID).redirect(modonomiconCommand));
}

public static void registerClientCommands(CommandDispatcher<CommandSourceStack> dispatcher) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 1.20.1 2024-01-08T09:19:58.146027 Modonomicon/Books: modonomicon
// 1.20.1 2024-01-09T11:06:30.7029862 Modonomicon/Books: modonomicon
149f724b78931be9fa643e1c9288933cc73a75c4 data\modonomicon\modonomicon\books\demo\entries\formatting\basic.json
b9ad32b76e2efc9f44c8b76d56670f9dc63f2a94 data\modonomicon\modonomicon\books\demo\commands\test_command2.json
da6b6a832a9392085f982ba05f34e606d64fa13b data\modonomicon\modonomicon\books\demo\entries\formatting\always_locked.json
7cb4c581142b83471177183ec136e5db0f8a1540 data\modonomicon\modonomicon\books\demo\entries\features\multiblock.json
da6b6a832a9392085f982ba05f34e606d64fa13b data\modonomicon\modonomicon\books\demo\entries\formatting\always_locked.json
b82c0b99b596152481f8281592d77c822fd7590e data\modonomicon\modonomicon\books\demo\categories\conditional.json
09d31cc11836a50dad4c92daa1695475a6c4fc1f data\modonomicon\modonomicon\books\demo\entries\conditional\always_locked.json
a0b01243aa86122c9894a419e5f6998e2d1a2ca3 data\modonomicon\modonomicon\books\demo\entries\features\recipe.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.20.1 2024-01-08T09:19:58.1535555 Modonomicon/Languages: en_us
c02a2188ed3f6ed7de335e505e38a4d6cf90d9e1 assets\modonomicon\lang\en_us.json
// 1.20.1 2024-01-09T11:06:30.7079865 Modonomicon/Languages: en_us
619af0dd17a726e34764055a425bf703af4821e6 assets\modonomicon\lang\en_us.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.20.1 2024-01-08T09:19:58.1525565 Modonomicon/Model Definitions
// 1.20.1 2024-01-09T11:06:30.7069867 Modonomicon/Model Definitions
b02e0bc9f75e0fde6807935577e74b75348413fb assets\modonomicon\models\item\modonomicon_blue.json
83adea7fd65acd9581797454e1a6dc6122d8a826 assets\modonomicon\models\item\modonomicon_green.json
4b35646623b74487675fa8ec1bb375535100c336 assets\modonomicon\models\item\modonomicon_purple.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.20.1 2024-01-08T09:19:58.1525565 Modonomicon/Multiblocks: modonomicon
// 1.20.1 2024-01-09T11:06:30.7069867 Modonomicon/Multiblocks: modonomicon
6af533f5fa171fb2cf2d27b166e16ec54b2c5c06 data\modonomicon\modonomicon\multiblocks\test_non_square.json
89b499dd4f3850c8099ad26ae3c286e95ac8cf99 data\modonomicon\modonomicon\multiblocks\demo_fluid.json
9dc306d79a39ee2085be7e1384d8e39640d79e49 data\modonomicon\modonomicon\multiblocks\demo_dense.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
"modonomicon.command.error.load_progress_client": "Failed to decode unlock code. Make sure to have a valid unlock code in your clipboard! Current Clipboard content: \"%s\"",
"modonomicon.command.error.unknown_book": "Unknown book: %s",
"modonomicon.command.failure": "Modonomicon tried to run a command for you (e.g. because you read an entry for the first time, or clicked a command button or command link). However, it seems you already reached the maximum use limit for this command.",
"modonomicon.command.reload_requested": "Requested reload of resource- and datapacks.",
"modonomicon.command.success.load_progress": "Successfully loaded progress for book: %s.",
"modonomicon.command.success.reload_requested": "Successfully reloaded resource- and datapacks.",
"modonomicon.command.success.reset_book": "Successfully reset book: %s",
"modonomicon.command.success.save_progress": "Saved progress for book: %s. The unlock code has been copied to your clipboard.",
"modonomicon.command.test_command.success": "You got an apple, because reading is cool!",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public static void registerMessages() {
ClientPlayNetworking.registerGlobalReceiver(SyncBookUnlockStatesMessage.ID, new ClientMessageHandler<>(SyncBookUnlockStatesMessage::new));
ClientPlayNetworking.registerGlobalReceiver(SyncBookVisualStatesMessage.ID, new ClientMessageHandler<>(SyncBookVisualStatesMessage::new));
ClientPlayNetworking.registerGlobalReceiver(SyncMultiblockDataMessage.ID, new ClientMessageHandler<>(SyncMultiblockDataMessage::new));
ClientPlayNetworking.registerGlobalReceiver(ReloadResourcesOnClientMessage.ID, new ClientMessageHandler<>(ReloadResourcesOnClientMessage::new));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public static void registerMessages() {
ServerPlayNetworking.registerGlobalReceiver(SaveCategoryStateMessage.ID, new ServerMessageHandler<>(SaveCategoryStateMessage::new));
ServerPlayNetworking.registerGlobalReceiver(SaveEntryStateMessage.ID, new ServerMessageHandler<>(SaveEntryStateMessage::new));
ServerPlayNetworking.registerGlobalReceiver(SendUnlockCodeToServerMessage.ID, new ServerMessageHandler<>(SendUnlockCodeToServerMessage::new));
ServerPlayNetworking.registerGlobalReceiver(ReloadResourcesDoneMessage.ID, new ServerMessageHandler<>(ReloadResourcesDoneMessage::new));
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.20.1 2024-01-08T09:19:17.5634628 Languages: en_us
c02a2188ed3f6ed7de335e505e38a4d6cf90d9e1 assets/modonomicon/lang/en_us.json
// 1.20.1 2024-01-09T10:59:40.9721648 Languages: en_us
619af0dd17a726e34764055a425bf703af4821e6 assets/modonomicon/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
"modonomicon.command.error.load_progress_client": "Failed to decode unlock code. Make sure to have a valid unlock code in your clipboard! Current Clipboard content: \"%s\"",
"modonomicon.command.error.unknown_book": "Unknown book: %s",
"modonomicon.command.failure": "Modonomicon tried to run a command for you (e.g. because you read an entry for the first time, or clicked a command button or command link). However, it seems you already reached the maximum use limit for this command.",
"modonomicon.command.reload_requested": "Requested reload of resource- and datapacks.",
"modonomicon.command.success.load_progress": "Successfully loaded progress for book: %s.",
"modonomicon.command.success.reload_requested": "Successfully reloaded resource- and datapacks.",
"modonomicon.command.success.reset_book": "Successfully reset book: %s",
"modonomicon.command.success.save_progress": "Saved progress for book: %s. The unlock code has been copied to your clipboard.",
"modonomicon.command.test_command.success": "You got an apple, because reading is cool!",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ public static void registerMessages() {
SyncMultiblockDataMessage::new,
MessageHandler::handle);

INSTANCE.registerMessage(nextID(),
ReloadResourcesOnClientMessage.class,
ReloadResourcesOnClientMessage::encode,
ReloadResourcesOnClientMessage::new,
MessageHandler::handle);

INSTANCE.registerMessage(nextID(),
ReloadResourcesDoneMessage.class,
ReloadResourcesDoneMessage::encode,
ReloadResourcesDoneMessage::new,
MessageHandler::handle);
}

public static <T> void sendToSplit(ServerPlayer player, T message) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ group=com.klikli_dev
mod_id=modonomicon
mod_name=Modonomicon
mod_license=MIT AND CC-BY-4.0
mod_version=1.48.1
mod_version=1.49.0
mod_authors=Kli Kli
mod_description=Data-driven minecraft in-game documentation with progress visualization.

Expand Down

0 comments on commit 82a18ce

Please sign in to comment.