Skip to content

Commit e59087a

Browse files
committed
Add field informational commands, <ref> and <center>
1 parent 6338fae commit e59087a

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

src/main/java/dev/compactmods/crafting/command/CraftingCommandRoot.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public static void onCommandsRegister(final RegisterCommandsEvent event) {
2828

2929
private static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
3030
final LiteralArgumentBuilder<CommandSourceStack> root = LiteralArgumentBuilder.literal(CompactCrafting.MOD_ID);
31-
// root.then(Commands.literal("test")
32-
// .requires(cs -> cs.hasPermission(2))
33-
// .executes(CraftingCommandRoot::test));
31+
32+
root.then(FieldInfoCommand.create());
33+
3434
dispatcher.register(root);
3535
}
3636

src/main/java/dev/compactmods/crafting/command/FieldInfoCommand.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,75 @@
44
import com.mojang.brigadier.context.CommandContext;
55
import com.mojang.brigadier.exceptions.CommandSyntaxException;
66
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;
99
import net.minecraft.commands.CommandSourceStack;
1010
import net.minecraft.commands.Commands;
1111
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
1212
import net.minecraft.core.BlockPos;
1313
import net.minecraft.network.chat.TextComponent;
1414
import net.minecraft.network.chat.TranslatableComponent;
1515
import net.minecraft.server.level.ServerLevel;
16+
import net.minecraft.world.level.block.entity.BlockEntity;
1617

17-
public class ProjectorInfoCommand {
18+
public class FieldInfoCommand {
1819
public static ArgumentBuilder<CommandSourceStack, ?> create() {
1920

20-
var fieldInfo = Commands.literal("fieldinfo");
21-
fieldInfo.then(Commands.argument("block", BlockPosArgument.blockPos())
22-
.executes(ProjectorInfoCommand::exe));
21+
var field = Commands.literal("field");
2322

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;
2549
}
2650

27-
private static int exe(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
51+
private static int byReference(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
2852
final BlockPos pos = BlockPosArgument.getLoadedBlockPos(ctx, "block");
2953
final CommandSourceStack src = ctx.getSource();
3054
final ServerLevel level = src.getLevel();
3155

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;
4161
}
62+
63+
ent.getCapability(CCCapabilities.MINIATURIZATION_FIELD).ifPresent(field -> outputStdFieldInfo(src, field));
4264
} 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));
4466
}
4567

4668
return 0;
4769
}
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+
}
4878
}

src/main/resources/assets/compactcrafting/lang/en_us.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
"tooltip.compactcrafting.proxy_bound": "Bound to: %s",
2020
"tooltip.compactcrafting.proxy_hint": "Use proxies for field automation with redstone.",
2121
"tooltip.compactcrafting.proxy_bind_hint": "Shift-use on a projector to bind to a field.",
22-
"tooltip.compactcrafting.proxy_unbind_hint": "Shift-use in the air to unbind from field."
22+
"tooltip.compactcrafting.proxy_unbind_hint": "Shift-use in the air to unbind from field.",
23+
24+
"messages.compactcrafting.no_field_cap": "The block at %s does not have a field reference.",
25+
"messages.compactcrafting.no_field_found": "The miniaturization field info could not be found."
2326
}

0 commit comments

Comments
 (0)