Changed the API slightly. Added WorldType to PacketContainer.
Dieser Commit ist enthalten in:
Ursprung
035277bdeb
Commit
353302fe5f
@ -1,5 +1,6 @@
|
|||||||
package com.comphenix.protocol.events;
|
package com.comphenix.protocol.events;
|
||||||
|
|
||||||
|
import org.bukkit.WorldType;
|
||||||
import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
@ -22,6 +23,17 @@ public class PacketContainer {
|
|||||||
// Current structure modifier
|
// Current structure modifier
|
||||||
protected StructureModifier<Object> structureModifier;
|
protected StructureModifier<Object> structureModifier;
|
||||||
|
|
||||||
|
// Check whether or not certain classes exists
|
||||||
|
private static boolean hasWorldType = false;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
Class.forName("net.minecraft.server.WorldType");
|
||||||
|
hasWorldType = true;
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a packet container for a new packet.
|
* Creates a packet container for a new packet.
|
||||||
* @param id - ID of the packet to create.
|
* @param id - ID of the packet to create.
|
||||||
@ -70,10 +82,22 @@ public class PacketContainer {
|
|||||||
return structureModifier;
|
return structureModifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> StructureModifier<T> getPrimitiveModifier(Class<T> primitiveType) {
|
/**
|
||||||
|
* Retrieves a read/write structure for every field with the given type.
|
||||||
|
* @param primitiveType - the type to find.
|
||||||
|
* @return A modifier for this specific type.
|
||||||
|
*/
|
||||||
|
public <T> StructureModifier<T> getSpecificModifier(Class<T> primitiveType) {
|
||||||
return structureModifier.withType(primitiveType);
|
return structureModifier.withType(primitiveType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a read/write structure for ItemStack.
|
||||||
|
* <p>
|
||||||
|
* This modifier will automatically marshall between the Bukkit ItemStack and the
|
||||||
|
* internal Minecraft ItemStack.
|
||||||
|
* @return A modifier for ItemStack fields.
|
||||||
|
*/
|
||||||
public StructureModifier<ItemStack> getItemModifier() {
|
public StructureModifier<ItemStack> getItemModifier() {
|
||||||
// Convert from and to the Bukkit wrapper
|
// Convert from and to the Bukkit wrapper
|
||||||
return structureModifier.<ItemStack>withType(net.minecraft.server.ItemStack.class, new EquivalentConverter<ItemStack>() {
|
return structureModifier.<ItemStack>withType(net.minecraft.server.ItemStack.class, new EquivalentConverter<ItemStack>() {
|
||||||
@ -88,6 +112,13 @@ public class PacketContainer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a read/write structure for arrays of ItemStacks.
|
||||||
|
* <p>
|
||||||
|
* This modifier will automatically marshall between the Bukkit ItemStack and the
|
||||||
|
* internal Minecraft ItemStack.
|
||||||
|
* @return A modifier for ItemStack array fields.
|
||||||
|
*/
|
||||||
public StructureModifier<ItemStack[]> getItemArrayModifier() {
|
public StructureModifier<ItemStack[]> getItemArrayModifier() {
|
||||||
// Convert to and from the Bukkit wrapper
|
// Convert to and from the Bukkit wrapper
|
||||||
return structureModifier.<ItemStack[]>withType(net.minecraft.server.ItemStack[].class, new EquivalentConverter<ItemStack[]>() {
|
return structureModifier.<ItemStack[]>withType(net.minecraft.server.ItemStack[].class, new EquivalentConverter<ItemStack[]>() {
|
||||||
@ -115,6 +146,35 @@ public class PacketContainer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a read/write structure for the world type enum.
|
||||||
|
* <p>
|
||||||
|
* This modifier will automatically marshall between the Bukkit world type and the
|
||||||
|
* internal Minecraft world type.
|
||||||
|
* @return A modifier for world type fields.
|
||||||
|
*/
|
||||||
|
public StructureModifier<WorldType> getWorldTypeModifier() {
|
||||||
|
|
||||||
|
if (!hasWorldType) {
|
||||||
|
// We couldn't find the Minecraft equivalent
|
||||||
|
return structureModifier.withType(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to and from the Bukkit wrapper
|
||||||
|
return structureModifier.<WorldType>withType(net.minecraft.server.WorldType.class, new EquivalentConverter<WorldType>() {
|
||||||
|
@Override
|
||||||
|
public Object getGeneric(WorldType specific) {
|
||||||
|
return net.minecraft.server.WorldType.getType(specific.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WorldType getSpecific(Object generic) {
|
||||||
|
net.minecraft.server.WorldType type = (net.minecraft.server.WorldType) generic;
|
||||||
|
return WorldType.getByName(type.name());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the ID of this packet.
|
* Retrieves the ID of this packet.
|
||||||
* @return Packet ID.
|
* @return Packet ID.
|
||||||
|
@ -186,7 +186,7 @@ public class StructureModifier<TField> {
|
|||||||
|
|
||||||
StructureModifier<T> result = subtypeCache.get(fieldType);
|
StructureModifier<T> result = subtypeCache.get(fieldType);
|
||||||
|
|
||||||
if (fieldType.equals(this.fieldType)) {
|
if (this.fieldType.equals(fieldType)) {
|
||||||
|
|
||||||
// We're dealing with the exact field type.
|
// We're dealing with the exact field type.
|
||||||
return withConverter(converter);
|
return withConverter(converter);
|
||||||
@ -196,7 +196,7 @@ public class StructureModifier<TField> {
|
|||||||
Set<Field> defaults = new HashSet<Field>();
|
Set<Field> defaults = new HashSet<Field>();
|
||||||
|
|
||||||
for (Field field : data) {
|
for (Field field : data) {
|
||||||
if (fieldType.isAssignableFrom(field.getType())) {
|
if (fieldType != null && fieldType.isAssignableFrom(field.getType())) {
|
||||||
filtered.add(field);
|
filtered.add(field);
|
||||||
|
|
||||||
if (defaultFields.contains(field))
|
if (defaultFields.contains(field))
|
||||||
@ -208,7 +208,9 @@ public class StructureModifier<TField> {
|
|||||||
result = new StructureModifier<T>();
|
result = new StructureModifier<T>();
|
||||||
result.initialize(targetType, fieldType, filtered, defaults,
|
result.initialize(targetType, fieldType, filtered, defaults,
|
||||||
converter, new HashMap<Class, StructureModifier>());
|
converter, new HashMap<Class, StructureModifier>());
|
||||||
subtypeCache.put(fieldType, result);
|
|
||||||
|
if (fieldType != null)
|
||||||
|
subtypeCache.put(fieldType, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the target too
|
// Add the target too
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren