Allow users to construct signed property instances.

This commit is contained in:
Kristian S. Stangeland 2014-05-10 03:47:45 +02:00
parent a6dfa60d79
commit 180caed6b9
2 changed files with 65 additions and 5 deletions

View File

@ -1,8 +1,10 @@
package com.comphenix.protocol.wrappers;
import java.math.BigInteger;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import com.comphenix.protocol.injector.BukkitUnwrapper;
@ -16,6 +18,8 @@ import com.google.common.collect.Multimap;
import net.minecraft.util.com.mojang.authlib.GameProfile;
import net.minecraft.util.com.mojang.authlib.properties.Property;
import net.minecraft.util.io.netty.handler.codec.base64.Base64;
import net.minecraft.util.org.apache.commons.codec.binary.Base32;
/**
* Represents a wrapper for a game profile.
@ -27,7 +31,8 @@ public class WrappedGameProfile extends AbstractWrapper {
private static final FieldAccessor GET_UUID_STRING = Accessors.getFieldAcccessorOrNull(GameProfile.class, "id", String.class);
// Fetching game profile
private static FieldAccessor GET_PROFILE;
private static FieldAccessor PLAYER_PROFILE;
private static FieldAccessor OFFLINE_PROFILE;
// Property map
private Multimap<String, WrappedSignedProperty> propertyMap;
@ -40,18 +45,37 @@ public class WrappedGameProfile extends AbstractWrapper {
/**
* Retrieve the associated game profile of a player.
* <p>
* Note that this may not exist in the current Minecraft version.
* @param player - the player.
* @return The game profile.
*/
public static WrappedGameProfile fromPlayer(Player player) {
FieldAccessor accessor = GET_PROFILE;
FieldAccessor accessor = PLAYER_PROFILE;
Object nmsPlayer = BukkitUnwrapper.getInstance().unwrapItem(player);
if (accessor == null) {
accessor = Accessors.getFieldAccessor(MinecraftReflection.getEntityHumanClass(), GameProfile.class, true);
GET_PROFILE = accessor;
PLAYER_PROFILE = accessor;
}
return WrappedGameProfile.fromHandle(GET_PROFILE.get(nmsPlayer));
return WrappedGameProfile.fromHandle(PLAYER_PROFILE.get(nmsPlayer));
}
/**
* Retrieve the associated game profile of an offline player.
* <p>
* Note that this may not exist in the current Minecraft version.
* @param player - the offline player.
* @return The game profile.
*/
public static WrappedGameProfile fromOfflinePlayer(OfflinePlayer player) {
FieldAccessor accessor = OFFLINE_PROFILE;
if (accessor == null) {
accessor = Accessors.getFieldAccessor(player.getClass(), GameProfile.class, true);
OFFLINE_PROFILE = accessor;
}
return WrappedGameProfile.fromHandle(OFFLINE_PROFILE.get(player));
}
/**
@ -84,7 +108,21 @@ public class WrappedGameProfile extends AbstractWrapper {
// Lenient - add missing data
if (missing > 0) {
id += StringUtils.repeat("-0", missing);
if (id.length() < 12) {
id += StringUtils.repeat("-0", missing);
} else if (id.length() >= 32) {
StringBuilder builder = new StringBuilder(id);
int position = 8; // Initial position
while (missing > 0 && position < builder.length()) {
builder.insert(position, "-");
position += 5; // 4 in length, plus the hyphen
missing--;
}
id = builder.toString();
} else {
throw new IllegalArgumentException("Invalid partial UUID: " + id);
}
}
return UUID.fromString(id);
} catch (IllegalArgumentException e) {

View File

@ -10,6 +10,17 @@ import com.google.common.base.Objects;
* @author Kristian
*/
public class WrappedSignedProperty extends AbstractWrapper {
/**
* Construct a new wrapped signed property from the given values.
* @param name - the name of the property.
* @param value - the value of the property.
* @param signature - the BASE64-encoded signature of the value.
* @return The signed property.
*/
public WrappedSignedProperty(String name, String value, String signature) {
this(new Property(name, value, signature));
}
/**
* Construct a new wrapped signed property from a given handle.
* @param handle - the handle.
@ -28,6 +39,17 @@ public class WrappedSignedProperty extends AbstractWrapper {
return new WrappedSignedProperty(handle);
}
/**
* Construct a new wrapped signed property from the given values.
* @param name - the name of the property.
* @param value - the value of the property.
* @param signature - the BASE64-encoded signature of the value.
* @return The signed property.
*/
public static WrappedSignedProperty fromValues(String name, String value, String signature) {
return new WrappedSignedProperty(name, value, signature);
}
/**
* Retrieve the underlying signed property.
* @return The GameProfile.