Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/client/command/CommandsExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import client.command.commands.gm0.EnableAuthCommand;
import client.command.commands.gm0.EquipLvCommand;
import client.command.commands.gm0.GachaCommand;
import client.command.commands.gm0.GlobalCommand;
import client.command.commands.gm0.GmCommand;
import client.command.commands.gm0.HelpCommand;
import client.command.commands.gm0.JoinEventCommand;
Expand Down Expand Up @@ -346,6 +347,7 @@ private void registerLv0Commands() {
addCommand("credits", StaffCommand.class);
addCommand("uptime", UptimeCommand.class);
addCommand("gacha", GachaCommand.class);
addCommand("global", GlobalCommand.class);
addCommand("dispose", DisposeCommand.class);
addCommand("changel", ChangeLanguageCommand.class);
addCommand("equiplv", EquipLvCommand.class);
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/client/command/commands/gm0/GlobalCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
This file is part of the HeavenMS MapleStory Server, commands OdinMS-based
Copyleft (L) 2016 - 2019 RonanLana

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package client.command.commands.gm0;

import client.Character;
import client.Client;
import client.command.Command;
import server.ChatLogger;
import tools.PacketCreator;

public class GlobalCommand extends Command {
{
setDescription("Send a message to every player in the world.");
}

@Override
public void execute(Client client, String[] params) {
Character player = client.getPlayer();
String message = player.getLastCommandMessage();
if (message == null || message.isBlank()) {
player.yellowMessage("Usage: @global <message>");
return;
}

String formattedMessage = "[Global] " + player.getName() + ": " + message;
client.getWorldServer().broadcastPacket(PacketCreator.serverNotice(6, formattedMessage));
ChatLogger.log(client, "Global", message);
}
}
68 changes: 68 additions & 0 deletions src/test/java/client/command/commands/gm0/GlobalCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package client.command.commands.gm0;

import client.Character;
import client.Client;
import io.netty.buffer.Unpooled;
import net.packet.ByteBufInPacket;
import net.packet.Packet;
import net.server.world.World;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import server.ChatLogger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class GlobalCommandTest {
@Mock
private Client client;
@Mock
private Character player;
@Mock
private World world;

@Test
void broadcastsOriginalMessageToTheWholeWorld() {
when(client.getPlayer()).thenReturn(player);
when(client.getWorldServer()).thenReturn(world);
when(player.getName()).thenReturn("RustPocChar");
when(player.getLastCommandMessage()).thenReturn("Heading to Henesys");

try (MockedStatic<ChatLogger> loggerStatic = mockStatic(ChatLogger.class)) {
new GlobalCommand().execute(client, new String[]{"heading", "to", "henesys"});

var packetCaptor = org.mockito.ArgumentCaptor.forClass(Packet.class);
verify(world).broadcastPacket(packetCaptor.capture());
var packet = new ByteBufInPacket(
Unpooled.wrappedBuffer(packetCaptor.getValue().getBytes()));
assertEquals(0x0044, packet.readShort());
assertEquals(6, packet.readUnsignedByte());
assertEquals("[Global] RustPocChar: Heading to Henesys", packet.readString());
assertEquals(0, packet.readInt());
assertEquals(0, packet.available());
loggerStatic.verify(() -> ChatLogger.log(client, "Global", "Heading to Henesys"));
}
}

@Test
void rejectsBlankMessagesWithoutBroadcastingOrLogging() {
when(client.getPlayer()).thenReturn(player);
when(player.getLastCommandMessage()).thenReturn(" ");

try (MockedStatic<ChatLogger> loggerStatic = mockStatic(ChatLogger.class)) {
new GlobalCommand().execute(client, new String[]{});

verify(player).yellowMessage("Usage: @global <message>");
verify(world, never()).broadcastPacket(any(Packet.class));
loggerStatic.verifyNoInteractions();
}
}
}