Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move player listeners to a separate class #30

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package com.lunarclient.apollo;

import com.google.protobuf.Any;
import com.lunarclient.apollo.listener.ApolloPlayerListener;
import com.lunarclient.apollo.listener.ApolloWorldListener;
import com.lunarclient.apollo.module.ApolloModuleManagerImpl;
import com.lunarclient.apollo.module.beam.BeamModule;
import com.lunarclient.apollo.module.beam.BeamModuleImpl;
Expand Down Expand Up @@ -60,24 +62,11 @@
import com.lunarclient.apollo.module.vignette.VignetteModuleImpl;
import com.lunarclient.apollo.module.waypoint.WaypointModule;
import com.lunarclient.apollo.module.waypoint.WaypointModuleImpl;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.version.ApolloVersionManager;
import com.lunarclient.apollo.world.ApolloWorldManagerImpl;
import com.lunarclient.apollo.wrapper.BukkitApolloPlayer;
import com.lunarclient.apollo.wrapper.BukkitApolloWorld;
import java.util.logging.Logger;
import lombok.Getter;
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;
import org.bukkit.event.player.PlayerRegisterChannelEvent;
import org.bukkit.event.player.PlayerUnregisterChannelEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.Messenger;

Expand All @@ -86,7 +75,7 @@
*
* @since 1.0.0
*/
public final class ApolloBukkitPlatform extends JavaPlugin implements ApolloPlatform, Listener {
public final class ApolloBukkitPlatform extends JavaPlugin implements ApolloPlatform {

@Getter private static ApolloBukkitPlatform instance;

Expand All @@ -95,7 +84,9 @@ public void onEnable() {
ApolloBukkitPlatform.instance = this;
ApolloManager.bootstrap(this);

this.getServer().getPluginManager().registerEvents(this, this);
PluginManager pluginManager = this.getServer().getPluginManager();
pluginManager.registerEvents(new ApolloPlayerListener(), this);
pluginManager.registerEvents(new ApolloWorldListener(), this);

((ApolloModuleManagerImpl) Apollo.getModuleManager())
.addModule(BeamModule.class, new BeamModuleImpl())
Expand Down Expand Up @@ -152,51 +143,6 @@ public Logger getPlatformLogger() {
return Bukkit.getServer().getLogger();
}

@EventHandler
private void onWorldLoad(WorldLoadEvent event) {
((ApolloWorldManagerImpl) Apollo.getWorldManager()).addWorld(new BukkitApolloWorld(event.getWorld()));
}

@EventHandler
private void onWorldUnload(WorldUnloadEvent event) {
((ApolloWorldManagerImpl) Apollo.getWorldManager()).removeWorld(event.getWorld().getName());
}

@EventHandler
private void onRegisterChannel(PlayerRegisterChannelEvent event) {
if (!event.getChannel().equalsIgnoreCase(ApolloManager.PLUGIN_MESSAGE_CHANNEL)) {
return;
}

((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).addPlayer(new BukkitApolloPlayer(event.getPlayer()));
}

@EventHandler
private void onUnregisterChannel(PlayerUnregisterChannelEvent event) {
if (!event.getChannel().equalsIgnoreCase(ApolloManager.PLUGIN_MESSAGE_CHANNEL)) {
return;
}

((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).removePlayer(event.getPlayer().getUniqueId());
}

@EventHandler
private void onPlayerQuit(PlayerQuitEvent event) {
((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).removePlayer(event.getPlayer().getUniqueId());
}

@EventHandler
private void onPlayerJoin(PlayerJoinEvent event) {
if (!ApolloManager.getVersionManager().isNeedsUpdate()) {
return;
}

Player player = event.getPlayer();
if (player.isOp()) {
player.sendMessage(ChatColor.YELLOW + ApolloVersionManager.UPDATE_MESSAGE);
}
}

private void handlePacket(Player player, byte[] bytes) {
Apollo.getPlayerManager().getPlayer(player.getUniqueId()).ifPresent(apolloPlayer -> {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.listener;

import com.lunarclient.apollo.Apollo;
import com.lunarclient.apollo.ApolloManager;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.version.ApolloVersionManager;
import com.lunarclient.apollo.wrapper.BukkitApolloPlayer;
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;
import org.bukkit.event.player.PlayerRegisterChannelEvent;
import org.bukkit.event.player.PlayerUnregisterChannelEvent;

/**
* Handles registration and un-registration of Apollo players.
*
* @since 1.0.0
*/
public final class ApolloPlayerListener implements Listener {

@EventHandler
private void onRegisterChannel(PlayerRegisterChannelEvent event) {
if (!event.getChannel().equalsIgnoreCase(ApolloManager.PLUGIN_MESSAGE_CHANNEL)) {
return;
}

((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).addPlayer(new BukkitApolloPlayer(event.getPlayer()));
}

@EventHandler
private void onUnregisterChannel(PlayerUnregisterChannelEvent event) {
if (!event.getChannel().equalsIgnoreCase(ApolloManager.PLUGIN_MESSAGE_CHANNEL)) {
return;
}

((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).removePlayer(event.getPlayer().getUniqueId());
}

@EventHandler
private void onPlayerQuit(PlayerQuitEvent event) {
((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).removePlayer(event.getPlayer().getUniqueId());
}

@EventHandler
private void onPlayerJoin(PlayerJoinEvent event) {
if (!ApolloManager.getVersionManager().isNeedsUpdate()) {
return;
}

Player player = event.getPlayer();
if (player.isOp()) {
player.sendMessage(ChatColor.YELLOW + ApolloVersionManager.UPDATE_MESSAGE);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.listener;

import com.lunarclient.apollo.Apollo;
import com.lunarclient.apollo.world.ApolloWorldManagerImpl;
import com.lunarclient.apollo.wrapper.BukkitApolloWorld;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;

/**
* Handles registration and un-registration of Apollo worlds.
*
* @since 1.0.0
*/
public final class ApolloWorldListener implements Listener {

@EventHandler
private void onWorldLoad(WorldLoadEvent event) {
((ApolloWorldManagerImpl) Apollo.getWorldManager()).addWorld(new BukkitApolloWorld(event.getWorld()));
}

@EventHandler
private void onWorldUnload(WorldUnloadEvent event) {
((ApolloWorldManagerImpl) Apollo.getWorldManager()).removeWorld(event.getWorld().getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,27 @@
*/
package com.lunarclient.apollo;

import com.google.common.base.Charsets;
import com.lunarclient.apollo.listener.ApolloPlayerListener;
import com.lunarclient.apollo.module.ApolloModuleManagerImpl;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.wrapper.BungeeApolloPlayer;
import java.util.logging.Logger;
import lombok.Getter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;

/**
* The Bungee platform plugin.
*
* @since 1.0.0
*/
public final class ApolloBungeePlatform extends Plugin implements ApolloPlatform, Listener {
public final class ApolloBungeePlatform extends Plugin implements ApolloPlatform {

@Getter private static ApolloBungeePlatform instance;

@Override
public void onEnable() {
ApolloBungeePlatform.instance = this;

this.getProxy().getPluginManager().registerListener(this, this);
this.getProxy().getPluginManager().registerListener(this, new ApolloPlayerListener());

ApolloManager.bootstrap(this);
ApolloManager.loadConfiguration(this.getDataFolder().toPath());
Expand Down Expand Up @@ -84,33 +77,4 @@ public Logger getPlatformLogger() {
return ProxyServer.getInstance().getLogger();
}

@EventHandler
private void onPluginMessage(PluginMessageEvent event) {
if (!(event.getReceiver() instanceof ProxyServer)) {
return;
}

if (!(event.getSender() instanceof ProxiedPlayer)) {
return;
}

if (!event.getTag().equals("REGISTER")) {
return;
}

String channels = new String(event.getData(), Charsets.UTF_8);
if (!channels.contains(ApolloManager.PLUGIN_MESSAGE_CHANNEL)) {
return;
}

ProxiedPlayer player = (ProxiedPlayer) event.getSender();
((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).addPlayer(new BungeeApolloPlayer(player));
}

@EventHandler
private void onDisconnect(PlayerDisconnectEvent event) {
ProxiedPlayer player = event.getPlayer();
((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).removePlayer(player.getUniqueId());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.listener;

import com.google.common.base.Charsets;
import com.lunarclient.apollo.Apollo;
import com.lunarclient.apollo.ApolloManager;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.wrapper.BungeeApolloPlayer;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

/**
* Handles registration and un-registration of Apollo players.
*
* @since 1.0.0
*/
public final class ApolloPlayerListener implements Listener {

@EventHandler
private void onPluginMessage(PluginMessageEvent event) {
if (!(event.getReceiver() instanceof ProxyServer)) {
return;
}

if (!(event.getSender() instanceof ProxiedPlayer)) {
return;
}

if (!event.getTag().equals("REGISTER")) {
return;
}

String channels = new String(event.getData(), Charsets.UTF_8);
if (!channels.contains(ApolloManager.PLUGIN_MESSAGE_CHANNEL)) {
return;
}

ProxiedPlayer player = (ProxiedPlayer) event.getSender();
((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).addPlayer(new BungeeApolloPlayer(player));
}

@EventHandler
private void onDisconnect(PlayerDisconnectEvent event) {
ProxiedPlayer player = event.getPlayer();
((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).removePlayer(player.getUniqueId());
}

}
Loading