Implement PPS for Sponge

This commit is contained in:
Matsv 2016-09-26 11:16:20 +02:00
parent 4840db095c
commit 9a8a6e5b16
No known key found for this signature in database
GPG Key ID: 97CEC2A2EA31350F
4 changed files with 38 additions and 36 deletions

View File

@ -137,36 +137,6 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform {
return protocolSupport;
}
public boolean handlePPS(UserConnection info) {
// Max PPS Checker
if (conf.getMaxPPS() > 0) {
if (info.getPacketsPerSecond() >= conf.getMaxPPS()) {
info.disconnect(conf.getMaxPPSKickMessage().replace("%pps", ((Long) info.getPacketsPerSecond()).intValue() + ""));
return true; // don't send current packet
}
}
// Tracking PPS Checker
if (conf.getMaxWarnings() > 0 && conf.getTrackingPeriod() > 0) {
if (info.getSecondsObserved() > conf.getTrackingPeriod()) {
// Reset
info.setWarnings(0);
info.setSecondsObserved(1);
} else {
info.setSecondsObserved(info.getSecondsObserved() + 1);
if (info.getPacketsPerSecond() >= conf.getWarningPPS()) {
info.setWarnings(info.getWarnings() + 1);
}
if (info.getWarnings() >= conf.getMaxWarnings()) {
info.disconnect(conf.getMaxWarningsKickMessage().replace("%pps", ((Long) info.getPacketsPerSecond()).intValue() + ""));
return true; // don't send current packet
}
}
}
return false;
}
@Override
public String getPlatformName() {
return "Bukkit";

View File

@ -39,7 +39,7 @@ public class ViaDecodeHandler extends ByteToMessageDecoder {
boolean second = info.incrementReceived();
// Check PPS
if (second) {
if (((ViaVersionPlugin) Via.getPlatform()).handlePPS(info))
if (info.handlePPS())
return;
}

View File

@ -7,6 +7,7 @@ import io.netty.channel.socket.SocketChannel;
import lombok.Data;
import net.md_5.bungee.api.ChatColor;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import java.util.Map;
@ -133,6 +134,37 @@ public class UserConnection {
return false;
}
public boolean handlePPS() {
ViaVersionConfig conf = Via.getConfig();
// Max PPS Checker
if (conf.getMaxPPS() > 0) {
if (getPacketsPerSecond() >= conf.getMaxPPS()) {
disconnect(conf.getMaxPPSKickMessage().replace("%pps", ((Long) getPacketsPerSecond()).intValue() + ""));
return true; // don't send current packet
}
}
// Tracking PPS Checker
if (conf.getMaxWarnings() > 0 && conf.getTrackingPeriod() > 0) {
if (getSecondsObserved() > conf.getTrackingPeriod()) {
// Reset
setWarnings(0);
setSecondsObserved(1);
} else {
setSecondsObserved(getSecondsObserved() + 1);
if (getPacketsPerSecond() >= conf.getWarningPPS()) {
setWarnings(getWarnings() + 1);
}
if (getWarnings() >= conf.getMaxWarnings()) {
disconnect(conf.getMaxWarningsKickMessage().replace("%pps", ((Long) getPacketsPerSecond()).intValue() + ""));
return true; // don't send current packet
}
}
}
return false;
}
/**
* Disconnect a connection
*

View File

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException;
@ -35,11 +36,10 @@ public class ViaDecodeHandler extends ByteToMessageDecoder {
// Increment received
boolean second = info.incrementReceived();
// Check PPS
// TODO implement pps
// if (second) {
// if (((ViaVersionPlugin) Via.getPlatform()).handlePPS(info))
// return;
// }
if (second) {
if (info.handlePPS())
return;
}
if (info.isActive()) {
// Handle ID