Skip to content

Commit

Permalink
improve performance and add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 3, 2024
1 parent 4d2a290 commit c05c113
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSetSlot;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -25,7 +22,7 @@ public class InventoryLag extends PacketModule implements Listener {

private final Cache<UUID, PlayerData> playerDataCache;
private final long maxByteSize, inventoryOpenCooldownMillis;
private final boolean closeInventory;
private final boolean closeInventory, log;

public InventoryLag() {
super("patches.inventory-lag", PacketListenerPriority.HIGHEST);
Expand All @@ -35,6 +32,7 @@ public InventoryLag() {
"If a player exceeds the limit, they will be put on a cooldown,\n" +
"during which they will be very limited in terms of ItemStack or\n" +
"Inventory interactions.");
this.log = config.getBoolean(configPath + ".log", false);
this.playerDataCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(Math.max(1L,
config.getLong(configPath + ".timeframe-millis", 20000,
"The time in millis in which to check if the player exceeded the limit.\n" +
Expand Down Expand Up @@ -86,22 +84,20 @@ public void onPacketSend(PacketSendEvent event) {
return;
}

WrapperPlayServerSetSlot serverSetSlot = new WrapperPlayServerSetSlot(event);
ItemStack itemStack = serverSetSlot.getItem();
if (itemStack == null || itemStack.isEmpty()) return;
NBTCompound nbt = itemStack.getNBT();
if (nbt == null || nbt.isEmpty()) return;

if (data.receivedSetSlotBytes.addAndGet(nbt.toString().getBytes(StandardCharsets.UTF_8).length) <= maxByteSize) {
if (data.receivedSetSlotBytes.addAndGet(ByteBufHelper.readableBytes(event.getByteBuf())) <= maxByteSize) {
return;
}

data.nextAllowedInvOpen.set(System.currentTimeMillis() + inventoryOpenCooldownMillis);

if (closeInventory) {
Player bukkitPlayer = (Player) event.getPlayer();
if (bukkitPlayer != null)
bukkitPlayer.getScheduler().execute(plugin, bukkitPlayer::closeInventory, null, 1L);
if (log) {
info("Player " + event.getUser().getName() + " is now on cooldown for requesting more data " +
"from the server than the set limit.");
}

if (closeInventory && event.getPlayer() != null) {
Player player = (Player) event.getPlayer();
player.getScheduler().execute(plugin, player::closeInventory, null, 1L);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSetSlot;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -25,7 +22,7 @@ public class InventoryLag extends PacketModule implements Listener {

private final Cache<UUID, PlayerData> playerDataCache;
private final long maxByteSize, inventoryOpenCooldownMillis;
private final boolean closeInventory;
private final boolean closeInventory, log;

public InventoryLag() {
super("patches.inventory-lag", PacketListenerPriority.HIGHEST);
Expand All @@ -35,6 +32,7 @@ public InventoryLag() {
"If a player exceeds the limit, they will be put on a cooldown,\n" +
"during which they will be very limited in terms of ItemStack or\n" +
"Inventory interactions.");
this.log = config.getBoolean(configPath + ".log", false);
this.playerDataCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(Math.max(1L,
config.getLong(configPath + ".timeframe-millis", 20000,
"The time in millis in which to check if the player exceeded the limit.\n" +
Expand Down Expand Up @@ -87,22 +85,19 @@ public void onPacketSend(PacketSendEvent event) {
return;
}

WrapperPlayServerSetSlot serverSetSlot = new WrapperPlayServerSetSlot(event);
ItemStack itemStack = serverSetSlot.getItem();
if (itemStack == null || itemStack.isEmpty()) return;
NBTCompound nbt = itemStack.getNBT();
if (nbt == null || nbt.isEmpty()) return;

if (data.receivedSetSlotBytes.addAndGet(nbt.toString().getBytes(StandardCharsets.UTF_8).length) <= maxByteSize) {
if (data.receivedSetSlotBytes.addAndGet(ByteBufHelper.readableBytes(event.getByteBuf())) <= maxByteSize) {
return;
}

data.nextAllowedInvOpen.set(System.currentTimeMillis() + inventoryOpenCooldownMillis);

if (closeInventory) {
Player bukkitPlayer = (Player) event.getPlayer();
if (bukkitPlayer != null)
plugin.getServer().getScheduler().runTask(plugin, bukkitPlayer::closeInventory);
if (log) {
info("Player " + event.getUser().getName() + " is now on cooldown for requesting more data " +
"from the server than the set limit.");
}

if (closeInventory && event.getPlayer() != null) {
plugin.getServer().getScheduler().runTask(plugin, ((Player) event.getPlayer())::closeInventory);
}
}
}
Expand Down

0 comments on commit c05c113

Please sign in to comment.