Skip to content

Commit 5caed9f

Browse files
committed
Command fixes, tweak room preview screen text, unbind cmd, fix new rooms
Fixes a few small details about commands such as room binding still referencing an ID (no longer valid) as well as adding an unbind command. Also tweaks some room preview text to be nicer to read and properly positioned. Fixes machine entry for brand new machines.
1 parent c148995 commit 5caed9f

File tree

12 files changed

+142
-21
lines changed

12 files changed

+142
-21
lines changed

src/api/java/dev/compactmods/machines/api/core/CMCommands.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class CMCommands {
1919
*/
2020
public static final ResourceLocation ROOM_REG_COUNT = new ResourceLocation(MOD_ID, "room_reg_count");
2121
public static final ResourceLocation NOT_A_MACHINE_BLOCK = new ResourceLocation(MOD_ID, "not_a_machine_block");
22+
23+
/**
24+
* Shows a machine is not bound. Takes in a single param, the machine position in world.
25+
*/
2226
public static final ResourceLocation MACHINE_NOT_BOUND = new ResourceLocation(MOD_ID, "machine_not_bound");
2327

2428
public static final ResourceLocation WRONG_DIMENSION = new ResourceLocation(MOD_ID, "not_in_compact_dimension");
@@ -27,4 +31,5 @@ public class CMCommands {
2731

2832
public static final ResourceLocation CANNOT_GIVE_MACHINE = new ResourceLocation(MOD_ID, "cannot_give_machine_item");
2933
public static final ResourceLocation MACHINE_GIVEN = new ResourceLocation(MOD_ID, "machine_given_successfully");
34+
public static final ResourceLocation NO_REBIND_TUNNEL_PRESENT = new ResourceLocation(MOD_ID, "cannot_rebind_tunnel_present");
3035
}

src/datagen/java/dev/compactmods/machines/datagen/lang/EnglishLangGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected void addTranslations() {
3434

3535
addCommand(CMCommands.NOT_IN_COMPACT_DIMENSION, "Cannot use that command outside of a machine room.");
3636
addCommand(CMCommands.FAILED_CMD_FILE_ERROR, "Failed to execute command; there was a file error. Check logs.");
37-
addCommand(CMCommands.MACHINE_NOT_BOUND, "Machine at %s does not have an associated ID.");
37+
addCommand(CMCommands.MACHINE_NOT_BOUND, "Machine at %s is not bound to a room.");
3838
addCommand(CMCommands.ROOM_REG_COUNT, "Number of registered rooms: %s");
3939
addCommand(CMCommands.MACHINE_REG_DIM, "[%s]: %s");
4040
addCommand(CMCommands.MACHINE_REG_TOTAL, "Total: %s");

src/main/java/dev/compactmods/machines/command/CMCommandRoot.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
1414
root.then(CMEjectSubcommand.make());
1515
root.then(CMSummarySubcommand.make());
1616
root.then(CMRebindSubcommand.make());
17+
root.then(CMUnbindSubcommand.make());
1718
root.then(CMReaddDimensionSubcommand.make());
1819
root.then(CMRoomsSubcommand.make());
1920
root.then(CMDataSubcommand.make());

src/main/java/dev/compactmods/machines/command/argument/RoomPositionArgument.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
import com.mojang.brigadier.context.CommandContext;
66
import com.mojang.brigadier.exceptions.CommandSyntaxException;
77
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
8+
import com.mojang.brigadier.suggestion.Suggestions;
9+
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
810
import net.minecraft.commands.CommandSourceStack;
911
import net.minecraft.commands.arguments.coordinates.*;
1012
import net.minecraft.core.BlockPos;
1113
import net.minecraft.network.chat.TranslatableComponent;
1214
import net.minecraft.server.level.ColumnPos;
1315
import net.minecraft.world.level.ChunkPos;
1416

17+
import java.util.concurrent.CompletableFuture;
18+
1519
import static net.minecraft.commands.arguments.coordinates.WorldCoordinate.ERROR_EXPECTED_INT;
1620

1721
public class RoomPositionArgument implements ArgumentType<RoomCoordinates> {
@@ -43,4 +47,9 @@ public RoomCoordinates parse(StringReader reader) throws CommandSyntaxException
4347
}
4448
}
4549
}
50+
51+
@Override
52+
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
53+
return Suggestions.empty();
54+
}
4655
}

