Fix High Memory Usage

This commit is contained in:
RayzsYT committed 2024-06-14 13:46:23 +02:00
1 parent 9c92421b1f
commit 66d7f7cbb5
4 files changed
+9 -54

No files matched your search

@@ -10,15 +10,11 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import net.md_5.bungee.protocol.ProtocolConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class BungeeServerBrand implements ServerBrand {
private static final List<ProxiedPlayer> MODIFIED_BRAND_PLAYERS = new ArrayList<>();
private static final ProxyServer SERVER = BungeeLoader.getPlugin().getProxy();
private static ScheduledTask TASK;
private static String BRAND = Storage.ConfigSections.Settings.CUSTOM_BRAND.BRANDS.getLines().get(0);
@@ -27,18 +23,13 @@ public class BungeeServerBrand implements ServerBrand {
@Override
public void initializeTask() {
if(TASK != null) {
TASK.cancel();
MODIFIED_BRAND_PLAYERS.clear();
}
if(TASK != null) TASK.cancel();
if(!Storage.ConfigSections.Settings.CUSTOM_BRAND.ENABLED) return;
if(Storage.ConfigSections.Settings.CUSTOM_BRAND.REPEAT_DELAY == -1) {
BRAND = MessageTranslator.replaceMessage(Storage.ConfigSections.Settings.CUSTOM_BRAND.BRANDS.getLines().get(0)) + "§r";
TASK = SERVER.getScheduler().schedule(BungeeLoader.getPlugin(), () -> {
SERVER.getPlayers().stream().filter(player -> !isModified(player)).forEach(this::send);
}, 1, 150, TimeUnit.MILLISECONDS);
SERVER.getPlayers().forEach(this::send);
} else {
AtomicInteger animationState = new AtomicInteger(0);
TASK = SERVER.getScheduler().schedule(BungeeLoader.getPlugin(), () -> {
@@ -66,16 +57,5 @@ public class BungeeServerBrand implements ServerBrand {
PacketUtils.BrandManipulate serverBrand = new PacketUtils.BrandManipulate(customBrand);
String brand = player.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_13 ? "minecraft:brand" : "MC|Brand";
player.sendData(brand, serverBrand.getBytes());
if(!MODIFIED_BRAND_PLAYERS.contains(player)) MODIFIED_BRAND_PLAYERS.add(player);
}
private boolean isModified(ProxiedPlayer player) {
if(!Storage.ConfigSections.Settings.CUSTOM_BRAND.ENABLED || Storage.ConfigSections.Settings.CUSTOM_BRAND.REPEAT_DELAY != -1) return false;
return MODIFIED_BRAND_PLAYERS.contains(player);
}
public static void removeFromModified(ProxiedPlayer player) {
MODIFIED_BRAND_PLAYERS.remove(player);
}
}
@@ -22,7 +22,6 @@ import java.util.concurrent.atomic.AtomicInteger;
public class VelocityServerBrand implements ServerBrand {
private static final List<Player> MODIFIED_BRAND_PLAYERS = new ArrayList<>();
private static final ProxyServer SERVER = VelocityLoader.getServer();
private static Class<?> pluginMessagePacketClass, minecraftConnectionClass, connectedPlayerConnectionClass;
private static Method connectionMethod;
@@ -31,10 +30,7 @@ public class VelocityServerBrand implements ServerBrand {
@Override
public void initializeTask() {
if(TASK != null) {
TASK.cancel();
MODIFIED_BRAND_PLAYERS.clear();
}
if(TASK != null) TASK.cancel();
if(!Storage.ConfigSections.Settings.CUSTOM_BRAND.ENABLED) return;
@@ -52,10 +48,7 @@ public class VelocityServerBrand implements ServerBrand {
if(Storage.ConfigSections.Settings.CUSTOM_BRAND.REPEAT_DELAY == -1) {
BRAND = MessageTranslator.replaceMessage(Storage.ConfigSections.Settings.CUSTOM_BRAND.BRANDS.getLines().get(0)) + "§r";
TASK = SERVER.getScheduler().buildTask(VelocityLoader.getInstance(), () -> {
SERVER.getAllPlayers().stream().filter(player -> !isModified(player)).forEach(this::send);
}).repeat(150, TimeUnit.MILLISECONDS).schedule();
SERVER.getAllPlayers().forEach(this::send);
} else {
AtomicInteger animationState = new AtomicInteger(0);
TASK = SERVER.getScheduler().buildTask(VelocityLoader.getInstance(), () -> {
@@ -89,19 +82,8 @@ public class VelocityServerBrand implements ServerBrand {
String brand = player.getProtocolVersion().getProtocol() >= ProtocolConstants.MINECRAFT_1_13 ? "minecraft:brand" : "MC|Brand";
Object pluginMessagePacket = pluginMessagePacketClass.getConstructor(String.class, ByteBuf.class).newInstance(brand, serverBrand.getByteBuf());
Reflection.getMethodsByParameterAndName(minecraftConnectionObj, "write", Object.class).get(0).invoke(minecraftConnectionObj, pluginMessagePacket);
if(!MODIFIED_BRAND_PLAYERS.contains(player)) MODIFIED_BRAND_PLAYERS.add(player);
} catch (Exception exception) {
exception.printStackTrace();
}
}
private boolean isModified(Player player) {
if(!Storage.ConfigSections.Settings.CUSTOM_BRAND.ENABLED || Storage.ConfigSections.Settings.CUSTOM_BRAND.REPEAT_DELAY != -1) return false;
return MODIFIED_BRAND_PLAYERS.contains(player);
}
public static void removeFromModified(Player player) {
MODIFIED_BRAND_PLAYERS.remove(player);
}
}
@@ -1,5 +1,6 @@
package de.rayzs.pat.plugin.listeners.bungee;
import de.rayzs.pat.api.brand.CustomServerBrand;
import de.rayzs.pat.api.brand.impl.BungeeServerBrand;
import de.rayzs.pat.api.netty.proxy.BungeePacketAnalyzer;
import de.rayzs.pat.api.storage.Storage;
@@ -38,7 +39,7 @@ public class BungeePlayerConnectionListener implements Listener {
BungeePacketAnalyzer.inject(player);
if(Storage.ConfigSections.Settings.CUSTOM_BRAND.REPEAT_DELAY != -1) return;
BungeeServerBrand.removeFromModified(player);
CustomServerBrand.sendBrandToPlayer(player);
}
@EventHandler (priority = EventPriority.LOWEST)
@@ -47,6 +48,6 @@ public class BungeePlayerConnectionListener implements Listener {
PermissionUtil.resetPermissions(player.getUniqueId());
BungeePacketAnalyzer.uninject(player);
if(Storage.ConfigSections.Settings.CUSTOM_BRAND.REPEAT_DELAY != -1) return;
BungeeServerBrand.removeFromModified(player);
CustomServerBrand.sendBrandToPlayer(player);
}
}
@@ -6,19 +6,12 @@ import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.event.player.ServerPreConnectEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.packet.TabCompleteResponsePacket;
import de.rayzs.pat.api.brand.impl.VelocityServerBrand;
import de.rayzs.pat.api.brand.CustomServerBrand;
import de.rayzs.pat.api.netty.proxy.VelocityPacketAnalyzer;
import de.rayzs.pat.api.storage.Storage;
import de.rayzs.pat.plugin.VelocityLoader;
import de.rayzs.pat.utils.message.MessageTranslator;
import de.rayzs.pat.utils.permission.PermissionUtil;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import java.util.concurrent.TimeUnit;
public class VelocityConnectionListener {
@@ -53,7 +46,7 @@ public class VelocityConnectionListener {
VelocityPacketAnalyzer.inject(player);
if(Storage.ConfigSections.Settings.CUSTOM_BRAND.REPEAT_DELAY != -1) return;
VelocityServerBrand.removeFromModified(player);
CustomServerBrand.sendBrandToPlayer(player);
}
@Subscribe
@@ -62,6 +55,5 @@ public class VelocityConnectionListener {
PermissionUtil.resetPermissions(player.getUniqueId());
VelocityPacketAnalyzer.uninject(player);
if(Storage.ConfigSections.Settings.CUSTOM_BRAND.REPEAT_DELAY != -1) return;
VelocityServerBrand.removeFromModified(player);
}
}