From b5984259795f2510ccf4fdac1ea2934ca49900bd Mon Sep 17 00:00:00 2001 From: Mike <76-Mike@users.noreply.git.krews.org> Date: Fri, 12 Jun 2020 22:10:05 +0200 Subject: [PATCH] Fix flood race conditions --- .../com/eu/habbo/habbohotel/rooms/Room.java | 54 ++++++++++--------- .../eu/habbo/habbohotel/users/HabboStats.java | 2 +- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java index e0dee0a1..27f8c25e 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java @@ -204,12 +204,13 @@ public class Room implements Comparable, ISerialize, Runnable { private int idleCycles; private volatile int unitCounter; private volatile int rollerSpeed; - private int muteTime = Emulator.getConfig().getInt("hotel.flood.mute.time"); + private final int muteTime = Emulator.getConfig().getInt("hotel.flood.mute.time", 30); private long rollerCycle = System.currentTimeMillis(); private volatile int lastTimerReset = Emulator.getIntUnixTimestamp(); private volatile boolean muted; private RoomSpecialTypes roomSpecialTypes; private TraxManager traxManager; + private boolean cycleOdd; private long cycleTimestamp; public Room(ResultSet set) throws SQLException { @@ -1118,6 +1119,7 @@ public class Room implements Comparable, ISerialize, Runnable { } private void cycle() { + this.cycleOdd = !this.cycleOdd; this.cycleTimestamp = System.currentTimeMillis(); final boolean[] foundRightHolder = {false}; @@ -1199,28 +1201,10 @@ public class Room implements Comparable, ISerialize, Runnable { habbo.getHabboStats().mutedBubbleTracker = false; this.sendComposer(new RoomUserIgnoredComposer(habbo, RoomUserIgnoredComposer.UNIGNORED).compose()); } - if (!habbo.hasPermission(Permission.ACC_CHAT_NO_FLOOD) && habbo.getHabboStats().chatCounter > 0) { - //if (habbo.getRoomUnit().talkTimeOut == 0 || currentTimestamp - habbo.getRoomUnit().talkTimeOut < 0) - { - habbo.getHabboStats().chatCounter--; - if (habbo.getHabboStats().chatCounter > 3) { - final boolean floodRights = Emulator.getConfig().getBoolean("flood.with.rights"); - final boolean hasRights = this.hasRights(habbo); - - if (floodRights || !hasRights) { - if (this.chatProtection == 0) { - this.floodMuteHabbo(habbo, muteTime); - } else if (this.chatProtection == 1 && habbo.getHabboStats().chatCounter > 4) { - this.floodMuteHabbo(habbo, muteTime); - } else if (this.chatProtection == 2 && habbo.getHabboStats().chatCounter > 5) { - this.floodMuteHabbo(habbo, muteTime); - } - } - } - } - } else { - habbo.getHabboStats().chatCounter = 0; + // Substract 1 from the chatCounter every odd cycle, which is every (500ms * 2). + if (this.cycleOdd && habbo.getHabboStats().chatCounter.get() > 0) { + habbo.getHabboStats().chatCounter.decrementAndGet(); } if (this.cycleRoomUnit(habbo.getRoomUnit(), RoomUnitType.USER)) { @@ -3019,7 +3003,7 @@ public class Room implements Comparable, ISerialize, Runnable { public void floodMuteHabbo(Habbo habbo, int timeOut) { habbo.getHabboStats().mutedCount++; timeOut += (timeOut * (int) Math.ceil(Math.pow(habbo.getHabboStats().mutedCount, 2))); - habbo.getHabboStats().chatCounter = 0; + habbo.getHabboStats().chatCounter.set(0); habbo.mute(timeOut, true); } @@ -3050,7 +3034,7 @@ public class Room implements Comparable, ISerialize, Runnable { } habbo.getHabboStats().lastChat = millis; if (roomChatMessage != null && roomChatMessage.getMessage().equalsIgnoreCase("i am a pirate")) { - habbo.getHabboStats().chatCounter += 2; + habbo.getHabboStats().chatCounter.addAndGet(1); Emulator.getThreading().run(new YouAreAPirate(habbo, this)); return; } @@ -3109,7 +3093,27 @@ public class Room implements Comparable, ISerialize, Runnable { } } - habbo.getHabboStats().chatCounter += 2; + if (!habbo.hasPermission(Permission.ACC_CHAT_NO_FLOOD)) { + final int chatCounter = habbo.getHabboStats().chatCounter.addAndGet(1); + + if (chatCounter > 3) { + final boolean floodRights = Emulator.getConfig().getBoolean("flood.with.rights"); + final boolean hasRights = this.hasRights(habbo); + + if (floodRights || !hasRights) { + if (this.chatProtection == 0) { + this.floodMuteHabbo(habbo, muteTime); + return; + } else if (this.chatProtection == 1 && chatCounter > 4) { + this.floodMuteHabbo(habbo, muteTime); + return; + } else if (this.chatProtection == 2 && chatCounter > 5) { + this.floodMuteHabbo(habbo, muteTime); + return; + } + } + } + } ServerMessage prefixMessage = roomChatMessage.getHabbo().getHabboInfo().getRank().hasPrefix() ? new RoomUserNameChangedComposer(habbo, true).compose() : null; ServerMessage clearPrefixMessage = prefixMessage != null ? new RoomUserNameChangedComposer(habbo).compose() : null; diff --git a/src/main/java/com/eu/habbo/habbohotel/users/HabboStats.java b/src/main/java/com/eu/habbo/habbohotel/users/HabboStats.java index d40f1682..d7b03956 100644 --- a/src/main/java/com/eu/habbo/habbohotel/users/HabboStats.java +++ b/src/main/java/com/eu/habbo/habbohotel/users/HabboStats.java @@ -71,7 +71,7 @@ public class HabboStats implements Runnable { public int helpersLevel; public boolean perkTrade; public long roomEnterTimestamp; - public int chatCounter; + public AtomicInteger chatCounter = new AtomicInteger(0); public long lastChat; public long lastUsersSearched; public boolean nux;