mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2025-01-24 01:01:59 +01:00
allow easier access to converters (#1610)
This commit is contained in:
parent
4cc3957723
commit
84a0b5ffdd
@ -7,6 +7,7 @@ import com.comphenix.protocol.utility.MinecraftReflection;
|
|||||||
import com.comphenix.protocol.utility.MinecraftVersion;
|
import com.comphenix.protocol.utility.MinecraftVersion;
|
||||||
import com.comphenix.protocol.utility.StreamSerializer;
|
import com.comphenix.protocol.utility.StreamSerializer;
|
||||||
import com.comphenix.protocol.wrappers.*;
|
import com.comphenix.protocol.wrappers.*;
|
||||||
|
import com.comphenix.protocol.wrappers.WrappedProfilePublicKey.WrappedProfileKeyData;
|
||||||
import com.comphenix.protocol.wrappers.nbt.NbtBase;
|
import com.comphenix.protocol.wrappers.nbt.NbtBase;
|
||||||
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
|
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
|
||||||
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
|
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
|
||||||
@ -933,6 +934,26 @@ public abstract class AbstractStructure {
|
|||||||
return structureModifier.withType(Instant.class);
|
return structureModifier.withType(Instant.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a read/write structure for profile public keys in 1.9
|
||||||
|
* @return The Structure Modifier
|
||||||
|
*/
|
||||||
|
public StructureModifier<WrappedProfilePublicKey> getProfilePublicKeys() {
|
||||||
|
return structureModifier.withType(
|
||||||
|
MinecraftReflection.getProfilePublicKeyClass(),
|
||||||
|
BukkitConverters.getWrappedProfilePublicKeyConverter());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a read/write structure for profile public key data in 1.9
|
||||||
|
* @return The Structure Modifier
|
||||||
|
*/
|
||||||
|
public StructureModifier<WrappedProfileKeyData> getProfilePublicKeyData() {
|
||||||
|
return structureModifier.withType(
|
||||||
|
MinecraftReflection.getProfilePublicKeyDataClass(),
|
||||||
|
BukkitConverters.getWrappedPublicKeyDataConverter());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a read/write structure for the Map class.
|
* Retrieve a read/write structure for the Map class.
|
||||||
* @param keyConverter Converter for map keys
|
* @param keyConverter Converter for map keys
|
||||||
@ -1009,6 +1030,19 @@ public abstract class AbstractStructure {
|
|||||||
structureModifier.getField(index).getType());
|
structureModifier.getField(index).getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a read/write structure for an optional, passing
|
||||||
|
* the value of the optional field through the given converter
|
||||||
|
* if present.
|
||||||
|
*
|
||||||
|
* @param converter Converter for internal element of optional, if present
|
||||||
|
* @param <T> The inner type of the optional
|
||||||
|
* @return The modifier
|
||||||
|
*/
|
||||||
|
public <T> StructureModifier<Optional<T>> getOptionals(EquivalentConverter<T> converter) {
|
||||||
|
return structureModifier.withType(Optional.class, Converters.optional(converter));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an equivalent converter for ItemStack arrays.
|
* Represents an equivalent converter for ItemStack arrays.
|
||||||
* @author Kristian
|
* @author Kristian
|
||||||
|
@ -30,6 +30,10 @@ public class InternalStructure extends AbstractStructure {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public static EquivalentConverter<InternalStructure> getConverter() {
|
||||||
|
return CONVERTER;
|
||||||
|
}
|
||||||
|
|
||||||
public StructureModifier<InternalStructure> getStructures() {
|
public StructureModifier<InternalStructure> getStructures() {
|
||||||
return structureModifier.withType(Object.class, CONVERTER);
|
return structureModifier.withType(Object.class, CONVERTER);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,83 @@
|
|||||||
|
package com.comphenix.protocol.wrappers;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
import com.comphenix.protocol.BukkitInitialization;
|
||||||
|
import com.comphenix.protocol.wrappers.WrappedProfilePublicKey.WrappedProfileKeyData;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.time.Instant;
|
||||||
|
import net.minecraft.world.entity.player.ProfilePublicKey;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class WrappedProfilePublicKeyTest {
|
||||||
|
|
||||||
|
private static KeyPair generateKeyPair() throws Exception {
|
||||||
|
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||||
|
generator.initialize(2048);
|
||||||
|
return generator.generateKeyPair();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void initializeBukkit() {
|
||||||
|
BukkitInitialization.initializeAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPublicKey() throws Exception {
|
||||||
|
KeyPair keyPair = generateKeyPair();
|
||||||
|
Instant timeout = Instant.now().plusSeconds(120);
|
||||||
|
|
||||||
|
// test key data conversion
|
||||||
|
WrappedProfileKeyData keyData = new WrappedProfileKeyData(timeout, keyPair.getPublic(), new byte[]{0x01, 0x0F});
|
||||||
|
|
||||||
|
Object handle = keyData.getHandle();
|
||||||
|
ProfilePublicKey.a data = assertInstanceOf(ProfilePublicKey.a.class, handle);
|
||||||
|
|
||||||
|
assertFalse(data.a());
|
||||||
|
assertEquals(timeout, data.b());
|
||||||
|
assertEquals(keyPair.getPublic(), data.c());
|
||||||
|
assertArrayEquals(keyData.getSignature(), data.d());
|
||||||
|
|
||||||
|
// test key data unwrapping
|
||||||
|
WrappedProfileKeyData unwrapped = BukkitConverters.getWrappedPublicKeyDataConverter().getSpecific(data);
|
||||||
|
assertNotNull(unwrapped);
|
||||||
|
assertFalse(unwrapped.isExpired());
|
||||||
|
assertEquals(keyData.getKey(), unwrapped.getKey());
|
||||||
|
assertEquals(keyData.getExpireTime(), unwrapped.getExpireTime());
|
||||||
|
assertArrayEquals(keyData.getSignature(), unwrapped.getSignature());
|
||||||
|
|
||||||
|
// test key data wrapping
|
||||||
|
Object wrappedData = BukkitConverters.getWrappedPublicKeyDataConverter().getGeneric(keyData);
|
||||||
|
ProfilePublicKey.a wrapped = assertInstanceOf(ProfilePublicKey.a.class, wrappedData);
|
||||||
|
|
||||||
|
assertFalse(wrapped.a());
|
||||||
|
assertEquals(timeout, wrapped.b());
|
||||||
|
assertEquals(keyPair.getPublic(), wrapped.c());
|
||||||
|
assertArrayEquals(keyData.getSignature(), wrapped.d());
|
||||||
|
|
||||||
|
// test profile key unwrapping
|
||||||
|
WrappedProfilePublicKey profilePublicKey = new WrappedProfilePublicKey(keyData);
|
||||||
|
|
||||||
|
Object keyHandle = profilePublicKey.getHandle();
|
||||||
|
ProfilePublicKey profileKey = assertInstanceOf(ProfilePublicKey.class, keyHandle);
|
||||||
|
|
||||||
|
assertNotNull(profileKey.b());
|
||||||
|
|
||||||
|
// test profile key wrapping
|
||||||
|
WrappedProfilePublicKey wrappedKey = BukkitConverters.getWrappedProfilePublicKeyConverter().getSpecific(keyHandle);
|
||||||
|
assertNotNull(wrappedKey);
|
||||||
|
assertNotNull(wrappedKey.getKeyData());
|
||||||
|
|
||||||
|
WrappedProfileKeyData wrappedKeyData = wrappedKey.getKeyData();
|
||||||
|
assertFalse(wrappedKeyData.isExpired());
|
||||||
|
assertEquals(keyData.getKey(), wrappedKeyData.getKey());
|
||||||
|
assertEquals(keyData.getExpireTime(), wrappedKeyData.getExpireTime());
|
||||||
|
assertArrayEquals(keyData.getSignature(), wrappedKeyData.getSignature());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user