src/main/java/dev/compactmods/machines/command/subcommand/CMEjectSubcommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public class CMEjectSubcommand {
1717
return Commands.literal("eject")
1818
.executes(CMEjectSubcommand::execExecutingPlayer)
1919
.then(Commands.argument("player", EntityArgument.player())
20-
.executes(CMEjectSubcommand::execSpecificPlayer));
20+
.requires(cs -> cs.hasPermission(Commands.LEVEL_GAMEMASTERS))
21+
.executes(CMEjectSubcommand::execSpecificPlayer));
2122
}
2223

2324
private static int execSpecificPlayer(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {

src/main/java/dev/compactmods/machines/command/subcommand/CMRebindSubcommand.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,16 @@ private static int doRebind(CommandContext<CommandSourceStack> ctx) throws Comma
4848
throw new CommandRuntimeException(TranslationUtil.command(CMCommands.NOT_A_MACHINE_BLOCK));
4949
}
5050

51-
final var dimMachines = DimensionMachineGraph.forDimension(level);
5251
machine.getConnectedRoom().ifPresentOrElse(currentRoom -> {
5352
final var currentRoomTunnels = TunnelConnectionGraph.forRoom(compactDim, currentRoom);
5453
final var firstTunnel = currentRoomTunnels.getConnections(machine.getLevelPosition()).findFirst();
55-
if(firstTunnel.isPresent()) {
54+
firstTunnel.ifPresent(ft -> {
55+
throw new CommandRuntimeException(TranslationUtil.command(CMCommands.NO_REBIND_TUNNEL_PRESENT, ft));
56+
});
5657

57-
}
58-
}, () -> {
59-
dimMachines.connectMachineToRoom(rebindingMachine, roomPos);
60-
machine.syncConnectedRoom();
61-
});
58+
// No tunnels - clear to rebind
59+
machine.setConnectedRoom(roomPos);
60+
}, () -> machine.setConnectedRoom(roomPos));
6261

6362
return 0;
6463
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package dev.compactmods.machines.command.subcommand;
2+
3+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
4+
import com.mojang.brigadier.context.CommandContext;
5+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6+
import dev.compactmods.machines.CompactMachines;
7+
import dev.compactmods.machines.api.core.CMCommands;
8+
import dev.compactmods.machines.command.argument.RoomPositionArgument;
9+
import dev.compactmods.machines.config.ServerConfig;
10+
import dev.compactmods.machines.core.Registration;
11+
import dev.compactmods.machines.i18n.TranslationUtil;
12+
import dev.compactmods.machines.machine.CompactMachineBlockEntity;
13+
import dev.compactmods.machines.tunnel.graph.TunnelConnectionGraph;
14+
import net.minecraft.commands.CommandRuntimeException;
15+
import net.minecraft.commands.CommandSourceStack;
16+
import net.minecraft.commands.Commands;
17+
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
18+
19+
public class CMUnbindSubcommand {
20+
21+
public static LiteralArgumentBuilder<CommandSourceStack> make() {
22+
final var subRoot = Commands.literal("unbind")
23+
.requires(cs -> cs.hasPermission(ServerConfig.rebindLevel()));
24+
25+
subRoot.then(Commands.argument("pos", BlockPosArgument.blockPos())
26+
.executes(CMUnbindSubcommand::doUnbind));
27+
28+
return subRoot;
29+
}
30+
31+
private static int doUnbind(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
32+
final var server = ctx.getSource().getServer();
33+
final var level = ctx.getSource().getLevel();
34+
final var compactDim = server.getLevel(Registration.COMPACT_DIMENSION);
35+
if (compactDim == null) {
36+
throw new CommandRuntimeException(TranslationUtil.command(CMCommands.LEVEL_NOT_FOUND));
37+
}
38+
39+
final var rebindingMachine = BlockPosArgument.getLoadedBlockPos(ctx, "pos");
40+
41+
if(!(level.getBlockEntity(rebindingMachine) instanceof CompactMachineBlockEntity machine)) {
42+
CompactMachines.LOGGER.error("Refusing to rebind block at {}; block has invalid machine data.", rebindingMachine);
43+
throw new CommandRuntimeException(TranslationUtil.command(CMCommands.NOT_A_MACHINE_BLOCK));
44+
}
45+
46+
machine.getConnectedRoom().ifPresentOrElse(currentRoom -> {
47+
final var currentRoomTunnels = TunnelConnectionGraph.forRoom(compactDim, currentRoom);
48+
final var firstTunnel = currentRoomTunnels.getConnections(machine.getLevelPosition()).findFirst();
49+
firstTunnel.ifPresent(ft -> {
50+
throw new CommandRuntimeException(TranslationUtil.command(CMCommands.NO_REBIND_TUNNEL_PRESENT, ft));
51+
});
52+
53+
machine.disconnect();
54+
}, () -> {
55+
throw new CommandRuntimeException(TranslationUtil.command(CMCommands.MACHINE_NOT_BOUND, rebindingMachine.toShortString()));
56+
});
57+
58+
return 0;
59+
}
60+
}

