Archiviert
13
0

Try to fix a null pointer

Addresses #665
Dieser Commit ist enthalten in:
Dan Mulloy 2019-08-07 11:34:59 -04:00
Ursprung 9dfffe5366
Commit 8c20d19339
3 geänderte Dateien mit 23 neuen und 18 gelöschten Zeilen

Datei anzeigen

@ -318,12 +318,12 @@ public class AsyncFilterManager implements AsynchronousManager {
// The player is only be null when they're logged out, // The player is only be null when they're logged out,
// so this should be a pretty safe check // so this should be a pretty safe check
Player player = newEvent.getPlayer(); Player player = syncPacket.getPlayer();
if (player != null) { if (player != null) {
// Start the process // Start the process
getSendingQueue(syncPacket).enqueue(newEvent); getSendingQueue(syncPacket).enqueue(newEvent);
// We know this is occuring on the main thread, so pass TRUE // We know this is occurring on the main thread, so pass TRUE
getProcessingQueue(syncPacket).enqueue(newEvent, true); getProcessingQueue(syncPacket).enqueue(newEvent, true);
} }
} }

Datei anzeigen

@ -3,6 +3,7 @@ package com.comphenix.protocol.concurrency;
import com.comphenix.protocol.utility.SafeCacheBuilder; import com.comphenix.protocol.utility.SafeCacheBuilder;
import com.comphenix.protocol.utility.Util; import com.comphenix.protocol.utility.Util;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.RemovalListener; import com.google.common.cache.RemovalListener;
import com.google.common.collect.AbstractIterator; import com.google.common.collect.AbstractIterator;
@ -188,6 +189,8 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
* @return The key. * @return The key.
*/ */
private Object cachePlayerKey(Player player) { private Object cachePlayerKey(Player player) {
Preconditions.checkNotNull(player, "player cannot be null");
Object key = keyMethod.apply(player); Object key = keyMethod.apply(player);
keyLookup.put(key, player); keyLookup.put(key, player);

Datei anzeigen

@ -50,7 +50,7 @@ public class PacketEvent extends EventObject implements Cancellable {
"Plugin %s changed packet type from %s to %s in packet listener. This is confusing for other plugins! (Not an error, though!)"); "Plugin %s changed packet type from %s to %s in packet listener. This is confusing for other plugins! (Not an error, though!)");
private static final SetMultimap<PacketType, PacketType> CHANGE_WARNINGS = private static final SetMultimap<PacketType, PacketType> CHANGE_WARNINGS =
Multimaps.synchronizedSetMultimap(HashMultimap.<PacketType, PacketType>create()); Multimaps.synchronizedSetMultimap(HashMultimap.create());
/** /**
* Automatically generated by Eclipse. * Automatically generated by Eclipse.
@ -58,8 +58,7 @@ public class PacketEvent extends EventObject implements Cancellable {
private static final long serialVersionUID = -5360289379097430620L; private static final long serialVersionUID = -5360289379097430620L;
private transient WeakReference<Player> playerReference; private transient WeakReference<Player> playerReference;
private transient Player offlinePlayer;
private PacketContainer packet; private PacketContainer packet;
private boolean serverPacket; private boolean serverPacket;
private boolean cancel; private boolean cancel;
@ -90,7 +89,7 @@ public class PacketEvent extends EventObject implements Cancellable {
private PacketEvent(Object source, PacketContainer packet, NetworkMarker marker, Player player, boolean serverPacket, boolean filtered) { private PacketEvent(Object source, PacketContainer packet, NetworkMarker marker, Player player, boolean serverPacket, boolean filtered) {
super(source); super(source);
this.packet = packet; this.packet = packet;
this.playerReference = new WeakReference<Player>(player); this.playerReference = new WeakReference<>(player);
this.networkMarker = marker; this.networkMarker = marker;
this.serverPacket = serverPacket; this.serverPacket = serverPacket;
this.filtered = filtered; this.filtered = filtered;
@ -99,7 +98,7 @@ public class PacketEvent extends EventObject implements Cancellable {
private PacketEvent(PacketEvent origial, AsyncMarker asyncMarker) { private PacketEvent(PacketEvent origial, AsyncMarker asyncMarker) {
super(origial.source); super(origial.source);
this.packet = origial.packet; this.packet = origial.packet;
this.playerReference = origial.playerReference; this.playerReference = origial.getPlayerReference();
this.cancel = origial.cancel; this.cancel = origial.cancel;
this.serverPacket = origial.serverPacket; this.serverPacket = origial.serverPacket;
this.filtered = origial.filtered; this.filtered = origial.filtered;
@ -317,24 +316,26 @@ public class PacketEvent extends EventObject implements Cancellable {
this.cancel = cancel; this.cancel = cancel;
} }
/** private WeakReference<Player> getPlayerReference() {
* Retrieves the player that has sent the packet or is recieving it.
* @return The player associated with this event.
*/
public Player getPlayer() {
Player player = playerReference.get(); Player player = playerReference.get();
// Check if the player has been updated so we can do more stuff
if (player instanceof TemporaryPlayer) { if (player instanceof TemporaryPlayer) {
Player updated = player.getPlayer(); Player updated = player.getPlayer();
if (updated != null && !(updated instanceof TemporaryPlayer)) { if (updated != null && !(updated instanceof TemporaryPlayer)) {
playerReference.clear(); playerReference.clear();
playerReference = new WeakReference<>(updated); playerReference = new WeakReference<>(updated);
return updated;
} }
} }
return player; return playerReference;
}
/**
* Retrieves the player that has sent the packet or is receiving it.
* @return The player associated with this event.
*/
public Player getPlayer() {
return getPlayerReference().get();
} }
/** /**
@ -467,7 +468,8 @@ public class PacketEvent extends EventObject implements Cancellable {
output.defaultWriteObject(); output.defaultWriteObject();
// Write the name of the player (or NULL if it's not set) // Write the name of the player (or NULL if it's not set)
output.writeObject(playerReference.get() != null ? new SerializedOfflinePlayer(playerReference.get()) : null); Player player = getPlayer();
output.writeObject(player != null ? new SerializedOfflinePlayer(player) : null);
} }
private void readObject(ObjectInputStream input) throws ClassNotFoundException, IOException { private void readObject(ObjectInputStream input) throws ClassNotFoundException, IOException {
@ -479,8 +481,8 @@ public class PacketEvent extends EventObject implements Cancellable {
// Better than nothing // Better than nothing
if (serialized != null) { if (serialized != null) {
// Store it, to prevent weak reference from cleaning up the reference // Store it, to prevent weak reference from cleaning up the reference
offlinePlayer = serialized.getPlayer(); Player offlinePlayer = serialized.getPlayer();
playerReference = new WeakReference<Player>(offlinePlayer); playerReference = new WeakReference<>(offlinePlayer);
} }
} }