2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2026-04-08 16:10:26 +00:00

player manager

This commit is contained in:
2026-04-04 10:40:46 +04:00
parent f58a212782
commit c97de82970
17 changed files with 311 additions and 76 deletions

View File

@@ -21,18 +21,44 @@ import java.util.UUID;
import net.limework.valiobungee.api.entity.NetworkPlayer;
import net.limework.valiobungee.api.entity.NetworkProxy;
/**
* The api interface
*
* @author Ham1255
* @since 1.0.0
*/
public interface ValioBungeeAPI {
/**
* @param uuid of player
* @return {@link Optional} if empty player is offline and if present player on the network
*/
Optional<NetworkPlayer> getNetworkPlayer(UUID uuid);
/**
* @param id the proxy id
* @return {@link Optional} if empty the proxy doesn't exist if present it's an online proxy
*/
Optional<NetworkProxy> getNetworkProxy(String id);
/**
* @return the local proxy
*/
NetworkProxy getLocalProxy();
/**
* @return Returns the network proxies that is currently online including the local
*/
Set<NetworkProxy> getNetworkProxies();
/**
* @return the local players empty if no players
*/
Set<NetworkPlayer> getLocalProxyPlayers();
/**
* @return the network players empty if no players
*/
Set<NetworkPlayer> getNetworkPlayers();
/**

View File

@@ -15,6 +15,8 @@
*/
package net.limework.valiobungee.api.entity;
import java.util.UUID;
/**
* Network player.
*
@@ -33,7 +35,16 @@ public interface NetworkPlayer {
boolean isLocal();
/**
* This used when we already had the object but player went offline since getting a player as
* {@link java.util.Optional} we check if its present if not means offline see {@link
* net.limework.valiobungee.api.ValioBungeeAPI#getNetworkPlayer(UUID)}
*
* @return true when a player is online on the network
*/
boolean isOnline();
/**
* @return player unique id
*/
UUID getUniqueId();
}

View File

@@ -1,31 +0,0 @@
/*
* Copyright (c) 2026 ValioBungee contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.limework.valiobungee.api.entity;
import java.util.UUID;
/**
* Network player. that uses UUID as an ID
*
* @author Ham1255
* @since 1.0.0
*/
public interface UUIDPlayer {
/**
* @return player unique id
*/
UUID getUniqueId();
}

View File

@@ -18,7 +18,6 @@ package net.limework.valiobungee.velocity.api.entities;
import com.velocitypowered.api.proxy.Player;
import java.util.Optional;
import net.limework.valiobungee.api.entity.NetworkPlayer;
import net.limework.valiobungee.api.entity.UUIDPlayer;
/**
* Velocity Network player. hold specific stuff for velocity platform
@@ -26,7 +25,7 @@ import net.limework.valiobungee.api.entity.UUIDPlayer;
* @author Ham1255
* @since 1.0.0
*/
public interface VelocityNetworkPlayer extends NetworkPlayer, UUIDPlayer {
public interface VelocityNetworkPlayer extends NetworkPlayer {
/**
* @return an optional that contains a player if he is on the same proxy

View File

@@ -1,7 +1,6 @@
dependencies {
compileOnly(project(":valiobungee-core"))
api(libs.redisson)
}
description = "ValioBungee Redisson implementation"

View File

@@ -18,4 +18,28 @@
*/
package net.limework.valiobungee.core;
public abstract class PlayerManager {}
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import net.limework.valiobungee.api.entity.NetworkPlayer;
public abstract class PlayerManager {
protected final ValioBungeePlatform platform;
public PlayerManager(ValioBungeePlatform platform) {
this.platform = platform;
}
public abstract Optional<NetworkPlayer> getNetworkPlayer(UUID uuid);
public abstract Set<NetworkPlayer> getNetworkPlayers();
public abstract boolean isOnline(UUID uuid);
public abstract void handleJoin(UUID uuid);
public abstract void handleQuit(UUID uuid);
public abstract void correctionTask();
}

View File

@@ -60,7 +60,7 @@ public abstract class ProxyNetworkManager {
(key, value, cause) -> {
if (cause == RemovalCause.EXPIRED) {
assert value != null;
log.warn("proxy {} has disconnected but did not send death message", value);
log.warn("proxy {} has disconnected but did not send death message", key);
}
})
.build();

View File

@@ -33,6 +33,8 @@ public interface ValioBungeePlatform extends ValioBungeeAPI {
ProxyNetworkManager proxyNetworkManager();
PlayerManager playerManager();
NetworkProxy proxyPlatformCreator(String id);
@Override

View File

@@ -22,16 +22,15 @@ import java.util.Objects;
import java.util.UUID;
import net.limework.valiobungee.api.entity.NetworkPlayer;
import net.limework.valiobungee.api.entity.NetworkProxy;
import net.limework.valiobungee.api.entity.UUIDPlayer;
import net.limework.valiobungee.core.ValioBungeePlatform;
public abstract class AbstractUUIDNetworkPlayer implements NetworkPlayer, UUIDPlayer {
public abstract class AbstractNetworkPlayer implements NetworkPlayer {
private final ValioBungeePlatform platform;
private final UUID uuid;
private final NetworkProxy proxy;
protected final ValioBungeePlatform platform;
protected final UUID uuid;
protected final NetworkProxy proxy;
public AbstractUUIDNetworkPlayer(ValioBungeePlatform platform, UUID uuid, NetworkProxy proxy) {
public AbstractNetworkPlayer(ValioBungeePlatform platform, UUID uuid, NetworkProxy proxy) {
this.platform = platform;
this.uuid = uuid;
this.proxy = proxy;
@@ -49,7 +48,7 @@ public abstract class AbstractUUIDNetworkPlayer implements NetworkPlayer, UUIDPl
@Override
public boolean equals(Object o) {
if (!(o instanceof AbstractUUIDNetworkPlayer that)) return false;
if (!(o instanceof AbstractNetworkPlayer that)) return false;
return Objects.equals(uuid, that.uuid) && Objects.equals(proxy, that.proxy);
}

View File

@@ -0,0 +1,9 @@
dependencies {
compileOnly(project(":valiobungee-core"))
}
description = "ValioBungee standalone `offline` implementation"

View File

@@ -0,0 +1,57 @@
/*
* Copyright (c) 2026 ValioBungee contributors
*
* This file is part of ValioBungee.
*
* ValioBungee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ValioBungee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ValioBungee. If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
*/
package net.limework.valiobungee.core;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import net.limework.valiobungee.api.entity.NetworkPlayer;
public class StandalonePlayerManager extends PlayerManager {
public StandalonePlayerManager(ValioBungeePlatform platform) {
super(platform);
}
@Override
public Optional<NetworkPlayer> getNetworkPlayer(UUID uuid) {
return platform.getLocalProxyPlayers().stream()
.filter(p -> p.getUniqueId().equals(uuid))
.findFirst();
}
@Override
public Set<NetworkPlayer> getNetworkPlayers() {
return platform.getLocalProxyPlayers();
}
@Override
public boolean isOnline(UUID uuid) {
return false;
}
@Override
public void handleJoin(UUID uuid) {}
@Override
public void handleQuit(UUID uuid) {}
@Override
public void correctionTask() {}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright (c) 2026 ValioBungee contributors
*
* This file is part of ValioBungee.
*
* ValioBungee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ValioBungee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ValioBungee. If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
*/
package net.limework.valiobungee.core;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicBoolean;
import net.limework.valiobungee.core.proto.messages.Death;
import net.limework.valiobungee.core.proto.messages.DeathReason;
import net.limework.valiobungee.core.proto.messages.Heartbeat;
import net.limework.valiobungee.core.proto.messages.ProxyInfo;
/** this class is used to debug the abstract class */
public class StandaloneProxyNetworkManager extends ProxyNetworkManager {
public StandaloneProxyNetworkManager(ValioBungeePlatform platform) {
super(platform);
}
@Override
protected void publishDeathPayload() {}
@Override
protected void publishHeartbeatPayload() {}
private final AtomicBoolean shutdown = new AtomicBoolean(false);
private void makeFakeProxiesRunning() {
// fake 12 proxies
String fakePrefix = "fake-proxy-";
for (int i = 0; i < 6; i++) {
String prefix = fakePrefix + i;
Heartbeat heartbeat =
Heartbeat.newBuilder()
.setSender(ProxyInfo.newBuilder().setProxyId(prefix).build())
.setOnlinePlayersCount(i)
.build();
Thread.ofVirtual()
.start(
() -> {
while (!shutdown.get()) {
handleProxyHeartBeat(heartbeat);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
handleProxyDeath(
Death.newBuilder()
.setReason(DeathReason.SHUTDOWN)
.setSender(heartbeat.getSender())
.build());
});
}
}
private void makeFakeProxiesDisappearing() {
// fake 12 proxies
String fakePrefix = "fake-disappear-proxy-";
for (int i = 0; i < 2; i++) {
String prefix = fakePrefix + i;
Heartbeat heartbeat =
Heartbeat.newBuilder()
.setSender(ProxyInfo.newBuilder().setProxyId(prefix).build())
.setOnlinePlayersCount(i)
.build();
handleProxyHeartBeat(heartbeat);
}
}
private void makeFakeProxiesShuttingDown() {
// fake 12 proxies
String fakePrefix = "fake-shutdown-proxy-";
for (int i = 0; i < 2; i++) {
String prefix = fakePrefix + i;
Heartbeat heartbeat =
Heartbeat.newBuilder()
.setSender(ProxyInfo.newBuilder().setProxyId(prefix).build())
.setOnlinePlayersCount(i)
.build();
Thread.ofVirtual()
.start(
() -> {
int li = 0;
int liMax = 60 + ThreadLocalRandom.current().nextInt(20);
while (!shutdown.get() && li <= liMax) {
handleProxyHeartBeat(heartbeat);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
li++;
}
handleProxyDeath(
Death.newBuilder()
.setReason(DeathReason.SHUTDOWN)
.setSender(heartbeat.getSender())
.build());
});
}
}
@Override
public void init() {
makeFakeProxiesRunning();
makeFakeProxiesDisappearing();
makeFakeProxiesShuttingDown();
}
@Override
public void close() {
shutdown.set(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
}

View File

@@ -41,7 +41,7 @@ dependencyResolutionManagement {
// main project stuff
sequenceOf("api", "core", "velocity").forEach { configureRootProjects(it) }
// core data implementations
sequenceOf("redisson").forEach { configureCoreSubProject(it) }
sequenceOf("redisson", "standalone").forEach { configureCoreSubProject(it) }
// api
sequenceOf("velocity").forEach { configureAPISubProject(it) }

View File

@@ -8,6 +8,8 @@ plugins {
dependencies {
implementation(project(":valiobungee-velocity-api"))
implementation(project(":valiobungee-core"))
implementation(project(":valiobungee-core-standalone"))
implementation(project(":valiobungee-core-redisson"))
compileOnly(libs.platform.velocity)
annotationProcessor(libs.platform.velocity)
}

View File

@@ -16,25 +16,15 @@
* You should have received a copy of the GNU General Public License
* along with ValioBungee. If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
*/
package net.limework.valiobungee.velocity.api;
package net.limework.valiobungee.velocity;
import net.limework.valiobungee.core.ProxyNetworkManager;
import net.limework.valiobungee.core.ValioBungeePlatform;
public class TestProxyNetworkManager extends ProxyNetworkManager {
public TestProxyNetworkManager(ValioBungeePlatform platform) {
super(platform);
public class PlayerManagerListener {
private final ValioBungeePlatform platform;
public PlayerManagerListener(ValioBungeePlatform platform) {
this.platform = platform;
}
@Override
protected void publishDeathPayload() {}
@Override
protected void publishHeartbeatPayload() {}
@Override
public void init() {}
@Override
public void close() {}
}

View File

@@ -26,6 +26,7 @@ import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
@@ -33,11 +34,9 @@ import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import net.limework.valiobungee.api.entity.NetworkPlayer;
import net.limework.valiobungee.api.entity.NetworkProxy;
import net.limework.valiobungee.core.ConstantVariables;
import net.limework.valiobungee.core.ProxyNetworkManager;
import net.limework.valiobungee.core.ValioBungeePlatform;
import net.limework.valiobungee.core.*;
import net.limework.valiobungee.core.util.logging.LogProviderFactory;
import net.limework.valiobungee.velocity.api.TestProxyNetworkManager;
import net.limework.valiobungee.velocity.api.VelocityValioBungeeAPI;
import net.limework.valiobungee.velocity.api.entities.ImplVelocityNetworkPlayer;
import net.limework.valiobungee.velocity.api.entities.ImplVelocityNetworkProxy;
import org.slf4j.Logger;
@@ -48,12 +47,13 @@ import org.slf4j.Logger;
version = ConstantVariables.VERSION,
url = "https://github.com/ProxioDev/ValioBungee",
authors = {"limework", "ProxioDev"})
public class VelocityValioBungeePlugin implements ValioBungeePlatform {
public class VelocityValioBungeePlugin implements ValioBungeePlatform, VelocityValioBungeeAPI {
private final ProxyServer server;
private final Logger logger;
private final Path dataFolder;
private final ProxyNetworkManager proxyNetworkManager;
private final PlayerManager playerManager;
@Inject
public VelocityValioBungeePlugin(
@@ -63,7 +63,8 @@ public class VelocityValioBungeePlugin implements ValioBungeePlatform {
this.dataFolder = dataDirectory;
// init logging
LogProviderFactory.register(logger);
this.proxyNetworkManager = new TestProxyNetworkManager(this);
this.proxyNetworkManager = new StandaloneProxyNetworkManager(this);
this.playerManager = new StandalonePlayerManager(this);
}
@Subscribe(priority = Short.MAX_VALUE) // this really important so MAKE IT MAX
@@ -72,10 +73,19 @@ public class VelocityValioBungeePlugin implements ValioBungeePlatform {
"initializing ValioBungee for {} platform, version {}",
platformProxyVendor(),
ConstantVariables.VERSION);
this.proxyNetworkManager.init(); // init the heart beat system
this.server.getEventManager().register(this, new PlayerManagerListener(this));
this.server
.getScheduler()
.buildTask(this, this.playerManager::correctionTask)
.repeat(Duration.ofMinutes(30))
.schedule(); // the correction task
}
@Subscribe(priority = Short.MIN_VALUE) // this really import so Make it AT LOWEST
public void onProxyShutdownEvent(ProxyShutdownEvent event) {}
public void onProxyShutdownEvent(ProxyShutdownEvent event) {
this.proxyNetworkManager.close(); // shutdown the heartbeat system
}
@Override
public int localOnlinePlayers() {
@@ -92,6 +102,11 @@ public class VelocityValioBungeePlugin implements ValioBungeePlatform {
return this.proxyNetworkManager;
}
@Override
public PlayerManager playerManager() {
return this.playerManager;
}
@Override
public NetworkProxy proxyPlatformCreator(String id) {
return new ImplVelocityNetworkProxy(this, id);
@@ -112,6 +127,7 @@ public class VelocityValioBungeePlugin implements ValioBungeePlatform {
@Override
public Optional<NetworkPlayer> getNetworkPlayer(UUID uuid) {
logger.warn("not implemented api call returned as Optional empty");
server.getAllPlayers();
return Optional.empty();
}
@@ -132,7 +148,6 @@ public class VelocityValioBungeePlugin implements ValioBungeePlatform {
@Override
public Set<NetworkPlayer> getLocalProxyPlayers() {
NetworkProxy proxy = getLocalProxy();
return this.server.getAllPlayers().stream()
.map(p -> new ImplVelocityNetworkPlayer(this, p.getUniqueId(), getLocalProxy(), p))
.collect(Collectors.toSet());

View File

@@ -23,10 +23,9 @@ import java.util.Optional;
import java.util.UUID;
import net.limework.valiobungee.api.entity.NetworkProxy;
import net.limework.valiobungee.core.ValioBungeePlatform;
import net.limework.valiobungee.core.api.entities.AbstractUUIDNetworkPlayer;
import net.limework.valiobungee.core.util.logging.LogProviderFactory;
import net.limework.valiobungee.core.api.entities.AbstractNetworkPlayer;
public class ImplVelocityNetworkPlayer extends AbstractUUIDNetworkPlayer
public class ImplVelocityNetworkPlayer extends AbstractNetworkPlayer
implements VelocityNetworkPlayer {
private final Player handle;
@@ -43,7 +42,7 @@ public class ImplVelocityNetworkPlayer extends AbstractUUIDNetworkPlayer
@Override
public boolean isLocal() {
return handle != null;
return handle != null && handle.isActive();
}
@Override
@@ -51,7 +50,6 @@ public class ImplVelocityNetworkPlayer extends AbstractUUIDNetworkPlayer
if (isLocal()) {
return handle.isActive();
}
LogProviderFactory.get().warn("NOT IMPLEMENTED IS ONLINE for Velocity network player");
return false;
return platform.playerManager().isOnline(uuid);
}
}