|
4 | 4 | import com.mojang.brigadier.context.CommandContext; |
5 | 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException; |
6 | 6 | import dev.compactmods.crafting.CompactCrafting; |
7 | | -import dev.compactmods.crafting.projector.FieldProjectorBlock; |
8 | | -import dev.compactmods.crafting.projector.FieldProjectorEntity; |
| 7 | +import dev.compactmods.crafting.api.field.IMiniaturizationField; |
| 8 | +import dev.compactmods.crafting.core.CCCapabilities; |
9 | 9 | import net.minecraft.commands.CommandSourceStack; |
10 | 10 | import net.minecraft.commands.Commands; |
11 | 11 | import net.minecraft.commands.arguments.coordinates.BlockPosArgument; |
12 | 12 | import net.minecraft.core.BlockPos; |
13 | 13 | import net.minecraft.network.chat.TextComponent; |
14 | 14 | import net.minecraft.network.chat.TranslatableComponent; |
15 | 15 | import net.minecraft.server.level.ServerLevel; |
| 16 | +import net.minecraft.world.level.block.entity.BlockEntity; |
16 | 17 |
|
17 | | -public class ProjectorInfoCommand { |
| 18 | +public class FieldInfoCommand { |
18 | 19 | public static ArgumentBuilder<CommandSourceStack, ?> create() { |
19 | 20 |
|
20 | | - var fieldInfo = Commands.literal("fieldinfo"); |
21 | | - fieldInfo.then(Commands.argument("block", BlockPosArgument.blockPos()) |
22 | | - .executes(ProjectorInfoCommand::exe)); |
| 21 | + var field = Commands.literal("field"); |
23 | 22 |
|
24 | | - return fieldInfo; |
| 23 | + field.then(Commands.literal("ref") |
| 24 | + .then(Commands.argument("block", BlockPosArgument.blockPos()) |
| 25 | + .executes(FieldInfoCommand::byReference))); |
| 26 | + |
| 27 | + field.then(Commands.literal("center") |
| 28 | + .then(Commands.argument("center", BlockPosArgument.blockPos()) |
| 29 | + .executes(FieldInfoCommand::byCenter))); |
| 30 | + |
| 31 | + return field; |
| 32 | + } |
| 33 | + |
| 34 | + private static int byCenter(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException { |
| 35 | + final BlockPos pos = BlockPosArgument.getLoadedBlockPos(ctx, "center"); |
| 36 | + final CommandSourceStack src = ctx.getSource(); |
| 37 | + final ServerLevel level = src.getLevel(); |
| 38 | + |
| 39 | + level.getCapability(CCCapabilities.FIELDS).ifPresent(fields -> { |
| 40 | + if (!fields.hasActiveField(pos)) { |
| 41 | + src.sendFailure(new TranslatableComponent("messages." + CompactCrafting.MOD_ID + ".no_field_found", pos)); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + fields.get(pos).ifPresent(field -> outputStdFieldInfo(src, field)); |
| 46 | + }); |
| 47 | + |
| 48 | + return 0; |
25 | 49 | } |
26 | 50 |
|
27 | | - private static int exe(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException { |
| 51 | + private static int byReference(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException { |
28 | 52 | final BlockPos pos = BlockPosArgument.getLoadedBlockPos(ctx, "block"); |
29 | 53 | final CommandSourceStack src = ctx.getSource(); |
30 | 54 | final ServerLevel level = src.getLevel(); |
31 | 55 |
|
32 | | - if (level.getBlockState(pos).getBlock() instanceof FieldProjectorBlock fpb) { |
33 | | - if (level.getBlockEntity(pos) instanceof FieldProjectorEntity proj) { |
34 | | - proj.getField().ifPresent(field -> { |
35 | | - src.sendSuccess(new TextComponent("Center: " + field.getCenter().toString()), false); |
36 | | - src.sendSuccess(new TextComponent("Size: " + field.getFieldSize().getName()), false); |
37 | | - field.getCurrentRecipe().ifPresent(rec -> { |
38 | | - src.sendSuccess(new TextComponent("Recipe: " + rec.getRecipeIdentifier()), false); |
39 | | - }); |
40 | | - }); |
| 56 | + if (level.getBlockState(pos).hasBlockEntity()) { |
| 57 | + final BlockEntity ent = level.getBlockEntity(pos); |
| 58 | + if (ent == null) { |
| 59 | + src.sendFailure(new TranslatableComponent("messages." + CompactCrafting.MOD_ID + ".no_field_cap", pos)); |
| 60 | + return 0; |
41 | 61 | } |
| 62 | + |
| 63 | + ent.getCapability(CCCapabilities.MINIATURIZATION_FIELD).ifPresent(field -> outputStdFieldInfo(src, field)); |
42 | 64 | } else { |
43 | | - src.sendFailure(new TranslatableComponent("messages." + CompactCrafting.MOD_ID + ".not_a_projector", pos)); |
| 65 | + src.sendFailure(new TranslatableComponent("messages." + CompactCrafting.MOD_ID + ".no_field_cap", pos)); |
44 | 66 | } |
45 | 67 |
|
46 | 68 | return 0; |
47 | 69 | } |
| 70 | + |
| 71 | + private static void outputStdFieldInfo(CommandSourceStack src, IMiniaturizationField field) { |
| 72 | + src.sendSuccess(new TextComponent("Center: " + field.getCenter().toString()), false); |
| 73 | + src.sendSuccess(new TextComponent("Size: " + field.getFieldSize().getName()), false); |
| 74 | + field.getCurrentRecipe().ifPresent(rec -> { |
| 75 | + src.sendSuccess(new TextComponent("Recipe: " + rec.getRecipeIdentifier()), false); |
| 76 | + }); |
| 77 | + } |
48 | 78 | } |
0 commit comments