-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
separate code for different minecraft versions in skull builder
- Loading branch information
1 parent
9fe363e
commit 2a8b3e5
Showing
7 changed files
with
296 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
bukkit/src/main/java/com/gufli/kingdomcraft/bukkit/gui/skull/LegacySkullBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.gufli.kingdomcraft.bukkit.gui.skull; | ||
|
||
import com.gufli.kingdomcraft.bukkit.reflection.Reflection; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.SkullMeta; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.UUID; | ||
|
||
public class LegacySkullBuilder implements SkullBuilder { | ||
|
||
private static Class<?> GameProfile; | ||
private static Method GameProfile_getProperties; | ||
private static Class<?> Property; | ||
private static Method Property_getName; | ||
private static Method PropertyMap_put; | ||
private static Field CraftMetaSkull_profile; | ||
private static Method CraftMetaSkull_setOwningPlayer; | ||
|
||
static { | ||
|
||
try { | ||
GameProfile = Class.forName("com.mojang.authlib.GameProfile"); | ||
GameProfile_getProperties = GameProfile.getDeclaredMethod("getProperties"); | ||
|
||
Property = Class.forName("com.mojang.authlib.properties.Property"); | ||
Property_getName = Property.getMethod("getName"); | ||
|
||
Class<?> PropertyMap = Class.forName("com.mojang.authlib.properties.PropertyMap"); | ||
PropertyMap_put = PropertyMap.getMethod("put", Object.class, Object.class); | ||
|
||
Class<?> CraftMetaSkull = Reflection.PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftMetaSkull"); | ||
CraftMetaSkull_profile = CraftMetaSkull.getDeclaredField("profile"); | ||
CraftMetaSkull_profile.setAccessible(true); | ||
|
||
// Optional | ||
try { | ||
CraftMetaSkull_setOwningPlayer = CraftMetaSkull.getMethod("setOwningPlayer", OfflinePlayer.class); | ||
} catch (NoSuchMethodException ignored) {} | ||
|
||
} catch (NoSuchMethodException | ClassNotFoundException | NoSuchFieldException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void withSkullOwner(ItemStack itemStack, OfflinePlayer owner) { | ||
if ( owner == null ) { | ||
return; | ||
} | ||
|
||
SkullMeta meta = (SkullMeta) itemStack.getItemMeta(); | ||
if ( CraftMetaSkull_setOwningPlayer != null ) { | ||
try { | ||
CraftMetaSkull_setOwningPlayer.invoke(meta, owner); | ||
return; | ||
} catch (IllegalAccessException | InvocationTargetException ignored) {} | ||
} | ||
|
||
// Fallback to deprecated method | ||
meta.setOwner(owner.getName()); | ||
itemStack.setItemMeta(meta); | ||
} | ||
|
||
@Override | ||
public void withSkullTexture(ItemStack itemStack, String texture) { | ||
try { | ||
UUID uuid = UUID.randomUUID(); | ||
Object profile = GameProfile.getDeclaredConstructor(UUID.class, String.class).newInstance(uuid, uuid.toString().substring(0, 15)); | ||
Object property = Property.getDeclaredConstructor(String.class, String.class).newInstance("textures", texture); | ||
Object properties = GameProfile_getProperties.invoke(profile); | ||
PropertyMap_put.invoke(properties, Property_getName.invoke(property), property); | ||
withSkullProfile(itemStack, profile); | ||
} catch (IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InstantiationException | InvocationTargetException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
// public void withSkullTexture(ItemStack itemStack, UUID uuid) { | ||
// try { | ||
// Object profile = GameProfile.getDeclaredConstructor(UUID.class, String.class).newInstance(uuid, uuid.toString().substring(0, 15)); | ||
// withSkullProfile(itemStack, profile); | ||
// } catch (IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InstantiationException | InvocationTargetException ex) { | ||
// ex.printStackTrace(); | ||
// } | ||
// } | ||
|
||
private void withSkullProfile(ItemStack itemStack, Object profile) { | ||
try { | ||
SkullMeta meta = (SkullMeta) itemStack.getItemMeta(); | ||
CraftMetaSkull_profile.set(meta, profile); | ||
itemStack.setItemMeta(meta); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
bukkit/src/main/java/com/gufli/kingdomcraft/bukkit/gui/skull/ModernSkullBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.gufli.kingdomcraft.bukkit.gui.skull; | ||
|
||
import org.apache.commons.lang.RandomStringUtils; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.SkullMeta; | ||
import org.bukkit.profile.PlayerProfile; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.UUID; | ||
|
||
public class ModernSkullBuilder implements SkullBuilder { | ||
|
||
@Override | ||
public void withSkullOwner(ItemStack itemStack, OfflinePlayer owner) { | ||
SkullMeta meta = (SkullMeta) itemStack.getItemMeta(); | ||
meta.setOwningPlayer(owner); | ||
itemStack.setItemMeta(meta); | ||
} | ||
|
||
@Override | ||
public void withSkullTexture(ItemStack itemStack, String texture) { | ||
try { | ||
withTexture(itemStack, new URL("https://textures.minecraft.net/texture/" + texture)); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void withProfile(ItemStack itemStack, PlayerProfile profile) { | ||
SkullMeta meta = (SkullMeta) itemStack.getItemMeta(); | ||
meta.setOwnerProfile(profile); | ||
itemStack.setItemMeta(meta); | ||
} | ||
|
||
private void withTexture(ItemStack itemStack, URL textureUrl) { | ||
PlayerProfile profile = Bukkit.createPlayerProfile(UUID.randomUUID(), RandomStringUtils.randomAlphanumeric(16)); | ||
profile.getTextures().setSkin(textureUrl); | ||
withProfile(itemStack, profile); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
bukkit/src/main/java/com/gufli/kingdomcraft/bukkit/gui/skull/SkullBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.gufli.kingdomcraft.bukkit.gui.skull; | ||
|
||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
public interface SkullBuilder { | ||
|
||
void withSkullOwner(ItemStack itemStack, OfflinePlayer owner); | ||
|
||
void withSkullTexture(ItemStack itemStack, String texture); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.