|
| 1 | +package net.blay09.mods.littlejoys.command; |
| 2 | + |
| 3 | +import com.mojang.brigadier.CommandDispatcher; |
| 4 | +import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; |
| 5 | +import net.blay09.mods.balm.api.command.BalmCommands; |
| 6 | +import net.blay09.mods.littlejoys.LittleJoys; |
| 7 | +import net.blay09.mods.littlejoys.handler.DigSpotHandler; |
| 8 | +import net.blay09.mods.littlejoys.handler.DropRushHandler; |
| 9 | +import net.blay09.mods.littlejoys.handler.FishingSpotHandler; |
| 10 | +import net.blay09.mods.littlejoys.handler.GoldRushHandler; |
| 11 | +import net.blay09.mods.littlejoys.recipe.DigSpotRecipe; |
| 12 | +import net.blay09.mods.littlejoys.recipe.DropRushRecipe; |
| 13 | +import net.blay09.mods.littlejoys.recipe.FishingSpotRecipe; |
| 14 | +import net.blay09.mods.littlejoys.recipe.GoldRushRecipe; |
| 15 | +import net.minecraft.commands.CommandSourceStack; |
| 16 | +import net.minecraft.commands.Commands; |
| 17 | +import net.minecraft.commands.arguments.ResourceKeyArgument; |
| 18 | +import net.minecraft.commands.arguments.coordinates.BlockPosArgument; |
| 19 | +import net.minecraft.core.registries.Registries; |
| 20 | +import net.minecraft.network.chat.Component; |
| 21 | +import net.minecraft.resources.ResourceKey; |
| 22 | +import net.minecraft.resources.ResourceLocation; |
| 23 | +import net.minecraft.world.item.crafting.RecipeHolder; |
| 24 | + |
| 25 | +public class LittleJoysCommand { |
| 26 | + |
| 27 | + private static final ResourceLocation PERMISSION_LITTLEJOYS_DIGSPOT = LittleJoys.id("command.littlejoys.digspot"); |
| 28 | + private static final ResourceLocation PERMISSION_LITTLEJOYS_FISHINGSPOT = LittleJoys.id("command.littlejoys.fishingspot"); |
| 29 | + private static final ResourceLocation PERMISSION_LITTLEJOYS_GOLDRUSH = LittleJoys.id("command.littlejoys.goldrush"); |
| 30 | + private static final ResourceLocation PERMISSION_LITTLEJOYS_DROPRUSH = LittleJoys.id("command.littlejoys.droprush"); |
| 31 | + |
| 32 | + private static final DynamicCommandExceptionType ERROR_UNKNOWN_RECIPE = new DynamicCommandExceptionType((arg) -> Component.translatable("recipe.notFound", arg)); |
| 33 | + |
| 34 | + @SuppressWarnings("unchecked") |
| 35 | + public static void register(CommandDispatcher<CommandSourceStack> dispatcher) { |
| 36 | + BalmCommands.registerPermission(PERMISSION_LITTLEJOYS_DIGSPOT, 2); |
| 37 | + BalmCommands.registerPermission(PERMISSION_LITTLEJOYS_FISHINGSPOT, 2); |
| 38 | + BalmCommands.registerPermission(PERMISSION_LITTLEJOYS_GOLDRUSH, 2); |
| 39 | + BalmCommands.registerPermission(PERMISSION_LITTLEJOYS_DROPRUSH, 2); |
| 40 | + |
| 41 | + dispatcher.register(Commands.literal("littlejoys") |
| 42 | + .then(Commands.literal("digspot") |
| 43 | + .requires(BalmCommands.requirePermission(PERMISSION_LITTLEJOYS_DIGSPOT)) |
| 44 | + .then(Commands.argument("position", BlockPosArgument.blockPos()) |
| 45 | + .executes(context -> { |
| 46 | + final var level = context.getSource().getLevel(); |
| 47 | + final var player = context.getSource().getPlayerOrException(); |
| 48 | + final var pos = BlockPosArgument.getBlockPos(context, "position"); |
| 49 | + return DigSpotHandler.createDigSpot(level, pos, player) ? 1 : 0; |
| 50 | + }) |
| 51 | + .then(Commands.argument("recipe", ResourceKeyArgument.key(Registries.RECIPE)) |
| 52 | + .executes(context -> { |
| 53 | + final var level = context.getSource().getLevel(); |
| 54 | + final var pos = BlockPosArgument.getBlockPos(context, "position"); |
| 55 | + final var recipeHolder = ResourceKeyArgument.getRecipe(context, "recipe"); |
| 56 | + if (recipeHolder.value() instanceof DigSpotRecipe) { |
| 57 | + DigSpotHandler.createDigSpot(level, pos, (RecipeHolder<DigSpotRecipe>) recipeHolder); |
| 58 | + return 1; |
| 59 | + } else { |
| 60 | + throw ERROR_UNKNOWN_RECIPE.create(context.getArgument("recipe", ResourceKey.class)); |
| 61 | + } |
| 62 | + })))) |
| 63 | + .then(Commands.literal("fishingspot") |
| 64 | + .requires(BalmCommands.requirePermission(PERMISSION_LITTLEJOYS_FISHINGSPOT)) |
| 65 | + .then(Commands.argument("position", BlockPosArgument.blockPos()) |
| 66 | + .executes(context -> { |
| 67 | + final var level = context.getSource().getLevel(); |
| 68 | + final var player = context.getSource().getPlayerOrException(); |
| 69 | + final var pos = BlockPosArgument.getBlockPos(context, "position"); |
| 70 | + return FishingSpotHandler.createFishingSpot(level, pos, player) ? 1 : 0; |
| 71 | + }) |
| 72 | + .then(Commands.argument("recipe", ResourceKeyArgument.key(Registries.RECIPE)) |
| 73 | + .executes(context -> { |
| 74 | + final var level = context.getSource().getLevel(); |
| 75 | + final var pos = BlockPosArgument.getBlockPos(context, "position"); |
| 76 | + final var recipeHolder = ResourceKeyArgument.getRecipe(context, "recipe"); |
| 77 | + if (recipeHolder.value() instanceof FishingSpotRecipe) { |
| 78 | + FishingSpotHandler.createFishingSpot(level, pos, (RecipeHolder<FishingSpotRecipe>) recipeHolder); |
| 79 | + return 1; |
| 80 | + } else { |
| 81 | + throw ERROR_UNKNOWN_RECIPE.create(context.getArgument("recipe", ResourceKey.class)); |
| 82 | + } |
| 83 | + })))) |
| 84 | + .then(Commands.literal("goldrush") |
| 85 | + .requires(BalmCommands.requirePermission(PERMISSION_LITTLEJOYS_GOLDRUSH)) |
| 86 | + .then(Commands.argument("position", BlockPosArgument.blockPos()) |
| 87 | + .executes(context -> { |
| 88 | + final var level = context.getSource().getLevel(); |
| 89 | + final var player = context.getSource().getPlayerOrException(); |
| 90 | + final var pos = BlockPosArgument.getBlockPos(context, "position"); |
| 91 | + GoldRushHandler.startGoldRush(level, pos, level.getBlockState(pos), player); |
| 92 | + return 1; |
| 93 | + }) |
| 94 | + .then(Commands.argument("recipe", ResourceKeyArgument.key(Registries.RECIPE)) |
| 95 | + .executes(context -> { |
| 96 | + final var level = context.getSource().getLevel(); |
| 97 | + final var player = context.getSource().getPlayerOrException(); |
| 98 | + final var pos = BlockPosArgument.getBlockPos(context, "position"); |
| 99 | + final var recipeHolder = ResourceKeyArgument.getRecipe(context, "recipe"); |
| 100 | + if (recipeHolder.value() instanceof GoldRushRecipe) { |
| 101 | + GoldRushHandler.startGoldRush(level, pos, level.getBlockState(pos), player, (RecipeHolder<GoldRushRecipe>) recipeHolder); |
| 102 | + return 1; |
| 103 | + } else { |
| 104 | + throw ERROR_UNKNOWN_RECIPE.create(context.getArgument("recipe", ResourceKey.class)); |
| 105 | + } |
| 106 | + })))) |
| 107 | + .then(Commands.literal("droprush") |
| 108 | + .requires(BalmCommands.requirePermission(PERMISSION_LITTLEJOYS_DROPRUSH)) |
| 109 | + .then(Commands.argument("position", BlockPosArgument.blockPos()) |
| 110 | + .executes(context -> { |
| 111 | + final var level = context.getSource().getLevel(); |
| 112 | + final var player = context.getSource().getPlayerOrException(); |
| 113 | + final var pos = BlockPosArgument.getBlockPos(context, "position"); |
| 114 | + DropRushHandler.startDropRush(level, pos, level.getBlockState(pos), player); |
| 115 | + return 0; |
| 116 | + }).then(Commands.argument("recipe", ResourceKeyArgument.key(Registries.RECIPE)) |
| 117 | + .executes(context -> { |
| 118 | + final var level = context.getSource().getLevel(); |
| 119 | + final var player = context.getSource().getPlayerOrException(); |
| 120 | + final var pos = BlockPosArgument.getBlockPos(context, "position"); |
| 121 | + final var recipeHolder = ResourceKeyArgument.getRecipe(context, "recipe"); |
| 122 | + if (recipeHolder.value() instanceof DropRushRecipe) { |
| 123 | + DropRushHandler.startDropRush(level, pos, player, (RecipeHolder<DropRushRecipe>) recipeHolder); |
| 124 | + return 1; |
| 125 | + } else { |
| 126 | + throw ERROR_UNKNOWN_RECIPE.create(context.getArgument("recipe", ResourceKey.class)); |
| 127 | + } |
| 128 | + })))) |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments