Skip to content

Commit

Permalink
Feature - Update broadcaster (#29)
Browse files Browse the repository at this point in the history
* Add version comparison

* Fetch latest version

* Send update message to opped players

* Move player listeners to a separate class (#30)

* add an options container to ApolloPlatform

* Fix conflicts

* Add registerOptions method

* Remove gson

* give saveOptions the list of option keys

* Fix no such method call on older velocity versions

* Check the updater config key on join

---------

Co-authored-by: connorhartley <[email protected]>
  • Loading branch information
ItsNature and vectrixdevelops authored Sep 21, 2023
1 parent a86119b commit 7494ae4
Show file tree
Hide file tree
Showing 11 changed files with 569 additions and 119 deletions.
17 changes: 17 additions & 0 deletions api/src/main/java/com/lunarclient/apollo/ApolloPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.lunarclient.apollo;

import com.lunarclient.apollo.option.Options;
import java.util.logging.Logger;
import org.jetbrains.annotations.ApiStatus;

/**
Expand All @@ -50,6 +51,22 @@ public interface ApolloPlatform {
*/
Options getOptions();

/**
* Returns the current Apollo version.
*
* @return the current apollo version
* @since 1.0.0
*/
String getApolloVersion();

/**
* Returns the servers logger.
*
* @return the servers logger
* @since 1.0.0
*/
Logger getPlatformLogger();

/**
* Represents the kind of server a platform is.
*
Expand Down
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 @@ -62,19 +64,11 @@
import com.lunarclient.apollo.module.waypoint.WaypointModuleImpl;
import com.lunarclient.apollo.option.Options;
import com.lunarclient.apollo.option.OptionsImpl;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
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.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
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 @@ -83,7 +77,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 @@ -94,7 +88,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 @@ -139,37 +135,14 @@ public Kind getKind() {
return Kind.SERVER;
}

@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());
@Override
public String getApolloVersion() {
return this.getDescription().getVersion();
}

@EventHandler
private void onPlayerQuit(PlayerQuitEvent event) {
((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).removePlayer(event.getPlayer().getUniqueId());
@Override
public Logger getPlatformLogger() {
return Bukkit.getServer().getLogger();
}

private void handlePacket(Player player, byte[] bytes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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 (!Apollo.getPlatform().getOptions().get(ApolloVersionManager.SEND_UPDATE_MESSAGE)) {
return;
}

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,27 +23,21 @@
*/
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.option.Options;
import com.lunarclient.apollo.option.OptionsImpl;
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;

Expand All @@ -53,14 +47,13 @@ public final class ApolloBungeePlatform extends Plugin implements ApolloPlatform
public void onEnable() {
ApolloBungeePlatform.instance = this;

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

ApolloManager.bootstrap(this);

ApolloManager.loadConfiguration(this.getDataFolder().toPath());
((ApolloModuleManagerImpl) Apollo.getModuleManager()).enableModules();
ApolloManager.saveConfiguration();

this.getProxy().getPluginManager().registerListener(this, new ApolloPlayerListener());
this.getProxy().registerChannel(ApolloManager.PLUGIN_MESSAGE_CHANNEL);
}

Expand All @@ -76,33 +69,14 @@ public Kind getKind() {
return Kind.PROXY;
}

@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));
@Override
public String getApolloVersion() {
return this.getDescription().getVersion();
}

@EventHandler
private void onDisconnect(PlayerDisconnectEvent event) {
ProxiedPlayer player = event.getPlayer();
((ApolloPlayerManagerImpl) Apollo.getPlayerManager()).removePlayer(player.getUniqueId());
@Override
public Logger getPlatformLogger() {
return ProxyServer.getInstance().getLogger();
}

}
Loading

0 comments on commit 7494ae4

Please sign in to comment.