Skip to content

Commit

Permalink
improve spam packets command
Browse files Browse the repository at this point in the history
  • Loading branch information
vectrixdevelops committed Oct 22, 2024
1 parent 584d56c commit 2ae4dd0
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.lunarclient.apollo.example;

import com.lunarclient.apollo.event.EventBus;
import com.lunarclient.apollo.example.commands.ApolloDebugCommand;
import com.lunarclient.apollo.example.commands.BeamCommand;
import com.lunarclient.apollo.example.commands.BorderCommand;
import com.lunarclient.apollo.example.commands.ChatCommand;
Expand All @@ -49,7 +50,6 @@
import com.lunarclient.apollo.example.commands.TransferCommand;
import com.lunarclient.apollo.example.commands.VignetteCommand;
import com.lunarclient.apollo.example.commands.WaypointCommand;
import com.lunarclient.apollo.example.commands.debug.SpamPacketDebugCommand;
import com.lunarclient.apollo.example.debug.SpamPacketDebug;
import com.lunarclient.apollo.example.listeners.PlayerListener;
import com.lunarclient.apollo.example.modules.BeamExample;
Expand Down Expand Up @@ -185,7 +185,7 @@ private void registerCommands() {
this.getCommand("vignette").setExecutor(new VignetteCommand());
this.getCommand("waypoint").setExecutor(new WaypointCommand());

this.getCommand("spampacketdebug").setExecutor(new SpamPacketDebugCommand());
this.getCommand("apollodebug").setExecutor(new ApolloDebugCommand());
}

private void registerListeners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,41 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.lunarclient.apollo.example.commands.debug;
package com.lunarclient.apollo.example.commands;

