Skip to content

Commit 46f8694

Browse files
committed
add command /ap-chatbox to trigger chat event
close IntelligenceModding/Advanced-Peripherals-Features#60
1 parent ea54b20 commit 46f8694

1 file changed

Lines changed: 46 additions & 11 deletions

File tree

src/main/java/de/srendi/advancedperipherals/common/commands/APCommands.java

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import dan200.computercraft.shared.computer.core.ServerComputer;
1212
import dan200.computercraft.shared.computer.core.ServerContext;
1313
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.ChunkyPeripheral;
14+
import de.srendi.advancedperipherals.common.events.Events;
1415
import de.srendi.advancedperipherals.common.util.ChunkManager;
1516
import de.srendi.advancedperipherals.common.util.inventory.ItemUtil;
1617
import net.minecraft.ChatFormatting;
@@ -21,11 +22,14 @@
2122
import net.minecraft.network.chat.ComponentUtils;
2223
import net.minecraft.network.chat.HoverEvent;
2324
import net.minecraft.server.level.ServerPlayer;
25+
import net.minecraft.world.entity.Entity;
26+
import net.minecraft.world.entity.player.Player;
2427
import net.neoforged.bus.api.SubscribeEvent;
2528
import net.neoforged.fml.common.EventBusSubscriber;
2629
import net.neoforged.neoforge.event.RegisterCommandsEvent;
2730

2831
import java.util.Comparator;
32+
import java.util.UUID;
2933

3034

3135
@EventBusSubscriber
@@ -36,27 +40,35 @@ public class APCommands {
3640
"/" + ROOT_LITERAL + " " + FORCELOAD_LITERAL + " help" + " - show this help message\n" +
3741
"/" + ROOT_LITERAL + " " + FORCELOAD_LITERAL + " dump" + " - show all chunky turtles\n";
3842
public static final String EXEC_LITERAL = "safe-exec";
43+
public static final String CHATBOX_LITERAL = "chatbox";
3944
public static final String ROOT_SAFE_EXEC_LITERAL = "ap-safe-exec";
45+
public static final String ROOT_CHATBOX_LITERAL = "ap-chatbox";
4046

4147
@SubscribeEvent
4248
public static void register(RegisterCommandsEvent event) {
4349
LiteralCommandNode<CommandSourceStack> safeExecNode = Commands.literal(EXEC_LITERAL)
4450
.then(Commands.argument("command", StringArgumentType.greedyString())
4551
.executes(APCommands::safeExecute))
4652
.build();
53+
LiteralCommandNode<CommandSourceStack> chatBoxNode = Commands.literal(CHATBOX_LITERAL)
54+
.then(Commands.argument("message", StringArgumentType.greedyString())
55+
.executes(APCommands::chatBox))
56+
.build();
4757
event.getDispatcher().register(Commands.literal(ROOT_LITERAL)
48-
.then(Commands.literal("getHashItem")
49-
.executes(context -> getHashItem(context.getSource()))).then(Commands.literal(FORCELOAD_LITERAL)
58+
.then(Commands.literal("getHashItem")
59+
.executes(context -> getHashItem(context.getSource())))
60+
.then(Commands.literal(FORCELOAD_LITERAL)
5061
.executes(context -> forceloadHelp(context.getSource()))
5162
.then(Commands.literal("help")
5263
.executes(context -> forceloadHelp(context.getSource())))
5364
.then(Commands.literal("dump")
5465
.requires(ModRegistry.Permissions.PERMISSION_DUMP)
55-
.executes(context -> forceloadDump(context.getSource())))
56-
)
66+
.executes(context -> forceloadDump(context.getSource()))))
5767
.then(safeExecNode)
68+
.then(chatBoxNode)
5869
);
5970
event.getDispatcher().register(Commands.literal(ROOT_SAFE_EXEC_LITERAL).redirect(safeExecNode));
71+
event.getDispatcher().register(Commands.literal(ROOT_CHATBOX_LITERAL).redirect(chatBoxNode));
6072
}
6173

6274
private static int getHashItem(CommandSourceStack source) throws CommandSyntaxException {
@@ -84,14 +96,20 @@ private static int forceloadHelp(CommandSourceStack source) throws CommandSyntax
8496
private static int forceloadDump(CommandSourceStack source) throws CommandSyntaxException {
8597
TableBuilder table = new TableBuilder("ChunkyTurtles", "Computer", "Position");
8698

87-
ServerComputer[] computers = ServerContext.get(source.getServer()).registry().getComputers().stream().filter((computer) -> {
88-
for (ComputerSide side : ComputerSide.values()) {
89-
if (computer.getPeripheral(side) instanceof ChunkyPeripheral) {
90-
return true;
99+
ServerComputer[] computers = ServerContext.get(source.getServer())
100+
.registry()
101+
.getComputers()
102+
.stream()
103+
.filter((computer) -> {
104+
for (ComputerSide side : ComputerSide.values()) {
105+
if (computer.getPeripheral(side) instanceof ChunkyPeripheral) {
106+
return true;
107+
}
91108
}
92-
}
93-
return false;
94-
}).sorted(Comparator.comparingInt(ServerComputer::getID)).toArray(ServerComputer[]::new);
109+
return false;
110+
})
111+
.sorted(Comparator.comparingInt(ServerComputer::getID))
112+
.toArray(ServerComputer[]::new);
95113

96114
for (ServerComputer computer : computers) {
97115
table.row(
@@ -118,6 +136,23 @@ private static int safeExecute(CommandContext<CommandSourceStack> context) throw
118136
}
119137
}
120138

139+
private static int chatBox(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
140+
CommandSourceStack source = context.getSource();
141+
UUID uuid = null;
142+
String username = "[say]";
143+
Entity sourceEntity = source.getEntity();
144+
if (sourceEntity != null) {
145+
uuid = sourceEntity.getUUID();
146+
username = sourceEntity instanceof Player player
147+
? player.getGameProfile().getName()
148+
: sourceEntity.getName().getString();
149+
}
150+
String message = StringArgumentType.getString(context, "message");
151+
Events.putChatMessage(
152+
new Events.ChatMessageRecord(uuid, username, message, true, source.getLevel().dimension(), source.getPosition())
153+
);
154+
return 0;
155+
}
121156

122157
private static Component makeComputerDumpCommand(ServerComputer computer) {
123158
return ChatHelpers.link(

0 commit comments

Comments
 (0)