Archiviert
13
0

Added a custom exception. Simplified the event API.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2012-09-13 14:42:37 +02:00
Ursprung 353302fe5f
Commit 0d531ecc4b
3 geänderte Dateien mit 78 neuen und 41 gelöschten Zeilen

Datei anzeigen

@ -11,8 +11,8 @@ public class PacketEvent extends EventObject {
private static final long serialVersionUID = -5360289379097430620L; private static final long serialVersionUID = -5360289379097430620L;
private PacketContainer packet; private PacketContainer packet;
private Player sender; private Player player;
private Player reciever; private boolean serverPacket;
private boolean cancel; private boolean cancel;
/** /**
@ -23,11 +23,11 @@ public class PacketEvent extends EventObject {
super(source); super(source);
} }
private PacketEvent(Object source, PacketContainer packet, Player sender, Player reciever) { private PacketEvent(Object source, PacketContainer packet, Player player, boolean serverPacket) {
super(source); super(source);
this.packet = packet; this.packet = packet;
this.sender = sender; this.player = player;
this.reciever = reciever; this.serverPacket = serverPacket;
} }
/** /**
@ -38,7 +38,7 @@ public class PacketEvent extends EventObject {
* @return The event. * @return The event.
*/ */
public static PacketEvent fromClient(Object source, PacketContainer packet, Player client) { public static PacketEvent fromClient(Object source, PacketContainer packet, Player client) {
return new PacketEvent(source, packet, client, null); return new PacketEvent(source, packet, client, false);
} }
/** /**
@ -49,7 +49,7 @@ public class PacketEvent extends EventObject {
* @return The event. * @return The event.
*/ */
public static PacketEvent fromServer(Object source, PacketContainer packet, Player recipient) { public static PacketEvent fromServer(Object source, PacketContainer packet, Player recipient) {
return new PacketEvent(source, packet, null, recipient); return new PacketEvent(source, packet, recipient, true);
} }
/** /**
@ -93,27 +93,18 @@ public class PacketEvent extends EventObject {
} }
/** /**
* Retrieves the player that has sent the packet. * Retrieves the player that has sent the packet or is recieving it.
* @return The sender, or NULL if the server is sending the packet. * @return The player associated with this event.
*/ */
public Player getSender() { public Player getPlayer() {
return sender; return player;
} }
/**
* Retrieves the player that will recieve the packet.
* @return The reciever, or NULL if the server is recieving the packet.
*/
public Player getReciever() {
return reciever;
}
/** /**
* Whether or not this packet was created by the server. * Whether or not this packet was created by the server.
* @return TRUE if the packet was created by the server, FALSE if it was created by a client. * @return TRUE if the packet was created by the server, FALSE if it was created by a client.
*/ */
public boolean isServerPacket() { public boolean isServerPacket() {
return getReciever() != null; return serverPacket;
} }
} }

Datei anzeigen

@ -0,0 +1,34 @@
package com.comphenix.protocol.reflect;
/**
* Invoked when a field is inaccessible due to security limitations, or when it simply doesn't exist.
*
* @author Kristian
*/
public class FieldAccessException extends Exception {
/**
* Generated by Eclipse.
*/
private static final long serialVersionUID = 1911011681494034617L;
public FieldAccessException() {
super();
}
public FieldAccessException(String message, Throwable cause) {
super(message, cause);
}
public FieldAccessException(String message) {
super(message);
}
public FieldAccessException(Throwable cause) {
super(cause);
}
public static FieldAccessException fromFormat(String message, Object... params) {
return new FieldAccessException(String.format(message, params));
}
}

Datei anzeigen

