An advanced chat solution for JC3MP
Much of this readme is from the default chat readme
It's pretty simple, except there are a few changes and a little bit of configuration you'll need to do. These are some basic steps to help you get started.
- Delete the old chat package.
- Install the chat2 package.
- Inside
config.js
, modify it to include the channels that you want, indefault_channels
. If a channel with the nameLocal
is specified, then it will be used as a local chat channel where you can only talk to players who are nearby. This local chat distance is specified bylocal_distance
inconfig.js
. - Admins won't have their stars anymore (though it is possible to readd them, but that will not be covered here), and if you implemented custom tags, you'll need to do a little bit of work to get tags to appear again. See the Tags section on more information about how you can implement nametags.
- If you aren't using the freeroam package, set
using_freeroam
inconfig.js
to false. - Continue on to the next section to finish up implementing chat2.
When you want to use the chat as a dependency for your package, make sure to add it to your jcmp_dependencies
array. You can then use the following code snippet to get the chat object:
const chat = jcmp.events.Call('get_chat')[0];
The returned object provides these functions:
- target: target Player
- message: message string
- color (optional): rgb color for the message
- args (optional): additional arguments for the message
- Available arguments include:
- timeout (integer): number of seconds until the message should be removed
- channel (string): what channel the message should be sent on. If no channel is given, it will be sent to the player's currently open channel
- style (string): a valid
font-style
for the message, such asitalic
- use_name (boolean): set this to true if the message contains a player's name
Example:
jcmp.events.Add('PlayerDeath', player =>
chat.send(player, "You died.");
});
Example 2:
jcmp.events.Add('PlayerDeath', player =>
chat.send(player, "You died.", new RGB(255,0,0), {channel: 'CoolChannel', timeout: 10});
});
- message: message string
- color (optional): rgb color for the message
- args (optional): additional arguments for the message
- Available arguments include:
- timeout (integer): number of seconds until the message should be removed
- channel (string): what channel the message should be sent on. If no channel is given, it will be sent to the player's currently open channel
- style (string): a valid
font-style
for the message, such asitalic
- use_name (boolean): set this to true if the message contains a player's name
Example:
jcmp.events.Add('PlayerDeath', () =>
chat.broadcast("Someone died!");
});
Your messages can include any html, except if use_name
is true in args
.
If you want your message to include multiple colors, you can do that by using color tags in this format: [#RRGGBB]
Example:
jcmp.events.Add('PlayerDeath', player =>
chat.send(player, "[#ff0000]You [#ffffff]died.");
// You will be red, the rest will be white
});
This chat supports neat looking tags. To enable this feature, you must give a player a tag. This can be done like so:
player.tags =
[
{
name: 'Admin',
color: '#FF0000'
}
]
If you want to replace the default admin star with an Admin tag, here are some steps on doing that.
- Navigate to
freeroam/events/player.js
. - Inside the
PlayerCreated
event, near line 34, add this line:
if (freeroam.utils.isAdmin(player))
{
player.tags =
[
{
name: 'Admin',
color: '#FF0000'
}
]
}
- The entire
PlayerCreated
event should now look like this:
jcmp.events.Add("PlayerCreated", player => {
player.escapedNametagName = player.name.replace(/</g, '<').replace(/>/g, '>').substring(0, 40);
console.log(`${player.escapedNametagName} has joined.`);
freeroam.chat.broadcast(`** ${player.escapedNametagName} has joined.`, freeroam.config.colours.connection);
const colour = freeroam.colours.randomColor();
player.freeroam = {
colour: colour,
colour_rgb: freeroam.utils.hexToRGB(colour),
kills: 0,
deaths: 0,
custom_time_set: false,
vehicle_nitro_toggled: false,
vehicle_turbojump_toggled: false
};
player.group = null;
player.groupInvite = null;
const data = {
id: player.networkId,
name: player.escapedNametagName,
colour: player.freeroam.colour,
isAdmin: freeroam.utils.isAdmin(player),
};
if (freeroam.utils.isAdmin(player))
{
player.tags =
[
{
name: 'Admin',
color: '#FF0000'
}
]
}
jcmp.events.CallRemote("freeroam_player_created", null, JSON.stringify(data));
});
If you want to add multiple tags, you can do so like this:
player.tags =
[
{
name: 'Admin',
color: '#FF0000'
},
{
name: 'Cool',
color: '#00BFFF'
},
{
name: 'Special',
color: '#F04FE2'
}
]
- Now we need to stop the default admin start from appearing. Navigate to
freeroam/events/chat.js
. See this?
jcmp.events.Add('chat_message', (player, message) => {
if (typeof player.freeroam === 'undefined')
return `${player.escapedNametagName}: ${message}`;
console.log(`${player.escapedNametagName}: ${message}`);
return `${freeroam.utils.isAdmin(player) ? '<div class="admin-logo"></div>' : ''}[${player.freeroam.colour}] ${player.escapedNametagName}[#FFFFFF]: ${message}`;
});
Comment all of it. It should now look like this:
/*jcmp.events.Add('chat_message', (player, message) => {
if (typeof player.freeroam === 'undefined')
return `${player.escapedNametagName}: ${message}`;
console.log(`${player.escapedNametagName}: ${message}`);
return `${freeroam.utils.isAdmin(player) ? '<div class="admin-logo"></div>' : ''}[${player.freeroam.colour}] ${player.escapedNametagName}[#FFFFFF]: ${message}`;
});*/
If you did everything correctly, any admins registered in the freeroam package should have Admin tags in chat.
You can mention players, similar to Discord.
Mentioning a specific player
- Start typing your message:
hello
- Start typing the @ part of the message:
hello @ri
- Once you have part of the player's name, press TAB to autocomplete:
hello @"rico"
- If it isn't the right player, you can hit TAB again to cycle through players.
- You must use TAB and autocomplete to mention a player, otherwise it will fail.
Mentioning Everyone
In order to mention everyone, the player must have the Admin
tag. If the message is sent by the server (through send
or broadcast
), it can mention everyone as well.
Player:
- Type your message, and make sure to add
@everyone
somewhere:hello @everyone!
- Send your message
Server:
- Make sure the message includes
@everyone
:chat.broadcast("@everyone Hello!");
- You can also mention specific players by name with
@"PlayerName"
All events from the default chat are included, such as chat_command
and chat_message
. However, these have an additional argument, which is the channel that the message was sent on.
jcmp.events.Add('LocalPlayerChat', (msg) => {})
Client event that fires when the client sends a message. Return false to cancel sending.
/clear
Clears the current channel of all chat messages.
F5
Hides/shows the chat window.
Up or Down Key while input is open
Go through previously sent messages to easily the same message again.
Switch channels
Press Y to switch channels if you don't want to click.
Chat logs are recorded in chat2/logs
daily. They include player names and steam ids.
This chat was originally written to be used in tandem with custom gamemodes, but it still works fine with freeroam. However, the command hints package will not work with this package.