mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2025-01-30 03:11:22 +01:00
Optimize for speed.
This commit is contained in:
parent
c0300c09a8
commit
1e455f77e2
@ -24,27 +24,31 @@ public class PlayerMessageSender extends OnDemandTickListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Queued entries, also used as lock. */
|
/** Queued entries, also used as lock. */
|
||||||
private final List<MessageEntry> messageEntries = new LinkedList<MessageEntry>();
|
private List<MessageEntry> messageEntries = new LinkedList<MessageEntry>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean delegateTick(int tick, long timeLast) {
|
public boolean delegateTick(int tick, long timeLast) {
|
||||||
// Copy entries.
|
// Copy entries.
|
||||||
final MessageEntry[] entries;
|
final List<MessageEntry> entries;
|
||||||
synchronized (messageEntries) {
|
synchronized (this) {
|
||||||
entries = new MessageEntry[messageEntries.size()];
|
if (messageEntries.isEmpty()){
|
||||||
messageEntries.toArray(entries);
|
// Force unregister.
|
||||||
messageEntries.clear();
|
unRegister(true);
|
||||||
|
// Always continue here to never use external setRegistered.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
entries = messageEntries;
|
||||||
|
messageEntries = new LinkedList<PlayerMessageSender.MessageEntry>();
|
||||||
}
|
}
|
||||||
// Do messaging.
|
// Do messaging.
|
||||||
for (int i = 0; i < entries.length; i++){
|
for (final MessageEntry entry : entries){
|
||||||
final MessageEntry entry = entries[i];
|
|
||||||
final Player player = DataManager.getPlayerExact(entry.playerName);
|
final Player player = DataManager.getPlayerExact(entry.playerName);
|
||||||
if (player != null && player.isOnline()){
|
if (player != null && player.isOnline()){
|
||||||
player.sendMessage(entry.message);
|
player.sendMessage(entry.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Unregister if no further entries are there.
|
// Unregister if no further entries are there.
|
||||||
synchronized (messageEntries) {
|
synchronized (this) {
|
||||||
if (messageEntries.isEmpty()){
|
if (messageEntries.isEmpty()){
|
||||||
// Force unregister.
|
// Force unregister.
|
||||||
unRegister(true);
|
unRegister(true);
|
||||||
@ -56,7 +60,7 @@ public class PlayerMessageSender extends OnDemandTickListener {
|
|||||||
|
|
||||||
public void sendMessageThreadSafe(final String playerName, final String message){
|
public void sendMessageThreadSafe(final String playerName, final String message){
|
||||||
final MessageEntry entry = new MessageEntry(playerName.toLowerCase(), message);
|
final MessageEntry entry = new MessageEntry(playerName.toLowerCase(), message);
|
||||||
synchronized (messageEntries) {
|
synchronized (this) {
|
||||||
messageEntries.add(entry);
|
messageEntries.add(entry);
|
||||||
// Called register asynchronously, potentially.
|
// Called register asynchronously, potentially.
|
||||||
register();
|
register();
|
||||||
|
Loading…
Reference in New Issue
Block a user