mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2025-01-15 03:51:20 +01:00
Simplify FlyingFrequency by using the cow map.
This commit is contained in:
parent
923f08c308
commit
7169788554
@ -1,7 +1,5 @@
|
||||
package fr.neatmonster.nocheatplus.net.protocollib;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
@ -9,13 +7,12 @@ 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
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user