Skip to content

Commit

Permalink
Don't inject hook directly
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
voruti committed Feb 3, 2024
1 parent d885ea6 commit 8168372
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import lombok.NonNull;
import lombok.experimental.FieldDefaults;
import org.slf4j.Logger;
import voruti.velocityplayerlistquery.hook.VanishBridgeHook;
import voruti.velocityplayerlistquery.hook.Hooks;
import voruti.velocityplayerlistquery.model.exception.InvalidServerPingException;
import voruti.velocityplayerlistquery.service.ConfigService;
import voruti.velocityplayerlistquery.service.serverpingprocessor.ServerPingProcessor;
Expand Down Expand Up @@ -45,14 +45,15 @@ public class VelocityPlayerListQuery {
ServerPingProcessorRegistry serverPingProcessorRegistry;

@Inject
VanishBridgeHook vanishBridgeHook;
Hooks hooks;


@Subscribe
public void onProxyInitialization(ProxyInitializeEvent ignored) {
this.configService.reloadConfig();

if (this.vanishBridgeHook.hooked()) this.logger.info("VanishBridge found, enabling vanish support");
this.hooks.vanishBridge().ifPresent(unused ->
this.logger.info("VanishBridge found, enabling vanish support"));

this.logger.info("Enabled");
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/voruti/velocityplayerlistquery/hook/Hooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package voruti.velocityplayerlistquery.hook;

import com.google.inject.Inject;
import com.velocitypowered.api.proxy.ProxyServer;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;

import javax.inject.Singleton;
import java.util.Optional;

@Singleton
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class Hooks {

VanishBridgeHook vanishBridgeHook;


@Inject
public Hooks(ProxyServer server) {
this.vanishBridgeHook = server.getPluginManager().isLoaded("vanishbridge")
? new VanishBridgeHook(server)
: null;
}


public Optional<VanishBridgeHook> vanishBridge() {
return Optional.ofNullable(vanishBridgeHook);
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package voruti.velocityplayerlistquery.hook;

import com.google.inject.Inject;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import dev.loapu.vanishbridge.api.VanishBridgeProvider;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.FieldDefaults;

import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.Collection;

@Singleton
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class VanishBridgeHook {
@Inject

ProxyServer server;

public boolean hooked() {
return server.getPluginManager().isLoaded("vanishbridge");
}

public Collection<Player> unvanishedPlayers() {
var allPlayers = server.getAllPlayers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.velocitypowered.api.proxy.server.ServerPing;
import lombok.NonNull;
import lombok.experimental.FieldDefaults;
import voruti.velocityplayerlistquery.hook.Hooks;
import voruti.velocityplayerlistquery.hook.VanishBridgeHook;
import voruti.velocityplayerlistquery.service.ConfigService;

Expand All @@ -21,7 +22,7 @@ public class OnlinePlayerCountServerPingProcessor extends ServerPingProcessor {
ProxyServer proxyServer;

@Inject
VanishBridgeHook vanishBridgeHook;
Hooks hooks;


@Override
Expand All @@ -33,6 +34,10 @@ public boolean isEnabled() {
public void applyToServerPing(@NonNull final ServerPing.Builder serverPing) {
super.applyToServerPing(serverPing);

serverPing.onlinePlayers(this.vanishBridgeHook.hooked() ? this.vanishBridgeHook.unvanishedPlayerCount() : this.proxyServer.getPlayerCount());
serverPing.onlinePlayers(
this.hooks.vanishBridge()
.map(VanishBridgeHook::unvanishedPlayerCount)
.orElse(this.proxyServer.getPlayerCount())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.velocitypowered.api.proxy.server.ServerPing;
import lombok.NonNull;
import lombok.experimental.FieldDefaults;
import voruti.velocityplayerlistquery.hook.Hooks;
import voruti.velocityplayerlistquery.hook.VanishBridgeHook;
import voruti.velocityplayerlistquery.model.Config;
import voruti.velocityplayerlistquery.model.Config.PlayerListMode;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class PlayerListServerPingProcessor extends ServerPingProcessor {
ServerListEntryBuilderService serverListEntryBuilderService;

@Inject
VanishBridgeHook vanishBridgeHook;
Hooks hooks;


@Override
Expand All @@ -55,7 +56,9 @@ public void applyToServerPing(@NonNull final ServerPing.Builder serverPing) {
super.applyToServerPing(serverPing);

// collect players:
final Collection<Player> players = this.vanishBridgeHook.hooked() ? this.vanishBridgeHook.unvanishedPlayers() : this.proxyServer.getAllPlayers();
final Collection<Player> players = this.hooks.vanishBridge()
.map(VanishBridgeHook::unvanishedPlayers)
.orElse(this.proxyServer.getAllPlayers());
final Stream<ServerPing.SamplePlayer> playerStream = players.stream()
// format players:
.map(player -> new ServerPing.SamplePlayer(
Expand Down

0 comments on commit 8168372

Please sign in to comment.