|
| 1 | +package dev.compactmods.machines.command; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 5 | +import com.mojang.brigadier.context.CommandContext; |
| 6 | +import dev.compactmods.machines.api.CompactMachines; |
| 7 | +import dev.compactmods.machines.api.room.template.RoomTemplateHelper; |
| 8 | +import net.minecraft.commands.CommandSourceStack; |
| 9 | +import net.minecraft.commands.Commands; |
| 10 | +import net.minecraft.network.chat.Component; |
| 11 | +import net.minecraft.server.commands.ReloadCommand; |
| 12 | +import net.minecraft.server.packs.repository.Pack; |
| 13 | +import net.minecraft.server.packs.repository.PackRepository; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class EnableBasicTemplatesSubcommand { |
| 18 | + |
| 19 | + final static String PACK_ID = "mod/compactmachines:data/compactmachines/datapacks/basic_templates"; |
| 20 | + |
| 21 | + public static LiteralArgumentBuilder<CommandSourceStack> make() { |
| 22 | + return Commands.literal("enable_basic_templates") |
| 23 | + .requires(cs -> cs.hasPermission(Commands.LEVEL_GAMEMASTERS) && |
| 24 | + RoomTemplateHelper.getTemplates(cs.registryAccess()).findAny().isEmpty()) |
| 25 | + .executes(EnableBasicTemplatesSubcommand::exec); |
| 26 | + } |
| 27 | + |
| 28 | + private static int exec(CommandContext<CommandSourceStack> ctx) { |
| 29 | + PackRepository packrepository = ctx.getSource().getServer().getPackRepository(); |
| 30 | + |
| 31 | + List<Pack> list = Lists.newArrayList(packrepository.getSelectedPacks()); |
| 32 | + if(!packrepository.getSelectedIds().contains(PACK_ID)) { |
| 33 | + final var pack = packrepository.getPack(PACK_ID); |
| 34 | + if (pack != null) { |
| 35 | + pack.getDefaultPosition().insert(list, pack, Pack::selectionConfig, false); |
| 36 | + ReloadCommand.reloadPacks(list.stream().map(Pack::getId).toList(), ctx.getSource()); |
| 37 | + |
| 38 | + ctx.getSource().sendSuccess(() -> Component.literal("Enabled basic templates, restart game to sync changes."), false); |
| 39 | + } |
| 40 | + } else { |
| 41 | + ctx.getSource().sendFailure(Component.literal("Already enabled, restart game to sync changes.")); |
| 42 | + } |
| 43 | + |
| 44 | + return 0; |
| 45 | + } |
| 46 | +} |
0 commit comments