src/main/java/dev/compactmods/machines/machine/CompactMachineBlock.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import dev.compactmods.machines.CompactMachines;
44
import dev.compactmods.machines.config.ServerConfig;
55
import dev.compactmods.machines.core.*;
6+
import dev.compactmods.machines.location.PreciseDimensionalPosition;
67
import dev.compactmods.machines.machine.graph.DimensionMachineGraph;
78
import dev.compactmods.machines.location.LevelBlockPosition;
89
import dev.compactmods.machines.room.RoomSize;
910
import dev.compactmods.machines.room.Rooms;
11+
import dev.compactmods.machines.room.exceptions.NonexistentRoomException;
12+
import dev.compactmods.machines.room.history.PlayerRoomHistoryItem;
1013
import dev.compactmods.machines.room.menu.MachineRoomMenu;
1114
import dev.compactmods.machines.tunnel.graph.TunnelConnectionGraph;
1215
import dev.compactmods.machines.util.PlayerUtil;
@@ -195,20 +198,32 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
195198
} catch (MissingDimensionException e) {
196199
e.printStackTrace();
197200
}
198-
}, () -> {
199-
try {
200-
final var newRoomPos = Rooms.createNew(server, size, player.getUUID());
201-
202-
} catch (MissingDimensionException e) {
203-
CompactMachines.LOGGER.error("Error occurred while generating new room and machine info for first player entry.", e);
204-
}
205-
});
201+
}, () -> createAndEnterRoom(player, server, tile));
206202
}
207203
}
208204

209205
return InteractionResult.SUCCESS;
210206
}
211207

208+
private void createAndEnterRoom(Player player, MinecraftServer server, CompactMachineBlockEntity tile) {
209+
try {
210+
final var newRoomPos = Rooms.createNew(server, size, player.getUUID());
211+
tile.setConnectedRoom(newRoomPos);
212+
213+
PlayerUtil.teleportPlayerIntoRoom(server, player, newRoomPos, true);
214+
215+
// Mark the player as inside the machine, set external spawn, and yeet
216+
player.getCapability(Capabilities.ROOM_HISTORY).ifPresent(hist -> {
217+
var entry = PreciseDimensionalPosition.fromPlayer(player);
218+
hist.addHistory(new PlayerRoomHistoryItem(entry, tile.getLevelPosition()));
219+
});
220+
} catch (MissingDimensionException e) {
221+
CompactMachines.LOGGER.error("Error occurred while generating new room and machine info for first player entry.", e);
222+
} catch (NonexistentRoomException e) {
223+
CompactMachines.LOGGER.error("Error occurred while generating new room and machine info for first player entry.", e);
224+
}
225+
}
226+
212227
public RoomSize getSize() {
213228
return this.size;
214229
}

src/main/java/dev/compactmods/machines/machine/CompactMachineBlockEntity.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,27 @@ public void syncConnectedRoom() {
235235
this.getConnectedRoom()
236236
.flatMap(room -> graph.getRoomNode(this.roomChunk))
237237
.ifPresent(roomNode -> this.roomNode = new WeakReference<>(roomNode));
238+
239+
this.setChanged();
240+
}
241+
}
242+
243+
public void setConnectedRoom(ChunkPos room) {
244+
if(level instanceof ServerLevel sl) {
245+
final var dimMachines = DimensionMachineGraph.forDimension(sl);
246+
dimMachines.connectMachineToRoom(worldPosition, room);
247+
syncConnectedRoom();
248+
}
249+
}
250+
251+
public void disconnect() {
252+
if(level instanceof ServerLevel sl) {
253+
final var dimMachines = DimensionMachineGraph.forDimension(sl);
254+
dimMachines.disconnect(worldPosition);
255+
256+
this.roomChunk = null;
257+
this.graphNode.clear();
258+
setChanged();
238259
}
239260
}
240261
}

src/main/java/dev/compactmods/machines/machine/graph/DimensionMachineGraph.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ public void disconnect(BlockPos machine) {
186186
graph.successors(node).stream()
187187
.filter(cn -> cn instanceof CompactMachineRoomNode)
188188
.forEach(room -> graph.removeEdge(node, room));
189+
190+
setDirty();
189191
}
190192

191193
public Optional<CompactMachineNode> getMachineNode(BlockPos worldPosition) {

0 commit comments

Comments
 (0)