Skip to content

Commit

Permalink
Add payload & handshake checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNature committed Nov 27, 2024
1 parent 50e28c1 commit 2774f83
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ public class ApolloExamplePlugin extends JavaPlugin {
private VignetteExample vignetteExample;
private WaypointExample waypointExample;

private DebugManager debugManager;
private SpamPacketDebug spamPacketDebug;

@Override
Expand Down Expand Up @@ -358,12 +357,12 @@ private void registerModuleExamples() {
}

private void registerListeners() {
this.debugManager = new DebugManager();
this.spamPacketDebug = new SpamPacketDebug();

switch (TYPE) {
case API: {
this.playerApiListener = new ApolloPlayerApiListener(this);
new DebugManager();
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package com.lunarclient.apollo.example.common.commands.debug;

import com.lunarclient.apollo.common.ApolloComponent;
import com.lunarclient.apollo.example.ApolloExamplePlugin;
import com.lunarclient.apollo.example.common.modules.ApolloExampleType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
Expand All @@ -42,6 +44,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

if (ApolloExamplePlugin.TYPE != ApolloExampleType.API) {
sender.sendMessage("Debug is only available with the API example type!");
sender.sendMessage("Run '/switch API' for more info");
return true;
}

Player player = (Player) sender;

if (args.length < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package com.lunarclient.apollo.example.common.commands.debug;

import com.lunarclient.apollo.common.ApolloComponent;
import com.lunarclient.apollo.example.ApolloExamplePlugin;
import com.lunarclient.apollo.example.debug.DebugManager;
import com.lunarclient.apollo.example.debug.impl.BorderCollisionTest;
import net.kyori.adventure.text.Component;
Expand All @@ -38,8 +37,6 @@

public class BordersCommand implements CommandExecutor {

private final DebugManager debugManager = ApolloExamplePlugin.getPlugin().getDebugManager();

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)) {
Expand All @@ -48,6 +45,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

Player player = (Player) sender;
DebugManager debugManager = DebugManager.getInstance();

if (args.length < 2) {
this.sendUsage(player);
Expand All @@ -56,12 +54,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

switch (args[1].toLowerCase()) {
case "start": {
this.debugManager.start(player, new BorderCollisionTest(player));
debugManager.start(player, new BorderCollisionTest(player));
break;
}

case "stop": {
this.debugManager.stop(player);
debugManager.stop(player);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.lunarclient.apollo.example.debug;

import com.lunarclient.apollo.example.ApolloExamplePlugin;
import com.lunarclient.apollo.example.debug.payload.PayloadListener;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
Expand All @@ -43,6 +44,9 @@ public DebugManager() {
instance = this;

this.players = new HashMap<>();

new PayloadListener();

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.debug.payload;

import com.lunarclient.apollo.Apollo;
import com.lunarclient.apollo.event.ApolloListener;
import com.lunarclient.apollo.event.EventBus;
import com.lunarclient.apollo.event.Listen;
import com.lunarclient.apollo.event.player.ApolloPlayerHandshakeEvent;
import com.lunarclient.apollo.example.ApolloExamplePlugin;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class PayloadListener implements Listener, ApolloListener {

private final Set<UUID> handshakeUuids;

public PayloadListener() {
this.handshakeUuids = new HashSet<>();

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

private void checkReceivedPackets(Player player) {
Bukkit.getScheduler().runTaskLater(ApolloExamplePlugin.getPlugin(), () -> {
UUID uuid = player.getUniqueId();

boolean register = Apollo.getPlayerManager().hasSupport(uuid);
boolean handshake = this.handshakeUuids.contains(uuid);

if (register && !handshake) {
player.sendMessage(ChatColor.RED + "Received Apollo register but not the handshake!");
} else if (!register && !handshake) {
player.sendMessage(ChatColor.RED + "Failed to receive Apollo register and handshake!");
}
}, 20 * 3);
}

@EventHandler
private void onPlayerJoin(PlayerJoinEvent event) {
this.checkReceivedPackets(event.getPlayer());
}

@EventHandler
private void onPlayerQuit(PlayerQuitEvent event) {
this.handshakeUuids.remove(event.getPlayer().getUniqueId());
}

@Listen
private void onApolloPlayerHandshake(ApolloPlayerHandshakeEvent event) {
this.handshakeUuids.add(event.getPlayer().getUniqueId());
}

}

0 comments on commit 2774f83

Please sign in to comment.