@ -71,31 +71,37 @@ public class StructureModifier<TField> {
* Reads the value of a field given its index. * Reads the value of a field given its index.
* @param fieldIndex - index of the field. * @param fieldIndex - index of the field.
* @return Value of the field. * @return Value of the field.
* @throws IllegalAccessException If we're unable to read the field due to a security limitation. * @throws FieldAccessException The field doesn't exist, or it cannot be accessed under the current security contraints.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public TField read(int fieldIndex) throws IllegalAccessException { public TField read(int fieldIndex) throws FieldAccessException {
if (fieldIndex < 0 || fieldIndex >= data.size()) if (fieldIndex < 0 || fieldIndex >= data.size())
throw new IllegalArgumentException("Field index must be within 0 - count"); throw new FieldAccessException("Field index must be within 0 - count",
new IndexOutOfBoundsException("Out of bounds"));
if (target == null) if (target == null)
throw new IllegalStateException("Cannot read from a NULL target."); throw new IllegalStateException("Cannot read from a NULL target.");
Object result = FieldUtils.readField(data.get(fieldIndex), target, true); try {
Object result = FieldUtils.readField(data.get(fieldIndex), target, true);
// Use the converter, if we have it
if (converter != null) // Use the converter, if we have it
return converter.getSpecific(result); if (converter != null)
else return converter.getSpecific(result);
return (TField) result; else
return (TField) result;
} catch (IllegalAccessException e) {
throw new FieldAccessException("Cannot read field due to a security limitation.", e);
}
} }
/** /**
* Reads the value of a field if and ONLY IF it exists. * Reads the value of a field if and ONLY IF it exists.
* @param fieldIndex - index of the field. * @param fieldIndex - index of the field.
* @return Value of the field, or NULL if it doesn't exist. * @return Value of the field, or NULL if it doesn't exist.
* @throws IllegalAccessException If we're unable to read the field due to a security limitation. * @throws FieldAccessException The field cannot be accessed under the current security contraints.
*/ */
public TField readSafely(int fieldIndex) throws IllegalAccessException { public TField readSafely(int fieldIndex) throws FieldAccessException {
if (fieldIndex >= 0 && fieldIndex < data.size()) { if (fieldIndex >= 0 && fieldIndex < data.size()) {
return read(fieldIndex); return read(fieldIndex);
} else { } else {
@ -108,17 +114,23 @@ public class StructureModifier<TField> {
* @param fieldIndex - index of the field. * @param fieldIndex - index of the field.
* @param value - new value of the field. * @param value - new value of the field.
* @return This structure modifier - for chaining. * @return This structure modifier - for chaining.
* @throws IllegalAccessException If we're unable to write to the field due to a security limitation. * @throws IllegalAccessException The field doesn't exist, or it cannot be accessed under the current security contraints.
*/ */
public StructureModifier<TField> write(int fieldIndex, TField value) throws IllegalAccessException { public StructureModifier<TField> write(int fieldIndex, TField value) throws FieldAccessException {
if (fieldIndex < 0 || fieldIndex >= data.size()) if (fieldIndex < 0 || fieldIndex >= data.size())
throw new IllegalArgumentException("Field index must be within 0 - count"); throw new FieldAccessException("Field index must be within 0 - count",
new IndexOutOfBoundsException("Out of bounds"));
if (target == null) if (target == null)
throw new IllegalStateException("Cannot write to a NULL target."); throw new IllegalStateException("Cannot write to a NULL target.");
// Use the converter, if it exists // Use the converter, if it exists
Object obj = converter != null ? converter.getGeneric(value) : value; Object obj = converter != null ? converter.getGeneric(value) : value;
FieldUtils.writeField(data.get(fieldIndex), target, obj, true);
try {
FieldUtils.writeField(data.get(fieldIndex), target, obj, true);
} catch (IllegalAccessException e) {
throw new FieldAccessException("Cannot read field due to a security limitation.", e);
}
// Make this method chainable // Make this method chainable
return this; return this;
@ -129,9 +141,9 @@ public class StructureModifier<TField> {
* @param fieldIndex - index of the potential field. * @param fieldIndex - index of the potential field.
* @param value - new value of the field. * @param value - new value of the field.
* @return This structure modifer - for chaining. * @return This structure modifer - for chaining.
* @throws IllegalAccessException If we're unable to write to the field due to a security limitation. * @throws IllegalAccessException The field cannot be accessed under the current security contraints.
*/ */
public StructureModifier<TField> writeSafely(int fieldIndex, TField value) throws IllegalAccessException { public StructureModifier<TField> writeSafely(int fieldIndex, TField value) throws FieldAccessException {
if (fieldIndex >= 0 && fieldIndex < data.size()) { if (fieldIndex >= 0 && fieldIndex < data.size()) {
write(fieldIndex, value); write(fieldIndex, value);
} }
@ -143,9 +155,9 @@ public class StructureModifier<TField> {
* @param fieldIndex - index of the field to modify. * @param fieldIndex - index of the field to modify.
* @param select - the function that modifies the field value. * @param select - the function that modifies the field value.
* @return This structure modifier - for chaining. * @return This structure modifier - for chaining.
* @throws IllegalAccessException * @throws IllegalAccessException The field cannot be accessed under the current security contraints.
*/ */
public StructureModifier<TField> modify(int fieldIndex, Function<TField, TField> select) throws IllegalAccessException { public StructureModifier<TField> modify(int fieldIndex, Function<TField, TField> select) throws FieldAccessException {
TField value = read(fieldIndex); TField value = read(fieldIndex);
return write(fieldIndex, select.apply(value)); return write(fieldIndex, select.apply(value));
} }