-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command to reload resource+ datapacks
- Loading branch information
1 parent
54fe427
commit 82a18ce
Showing
17 changed files
with
206 additions
and
10 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
38 changes: 38 additions & 0 deletions
38
common/src/main/java/com/klikli_dev/modonomicon/command/ReloadBooksCommand.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,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; | ||
} | ||
} |
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
86 changes: 86 additions & 0 deletions
86
common/src/main/java/com/klikli_dev/modonomicon/networking/ReloadResourcesDoneMessage.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,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)); | ||
}); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...n/src/main/java/com/klikli_dev/modonomicon/networking/ReloadResourcesOnClientMessage.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,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()); | ||
}); | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
fabric/src/generated/resources/.cache/13ce76d12c64f3a9c0874320845bdcbe00b11956
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
4 changes: 2 additions & 2 deletions
4
fabric/src/generated/resources/.cache/3eecf17e0b3542fa44b02bae7aa93622bb4a80e9
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,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 |
2 changes: 1 addition & 1 deletion
2
fabric/src/generated/resources/.cache/e6530a5ab7a2c0d5d93ba510671319215bfabee3
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
2 changes: 1 addition & 1 deletion
2
fabric/src/generated/resources/.cache/fbe7fdca8a3d7d4724f001a760fe02aeb76403e5
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
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
4 changes: 2 additions & 2 deletions
4
forge/src/generated/resources/.cache/c622617f6fabf890a00b9275cd5f643584a8a2c8
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,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 |
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