Skip to content

Commit

Permalink
Simplify FlyingFrequency by using the cow map.
Browse files Browse the repository at this point in the history
  • Loading branch information
asofold committed Jul 16, 2014
1 parent 923f08c commit 7169788
Showing 1 changed file with 10 additions and 25 deletions.
@@ -1,21 +1,18 @@
package fr.neatmonster.nocheatplus.net.protocollib;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Protocol;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;

import fr.neatmonster.nocheatplus.components.JoinLeaveListener;
import fr.neatmonster.nocheatplus.utilities.ActionFrequency;
import fr.neatmonster.nocheatplus.utilities.ds.LinkedHashMapCOW;

/**
* Prevent extremely fast ticking by just sending packets that don't do anything
Expand All @@ -30,44 +27,32 @@ public class FlyingFrequency extends PacketAdapter implements Listener, JoinLeav
// TODO: Optimized options (receive only, other?).
// TODO: Async version ?

private Map<String, ActionFrequency> freqMap = new LinkedHashMap<String, ActionFrequency>();
private final Map<String, ActionFrequency> freqMap = new LinkedHashMapCOW<String, ActionFrequency>();

public FlyingFrequency(Plugin plugin) {
// PacketPlayInFlying[3, legacy: 10]
super(plugin, PacketType.Play.Client.FLYING); // TODO: How does POS and POS_LOOK relate/translate?
}

private synchronized ActionFrequency addName(String name) {
Map<String, ActionFrequency> freqMap = new HashMap<String, ActionFrequency>(this.freqMap);
ActionFrequency freq = new ActionFrequency(5, 1000);
freqMap.put(name, freq);
this.freqMap = freqMap;
return freq;
}

private void removeName(String name) {
Map<String, ActionFrequency> freq = new HashMap<String, ActionFrequency>(this.freqMap);
freq.remove(name);
this.freqMap = freq;
}

@Override
public void playerJoins(Player player) {
addName(player.getName()); // Could spare that one.
getFreq(player.getName());
}

@Override
public void playerLeaves(Player player) {
removeName(player.getName());
freqMap.remove(player.getName());
}

private ActionFrequency getFreq(final String name) {
final ActionFrequency freq = this.freqMap.get(name);
if (freq == null) {
return addName(name);
} else {
if (freq != null) {
return freq;
}
} else {
final ActionFrequency newFreq = new ActionFrequency(5, 1000);
this.freqMap.put(name, newFreq);
return newFreq;
}
}

@Override
Expand Down

0 comments on commit 7169788

Please sign in to comment.