Add configuration option to kick players when toggling premium state

Fixes #1285
This commit is contained in:
TuxCoding
2025-04-23 17:12:32 +02:00
parent 1464ef2952
commit 568ad7a621
6 changed files with 57 additions and 25 deletions

View File

@ -30,6 +30,7 @@ import com.github.games647.fastlogin.bukkit.event.BukkitFastLoginPremiumToggleEv
import com.github.games647.fastlogin.core.storage.StoredProfile; import com.github.games647.fastlogin.core.storage.StoredProfile;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import static com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent.PremiumToggleReason; import static com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent.PremiumToggleReason;
@ -57,6 +58,7 @@ public class CrackedCommand extends ToggleCommand {
return; return;
} }
Player player = (Player) sender;
if (forwardCrackedCommand(sender, sender.getName())) { if (forwardCrackedCommand(sender, sender.getName())) {
return; return;
} }
@ -71,7 +73,16 @@ public class CrackedCommand extends ToggleCommand {
plugin.getScheduler().runAsync(() -> { plugin.getScheduler().runAsync(() -> {
plugin.getCore().getStorage().save(profile); plugin.getCore().getStorage().save(profile);
plugin.getServer().getPluginManager().callEvent( plugin.getServer().getPluginManager().callEvent(
new BukkitFastLoginPremiumToggleEvent(sender, profile, PremiumToggleReason.COMMAND_OTHER)); new BukkitFastLoginPremiumToggleEvent(sender, profile, PremiumToggleReason.COMMAND_OTHER)
);
plugin.getScheduler().getSyncExecutor().execute(() -> {
if (plugin.getCore().getConfig().getBoolean("kick-toggle", true)) {
player.kickPlayer(plugin.getCore().getMessage("remove-premium"));
} else {
plugin.getCore().sendLocaleMessage("add-premium", sender);
}
});
}); });
} else { } else {
plugin.getCore().sendLocaleMessage("not-premium", sender); plugin.getCore().sendLocaleMessage("not-premium", sender);

View File

@ -68,7 +68,8 @@ public class PremiumCommand extends ToggleCommand {
return; return;
} }
UUID id = ((Player) sender).getUniqueId(); Player player = (Player) sender;
UUID id = player.getUniqueId();
if (plugin.getConfig().getBoolean("premium-warning") && !plugin.getCore().getPendingConfirms().contains(id)) { if (plugin.getConfig().getBoolean("premium-warning") && !plugin.getCore().getPendingConfirms().contains(id)) {
sender.sendMessage(plugin.getCore().getMessage("premium-warning")); sender.sendMessage(plugin.getCore().getMessage("premium-warning"));
plugin.getCore().getPendingConfirms().add(id); plugin.getCore().getPendingConfirms().add(id);
@ -86,10 +87,17 @@ public class PremiumCommand extends ToggleCommand {
plugin.getScheduler().runAsync(() -> { plugin.getScheduler().runAsync(() -> {
plugin.getCore().getStorage().save(profile); plugin.getCore().getStorage().save(profile);
plugin.getServer().getPluginManager().callEvent( plugin.getServer().getPluginManager().callEvent(
new BukkitFastLoginPremiumToggleEvent(sender, profile, PremiumToggleReason.COMMAND_SELF)); new BukkitFastLoginPremiumToggleEvent(sender, profile, PremiumToggleReason.COMMAND_SELF)
}); );
plugin.getCore().sendLocaleMessage("add-premium", sender); plugin.getScheduler().getSyncExecutor().execute(() -> {
if (plugin.getCore().getConfig().getBoolean("kick-toggle", true)) {
player.kickPlayer(plugin.getCore().getMessage("remove-premium"));
} else {
plugin.getCore().sendLocaleMessage("add-premium", sender);
}
});
});
} }
} }
@ -117,7 +125,8 @@ public class PremiumCommand extends ToggleCommand {
plugin.getScheduler().runAsync(() -> { plugin.getScheduler().runAsync(() -> {
plugin.getCore().getStorage().save(profile); plugin.getCore().getStorage().save(profile);
plugin.getServer().getPluginManager().callEvent( plugin.getServer().getPluginManager().callEvent(
new BukkitFastLoginPremiumToggleEvent(sender, profile, PremiumToggleReason.COMMAND_OTHER)); new BukkitFastLoginPremiumToggleEvent(sender, profile, PremiumToggleReason.COMMAND_OTHER)
);
}); });
plugin.getCore().sendLocaleMessage("add-premium-other", sender); plugin.getCore().sendLocaleMessage("add-premium-other", sender);

View File

@ -76,7 +76,12 @@ public class AsyncToggleMessage implements Runnable {
? PremiumToggleReason.COMMAND_OTHER : PremiumToggleReason.COMMAND_SELF; ? PremiumToggleReason.COMMAND_OTHER : PremiumToggleReason.COMMAND_SELF;
core.getPlugin().getProxy().getPluginManager().callEvent( core.getPlugin().getProxy().getPluginManager().callEvent(
new BungeeFastLoginPremiumToggleEvent(playerProfile, reason)); new BungeeFastLoginPremiumToggleEvent(playerProfile, reason));
sendMessage("remove-premium");
if (isPlayerSender && core.getConfig().getBoolean("kick-toggle", true)) {
sender.disconnect(TextComponent.fromLegacyText(core.getMessage("remove-premium")));
} else {
sendMessage("remove-premium");
}
} }
private void activatePremium() { private void activatePremium() {

View File

@ -150,11 +150,14 @@ nameChangeCheck: false
forwardSkin: true forwardSkin: true
# Displays a warning message that this message SHOULD only be invoked by # Displays a warning message that this message SHOULD only be invoked by
# users who actually are the owner of this account. So not by cracked players # users who actually are the owner of this account (and not cracked players)
# #
# If they still want to invoke the command, they have to invoke /premium again # If they still want to invoke the command, they have to invoke /premium again
premium-warning: true premium-warning: true
# Kick players after they confirmed the command from above.
kick-toggle: true
# ======[[ Spigot+ProtocolLib users only ]]====== # ======[[ Spigot+ProtocolLib users only ]]======
# When set to true, enables the use of alternative session resolver which does not send the server IP # When set to true, enables the use of alternative session resolver which does not send the server IP
# to mojang session servers. This setting might be useful when you are trying to run the server via a # to mojang session servers. This setting might be useful when you are trying to run the server via a

View File

@ -83,12 +83,12 @@ public class PluginMessageListener {
plugin.getScheduler().runAsync(() -> readMessage(forPlayer, channel, data)); plugin.getScheduler().runAsync(() -> readMessage(forPlayer, channel, data));
} }
private void readMessage(Player forPlayer, String channel, byte[] data) { private void readMessage(Player sender, String channel, byte[] data) {
FastLoginCore<Player, CommandSource, FastLoginVelocity> core = plugin.getCore(); FastLoginCore<Player, CommandSource, FastLoginVelocity> core = plugin.getCore();
ByteArrayDataInput dataInput = ByteStreams.newDataInput(data); ByteArrayDataInput dataInput = ByteStreams.newDataInput(data);
if (successChannel.equals(channel)) { if (successChannel.equals(channel)) {
onSuccessMessage(forPlayer); onSuccessMessage(sender);
} else if (changeChannel.equals(channel)) { } else if (changeChannel.equals(channel)) {
ChangePremiumMessage changeMessage = new ChangePremiumMessage(); ChangePremiumMessage changeMessage = new ChangePremiumMessage();
changeMessage.readFrom(dataInput); changeMessage.readFrom(dataInput);
@ -97,19 +97,19 @@ public class PluginMessageListener {
boolean isSourceInvoker = changeMessage.isSourceInvoker(); boolean isSourceInvoker = changeMessage.isSourceInvoker();
if (changeMessage.shouldEnable()) { if (changeMessage.shouldEnable()) {
Boolean premiumWarning = plugin.getCore().getConfig().get("premium-warning", true); Boolean premiumWarning = plugin.getCore().getConfig().get("premium-warning", true);
if (playerName.equals(forPlayer.getUsername()) && premiumWarning if (playerName.equals(sender.getUsername()) && premiumWarning
&& !core.getPendingConfirms().contains(forPlayer.getUniqueId())) { && !core.getPendingConfirms().contains(sender.getUniqueId())) {
String message = core.getMessage("premium-warning"); String message = core.getMessage("premium-warning");
forPlayer.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(message)); sender.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(message));
core.getPendingConfirms().add(forPlayer.getUniqueId()); core.getPendingConfirms().add(sender.getUniqueId());
return; return;
} }
core.getPendingConfirms().remove(forPlayer.getUniqueId()); core.getPendingConfirms().remove(sender.getUniqueId());
Runnable task = new AsyncToggleMessage(core, forPlayer, playerName, true, isSourceInvoker); Runnable task = new AsyncToggleMessage(core, sender, playerName, true, isSourceInvoker);
plugin.getScheduler().runAsync(task); plugin.getScheduler().runAsync(task);
} else { } else {
Runnable task = new AsyncToggleMessage(core, forPlayer, playerName, false, isSourceInvoker); Runnable task = new AsyncToggleMessage(core, sender, playerName, false, isSourceInvoker);
plugin.getScheduler().runAsync(task); plugin.getScheduler().runAsync(task);
} }
} }

View File

@ -33,29 +33,26 @@ import com.github.games647.fastlogin.velocity.event.VelocityFastLoginPremiumTogg
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.ConsoleCommandSource; import com.velocitypowered.api.proxy.ConsoleCommandSource;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
public class AsyncToggleMessage implements Runnable { public class AsyncToggleMessage implements Runnable {
private final FastLoginCore<Player, CommandSource, FastLoginVelocity> core; private final FastLoginCore<Player, CommandSource, FastLoginVelocity> core;
private final CommandSource sender; private final Player sender;
private final String senderName; private final String senderName;
private final String targetPlayer; private final String targetPlayer;
private final boolean toPremium; private final boolean toPremium;
private final boolean isPlayerSender; private final boolean isPlayerSender;
public AsyncToggleMessage(FastLoginCore<Player, CommandSource, FastLoginVelocity> core, public AsyncToggleMessage(FastLoginCore<Player, CommandSource, FastLoginVelocity> core,
CommandSource sender, String playerName, boolean toPremium, boolean playerSender) { Player sender, String playerName, boolean toPremium, boolean playerSender) {
this.core = core; this.core = core;
this.sender = sender; this.sender = sender;
this.targetPlayer = playerName; this.targetPlayer = playerName;
this.toPremium = toPremium; this.toPremium = toPremium;
this.isPlayerSender = playerSender; this.isPlayerSender = playerSender;
if (sender instanceof Player playSender) { this.senderName = sender.getUsername();
senderName = playSender.getUsername();
} else {
senderName = "";
}
} }
@Override @Override
@ -82,7 +79,14 @@ public class AsyncToggleMessage implements Runnable {
? PremiumToggleReason.COMMAND_OTHER : PremiumToggleReason.COMMAND_SELF; ? PremiumToggleReason.COMMAND_OTHER : PremiumToggleReason.COMMAND_SELF;
core.getPlugin().getProxy().getEventManager().fire( core.getPlugin().getProxy().getEventManager().fire(
new VelocityFastLoginPremiumToggleEvent(playerProfile, reason)); new VelocityFastLoginPremiumToggleEvent(playerProfile, reason));
sendMessage("remove-premium");
if (isPlayerSender && core.getConfig().getBoolean("kick-toggle", true)) {
TextComponent msg = LegacyComponentSerializer.legacyAmpersand()
.deserialize(core.getMessage("remove-premium"));
sender.disconnect(msg);
} else {
sendMessage("remove-premium");
}
} }
private void activatePremium() { private void activatePremium() {