Skip to content

Commit e9237a0

Browse files
committed
implemented config-option vanish-fake-join-leave, analogous to EssentialsDiscord
1 parent 5baf239 commit e9237a0

4 files changed

Lines changed: 58 additions & 16 deletions

File tree

Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.papermc.paper.event.player.PlayerServerFullCheckEvent;
2222
import net.ess3.api.IEssentials;
2323
import net.ess3.api.events.AfkStatusChangeEvent;
24+
import net.ess3.api.events.VanishStatusChangeEvent;
2425
import net.ess3.provider.CommandSendListenerProvider;
2526
import net.ess3.provider.FormattedCommandAliasProvider;
2627
import net.ess3.provider.InventoryViewProvider;
@@ -32,6 +33,7 @@
3233
import net.essentialsx.api.v2.events.AsyncUserDataLoadEvent;
3334
import org.bukkit.BanEntry;
3435
import org.bukkit.BanList;
36+
import org.bukkit.Bukkit;
3537
import org.bukkit.GameMode;
3638
import org.bukkit.Location;
3739
import org.bukkit.Material;
@@ -297,14 +299,7 @@ public void onPlayerQuit(final PlayerQuitEvent event) {
297299
if (hideJoinQuitMessages() || ess.getSettings().allowSilentJoinQuit() && user.isAuthorized("essentials.silentquit")) {
298300
event.setQuitMessage(null);
299301
} else if (ess.getSettings().isCustomQuitMessage() && event.getQuitMessage() != null) {
300-
final Player player = event.getPlayer();
301-
final String msg = ess.getSettings().getCustomQuitMessage()
302-
.replace("{PLAYER}", player.getDisplayName())
303-
.replace("{USERNAME}", player.getName())
304-
.replace("{ONLINE}", NumberFormat.getInstance().format(ess.getOnlinePlayers().size() - 1)) // Subtract 1 as the leaving player is still online during this time
305-
.replace("{UPTIME}", DateUtil.formatDateDiff(ManagementFactory.getRuntimeMXBean().getStartTime()))
306-
.replace("{PREFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getPrefix(player)))
307-
.replace("{SUFFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getSuffix(player)));
302+
final String msg = buildQuitMessage(user);
308303

309304
event.setQuitMessage(msg.isEmpty() ? null : msg);
310305
}
@@ -341,6 +336,50 @@ public void onPlayerQuit(final PlayerQuitEvent event) {
341336
user.dispose();
342337
}
343338

339+
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
340+
public void onVanishStatusChange(final VanishStatusChangeEvent event) {
341+
if (!ess.getSettings().isVanishFakeJoinLeave()) {
342+
return;
343+
}
344+
345+
if (event.getValue()) {
346+
final String quitMessage = buildQuitMessage((User) event.getAffected());
347+
348+
if (!quitMessage.isEmpty()) {
349+
Bukkit.broadcastMessage(quitMessage);
350+
}
351+
352+
return;
353+
}
354+
355+
final String joinMessage = buildJoinMessage((User) event.getAffected(), false, null);
356+
357+
if (!joinMessage.isEmpty()) {
358+
Bukkit.broadcastMessage(joinMessage);
359+
}
360+
}
361+
362+
private String buildJoinMessage(final User user, boolean newUsername, String lastAccountName) {
363+
return (newUsername && ess.getSettings().isCustomNewUsernameMessage() ? ess.getSettings().getCustomNewUsernameMessage() : ess.getSettings().getCustomJoinMessage())
364+
.replace("{PLAYER}", user.getDisplayName()).replace("{USERNAME}", user.getName())
365+
.replace("{UNIQUE}", NumberFormat.getInstance().format(ess.getUsers().getUserCount()))
366+
.replace("{ONLINE}", NumberFormat.getInstance().format(ess.getOnlinePlayers().size()))
367+
.replace("{UPTIME}", DateUtil.formatDateDiff(ManagementFactory.getRuntimeMXBean().getStartTime()))
368+
.replace("{PREFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getPrefix(user.getBase())))
369+
.replace("{SUFFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getSuffix(user.getBase())))
370+
.replace("{OLDUSERNAME}", lastAccountName == null ? "" : lastAccountName);
371+
}
372+
373+
private String buildQuitMessage(final User user) {
374+
return ess.getSettings().getCustomQuitMessage()
375+
.replace("{PLAYER}", user.getDisplayName())
376+
.replace("{USERNAME}", user.getName())
377+
.replace("{ONLINE}", NumberFormat.getInstance().format(ess.getOnlinePlayers().size() - 1)) // Subtract 1 as the leaving player is still online during this time
378+
.replace("{UPTIME}", DateUtil.formatDateDiff(ManagementFactory.getRuntimeMXBean().getStartTime()))
379+
.replace("{PREFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getPrefix(user.getBase())))
380+
.replace("{SUFFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getSuffix(user.getBase())));
381+
}
382+
344383
@SuppressWarnings("UnstableApiUsage")
345384
private final class JoinListener1_21 implements Listener {
346385
private final Map<UUID, String> newUserLocales = new ConcurrentHashMap<>();
@@ -458,14 +497,7 @@ private void joinFlow(final User user, final long currentTime, final String mess
458497
} else if (message == null || hideJoinQuitMessages()) {
459498
effectiveMessage = null;
460499
} else if (ess.getSettings().isCustomJoinMessage()) {
461-
final String msg = (newUsername && ess.getSettings().isCustomNewUsernameMessage() ? ess.getSettings().getCustomNewUsernameMessage() : ess.getSettings().getCustomJoinMessage())
462-
.replace("{PLAYER}", user.getDisplayName()).replace("{USERNAME}", user.getName())
463-
.replace("{UNIQUE}", NumberFormat.getInstance().format(ess.getUsers().getUserCount()))
464-
.replace("{ONLINE}", NumberFormat.getInstance().format(ess.getOnlinePlayers().size()))
465-
.replace("{UPTIME}", DateUtil.formatDateDiff(ManagementFactory.getRuntimeMXBean().getStartTime()))
466-
.replace("{PREFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getPrefix(user.getBase())))
467-
.replace("{SUFFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getSuffix(user.getBase())))
468-
.replace("{OLDUSERNAME}", lastAccountName == null ? "" : lastAccountName);
500+
final String msg = buildJoinMessage(user, newUsername, lastAccountName);
469501
effectiveMessage = msg.isEmpty() ? null : msg;
470502
} else if (ess.getSettings().allowSilentJoinQuit()) {
471503
effectiveMessage = message;

Essentials/src/main/java/com/earth2me/essentials/ISettings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ public interface ISettings extends IConf {
229229

230230
boolean sleepIgnoresVanishedPlayers();
231231

232+
boolean isVanishFakeJoinLeave();
233+
232234
boolean isAfkListName();
233235

234236
String getAfkListName();

Essentials/src/main/java/com/earth2me/essentials/Settings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,11 @@ public boolean sleepIgnoresVanishedPlayers() {
13501350
return config.getBoolean("sleep-ignores-vanished-player", true);
13511351
}
13521352

1353+
@Override
1354+
public boolean isVanishFakeJoinLeave() {
1355+
return config.getBoolean("vanish-fake-join-leave", false);
1356+
}
1357+
13531358
public String _getAfkListName() {
13541359
return FormatUtil.replaceFormat(config.getString("afk-list-name", "none"));
13551360
}

Essentials/src/main/resources/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@ sleep-ignores-afk-players: true
544544
# Players with the permission 'essentials.sleepingignored' will always be ignored.
545545
sleep-ignores-vanished-player: true
546546

547+
# Whether or not fake join and leave messages should be sent to the ingame-chat when a player toggles vanish.
548+
vanish-fake-join-leave: false
549+
547550
# Change the player's /list name when they are AFK. This is none by default, which specifies that Essentials
548551
# should not interfere with the AFK player's /list name.
549552
# You may use color codes, {USERNAME} for the player's name, or {PLAYER} for the player's display name.

0 commit comments

Comments
 (0)