Skip to content

Commit 2340cec

Browse files
authored
Merge pull request #667 from Niki4tap/feature/broadcast_if_possible
Add `broadcast` option to the chat box
2 parents 6b38dfa + 9fc7ea0 commit 2340cec

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/ChatBoxPeripheral.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ private boolean checkBrackets(Optional<String> brackets) {
211211
public final MethodResult sendFormattedMessage(@NotNull IArguments arguments) throws LuaException {
212212
return withChatOperation(ignored -> {
213213
String message = arguments.getString(0);
214-
int maxRange = APConfig.PERIPHERALS_CONFIG.chatBoxMaxRange.get();
215-
int range = arguments.optInt(4, -1);
216214
ResourceKey<Level> dimension = getLevel().dimension();
217215
MutableComponent component = Component.Serializer.fromJson(message);
218216
if (component == null) {
@@ -236,6 +234,19 @@ public final MethodResult sendFormattedMessage(@NotNull IArguments arguments) th
236234
return MethodResult.of(null, "illegal prefix");
237235
}
238236
preparedMessage.append(component);
237+
238+
int maxRange = APConfig.PERIPHERALS_CONFIG.chatBoxMaxRange.get();
239+
int range = arguments.optInt(4, -1);
240+
if (
241+
APConfig.PERIPHERALS_CONFIG.chatBoxBroadcast.get()
242+
&& APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get()
243+
&& maxRange == -1
244+
&& range == -1
245+
) {
246+
ServerLifecycleHooks.getCurrentServer().getPlayerList().broadcastSystemMessage(preparedMessage, false);
247+
return MethodResult.of(true);
248+
}
249+
239250
for (ServerPlayer player : ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayers()) {
240251
if (!APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get() && player.getLevel().dimension() != dimension) {
241252
continue;
@@ -252,8 +263,6 @@ public final MethodResult sendFormattedMessage(@NotNull IArguments arguments) th
252263
public final MethodResult sendMessage(@NotNull IArguments arguments) throws LuaException {
253264
return withChatOperation(ignored -> {
254265
String message = arguments.getString(0);
255-
int maxRange = APConfig.PERIPHERALS_CONFIG.chatBoxMaxRange.get();
256-
int range = arguments.optInt(4, -1);
257266
ResourceKey<Level> dimension = getLevel().dimension();
258267
if (checkBrackets(arguments.optString(2))) {
259268
return MethodResult.of(null, "incorrect bracket string (e.g. [], {}, <>, ...)");
@@ -268,6 +277,19 @@ public final MethodResult sendMessage(@NotNull IArguments arguments) throws LuaE
268277
return MethodResult.of(null, "illegal prefix");
269278
}
270279
preparedMessage.append(message);
280+
281+
int maxRange = APConfig.PERIPHERALS_CONFIG.chatBoxMaxRange.get();
282+
int range = arguments.optInt(4, -1);
283+
if (
284+
APConfig.PERIPHERALS_CONFIG.chatBoxBroadcast.get()
285+
&& APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get()
286+
&& maxRange == -1
287+
&& range == -1
288+
) {
289+
ServerLifecycleHooks.getCurrentServer().getPlayerList().broadcastSystemMessage(preparedMessage, false);
290+
return MethodResult.of(true);
291+
}
292+
271293
for (ServerPlayer player : ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayers()) {
272294
if (!APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get() && player.getLevel().dimension() != dimension) {
273295
continue;

src/main/java/de/srendi/advancedperipherals/common/configuration/PeripheralsConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class PeripheralsConfig implements IAPConfig {
4444
public final ForgeConfigSpec.ConfigValue<String> defaultChatBoxPrefix;
4545
public final ForgeConfigSpec.IntValue chatBoxMaxRange;
4646
public final ForgeConfigSpec.BooleanValue chatBoxMultiDimensional;
47+
public final ForgeConfigSpec.BooleanValue chatBoxBroadcast;
4748
public final ForgeConfigSpec.BooleanValue chatBoxPreventRunCommand;
4849
public final ForgeConfigSpec.BooleanValue chatBoxWrapCommand;
4950
public final ForgeConfigSpec.ConfigValue<List<? extends String>> chatBoxBannedCommands;
@@ -147,6 +148,7 @@ public PeripheralsConfig() {
147148
defaultChatBoxPrefix = builder.comment("Defines default chatbox prefix").define("defaultChatBoxPrefix", "AP");
148149
chatBoxMaxRange = builder.comment("Defines the maximal range of the chat box in blocks. -1 for infinite. If the range is not -1, players in other dimensions won't able to receive messages").defineInRange("chatBoxMaxRange", -1, -1, 30000000);
149150
chatBoxMultiDimensional = builder.comment("If true, the chat box is able to send messages to other dimensions than its own").define("chatBoxMultiDimensional", true);
151+
chatBoxBroadcast = builder.comment("If true, chat box will 'broadcast' the messages instead of sending one individually to each player. This option does not affect anything directly, and relies on other mods to utilise this behavior (for example, mods that bridge minecraft chat to other platforms can intercept broadcasted messages and relay them, unlike individually sent ones). This option will only go in effect if `chatBoxMaxRange` is set to `-1` and `chatBoxMultiDimensional` is also set to `true`.").define("chatBoxBroadcast", true);
150152
chatBoxPreventRunCommand = builder.comment("If true, the chat box cannot use 'run_command' action").define("chatBoxPreventRunCommand", false);
151153
chatBoxWrapCommand = builder.comment("If true, the chat box will wrap and execute 'run_command' or 'suggest_command' action with zero permission, in order to prevent operators accidently run dangerous commands.").define("chatBoxWrapCommand", true);
152154
chatBoxBannedCommands = builder.comment("These commands below will not be able to send by 'run_command' or 'suggest_command' action. It will match as prefix if starts with '/', other wise use regex pattern").defineList("chatBoxBannedCommands", chatBoxDefaultBannedCommands, (o) -> o instanceof String value && value.length() > 0);

0 commit comments

Comments
 (0)