import com.lunarclient.apollo.example.ApolloExamplePlugin;
import com.lunarclient.apollo.example.debug.SpamPacketDebug;
import com.lunarclient.apollo.common.ApolloComponent;
import com.lunarclient.apollo.example.commands.debug.SpamPacketsCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class SpamPacketDebugCommand implements CommandExecutor {

private final SpamPacketDebug spamPacketDebug = ApolloExamplePlugin.getPlugin().getSpamPacketDebug();

public class ApolloDebugCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)) {
if(!(sender instanceof Player)) {
sender.sendMessage("Player only!");
return true;
}

Player player = (Player) sender;

if (args.length != 1) {
player.sendMessage("Usage: /spampacketdebug <start|pause|stop>");
return true;
}

switch (args[0].toLowerCase()) {
case "start": {
this.spamPacketDebug.start(player);
player.sendMessage("Debug started.");
break;
}

case "pause": {
this.spamPacketDebug.pause(player);
player.sendMessage("Debug paused.");
break;
}

case "stop": {
this.spamPacketDebug.stop();
player.sendMessage("Debug stopped.");
break;
}

default: {
player.sendMessage("Usage: /spampacketdebug <start|pause|stop>");
break;
}
if (args.length < 1) {
player.sendMessage(ApolloComponent.toLegacy(Component.text()
.append(Component.text("-------------------------------------", NamedTextColor.GRAY, TextDecoration.STRIKETHROUGH))
.appendNewline()
.append(Component.text("/apollodebug spampackets [start|stop|stopall] ", NamedTextColor.WHITE))
.append(Component.text("# Spam modsetting update packets to the client.", NamedTextColor.GREEN))
.appendNewline()
.append(Component.text("-------------------------------------", NamedTextColor.GRAY, TextDecoration.STRIKETHROUGH))
.build()
));
} else if(args[0].equalsIgnoreCase("spampackets")) {
return new SpamPacketsCommand().onCommand(sender, command, label, args);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* This file is part of Apollo, licensed under the MIT License.
*
* Copyright (c) 2023 Moonsworth
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.lunarclient.apollo.example.commands.debug;

import com.lunarclient.apollo.common.ApolloComponent;
import com.lunarclient.apollo.example.ApolloExamplePlugin;
import com.lunarclient.apollo.example.debug.SpamPacketDebug;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class SpamPacketsCommand implements CommandExecutor {

private final SpamPacketDebug spamPacketDebug = ApolloExamplePlugin.getPlugin().getSpamPacketDebug();

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Player only!");
return true;
}

Player player = (Player) sender;

if (args.length < 2) {
this.sendUsage(player);
} else {
switch (args[1].toLowerCase()) {
case "start": {
int amount = args.length > 2 ? Integer.parseInt(args[2]) : Integer.MAX_VALUE;
int delay = args.length > 3 ? Integer.parseInt(args[3]) : 50;

this.spamPacketDebug.start(player, amount, delay, () -> player.sendMessage("Debug completed!"));

player.sendMessage("Debug started. (Amount: " + amount + ", Delay: " + delay + "ms)");
break;
}

case "stop": {
this.spamPacketDebug.stop(player);
player.sendMessage("Debug stopped.");
break;
}

case "stopall": {
this.spamPacketDebug.stopAll();
player.sendMessage("Debug stopped for all debuggers.");
break;
}

default: {
this.sendUsage(player);
break;
}
}
}

return true;
}

private void sendUsage(Player player) {
player.sendMessage(ApolloComponent.toLegacy(Component.text()
.append(Component.text("-------------------------------------", NamedTextColor.GRAY, TextDecoration.STRIKETHROUGH))
.appendNewline()
.append(Component.text("/apollodebug spampackets start <amount> <delay> ", NamedTextColor.WHITE))
.append(Component.text("# Starts spamming modsetting update packets to the client, delay is in milliseconds.", NamedTextColor.GREEN))
.appendNewline()
.append(Component.text("/apollodebug spampackets stop ", NamedTextColor.WHITE))
.append(Component.text("# Stop spamming modsetting update packets to the client.", NamedTextColor.GREEN))
.appendNewline()
.append(Component.text("/apollodebug spampackets stopall ", NamedTextColor.WHITE))
.append(Component.text("# Stops spamming modsetting update packets to the client, for all debuggers.", NamedTextColor.GREEN))
.appendNewline()
.append(Component.text("-------------------------------------", NamedTextColor.GRAY, TextDecoration.STRIKETHROUGH))
.build()
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
package com.lunarclient.apollo.example.debug;

import com.google.common.collect.Sets;
import com.google.common.collect.Maps;
import com.lunarclient.apollo.Apollo;
import com.lunarclient.apollo.example.ApolloExamplePlugin;
import com.lunarclient.apollo.network.NetworkOptions;
import java.util.Set;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand All @@ -37,39 +37,55 @@

public class SpamPacketDebug implements Listener {

private final Set<UUID> players = Sets.newConcurrentHashSet();

private final SpamPacketTask debug;
private final Map<UUID, SpamPacketTask> players = Maps.newConcurrentMap();

public SpamPacketDebug() {
this.debug = new SpamPacketTask();

Bukkit.getPluginManager().registerEvents(this, ApolloExamplePlugin.getPlugin());
}

@EventHandler
private void onPlayerQuit(PlayerQuitEvent event) {
this.pause(event.getPlayer());
this.stop(event.getPlayer());
}

public void start(Player player) {
this.players.add(player.getUniqueId());
public void start(Player player, int amount, long delay, Runnable callback) {
UUID playerIdentifier = player.getUniqueId();
SpamPacketTask task = new SpamPacketTask(playerIdentifier, amount, delay, callback);
this.players.put(playerIdentifier, task);
task.start();
}

if(!this.debug.running) {
this.debug.start();
public void stop(Player player) {
UUID playerIdentifier = player.getUniqueId();
SpamPacketTask task = this.players.remove(playerIdentifier);
if (task != null) {
task.stop();
}
}

public void pause(Player player) {
this.players.remove(player.getUniqueId());
}
public void stopAll() {
for (SpamPacketTask task : this.players.values()) {
task.stop();
}

public void stop() {
this.players.clear();
}

public class SpamPacketTask implements Runnable {
private boolean running;
public static class SpamPacketTask implements Runnable {
private final UUID player;
private final int amount;
private final long delay;
private final Runnable onComplete;

private volatile boolean running;
private int sent;

public SpamPacketTask(UUID player, int amount, long delay, Runnable onComplete) {
this.player = player;
this.amount = amount;
this.delay = delay;
this.onComplete = onComplete;
}

public void start() {
this.running = true;
Expand All @@ -79,21 +95,26 @@ public void start() {
debugThread.start();
}

public void stop() {
this.running = false;
}

@Override
public void run() {
while (this.running) {
Set<UUID> players = SpamPacketDebug.this.players;
if (players.isEmpty()) {
if (this.sent > this.amount) {
this.onComplete.run();
this.running = false;
break;
}

for (UUID uuid : players) {
Apollo.getPlayerManager().getPlayer(uuid)
.ifPresent(apolloPlayer -> NetworkOptions.sendOptions(Apollo.getModuleManager().getModules(), true, apolloPlayer));
}
Apollo.getPlayerManager().getPlayer(this.player)
.ifPresent(apolloPlayer -> NetworkOptions.sendOptions(Apollo.getModuleManager().getModules(), true, apolloPlayer));

this.sent++;

try {
Thread.sleep(1);
Thread.sleep(this.delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
Expand Down
4 changes: 2 additions & 2 deletions bukkit-example/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ commands:
description: "Vignette!"
waypoint:
description: "Waypoints!"
spampacketdebug:
description: "Spam Packet Debug!"
apollodebug:
description: "Apollo Debug!"

0 comments on commit 2ae4dd0

Please sign in to comment.