Skip to content

Commit

Permalink
bugfix: use inbuilt craftbukkit setProfile method if it is available
Browse files Browse the repository at this point in the history
this helps prevent serializations exceptions for items.
  • Loading branch information
crashdemons committed Feb 12, 2021
1 parent f595e8b commit 33c0056
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>PlayerHeads-compatibility</artifactId>
<groupId>org.shininet.bukkit</groupId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>PlayerHeads-compatibility-lib</artifactId>
Expand Down
12 changes: 6 additions & 6 deletions PlayerHeads-compatibility/PlayerHeads-compatibility-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.shininet.bukkit</groupId>
<artifactId>PlayerHeads-compatibility</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</parent>
<artifactId>PlayerHeads-compatibility-lib</artifactId>
<packaging>jar</packaging>
Expand All @@ -31,7 +31,7 @@
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-craftbukkit-support</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
Expand Down Expand Up @@ -67,7 +67,7 @@
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-craftbukkit-1.8-support</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
Expand All @@ -79,14 +79,14 @@
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-craftbukkit-1.13-support</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</dependency>

<dependency>
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-paperapi-support</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
Expand All @@ -107,7 +107,7 @@
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-glowstone-1.12-support</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>PlayerHeads-compatibility</artifactId>
<groupId>org.shininet.bukkit</groupId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</parent>

<organization>
Expand Down Expand Up @@ -62,7 +62,7 @@
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-craftbukkit-support</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>PlayerHeads-compatibility</artifactId>
<groupId>org.shininet.bukkit</groupId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</parent>

<organization>
Expand Down Expand Up @@ -46,7 +46,7 @@
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-craftbukkit-support</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>PlayerHeads-compatibility</artifactId>
<groupId>org.shininet.bukkit</groupId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</parent>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import com.github.crashdemons.playerheads.compatibility.CompatibleProfile;
import com.mojang.authlib.GameProfile;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Skull;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;

/**
Expand All @@ -32,6 +31,12 @@ private static Field getProfileField(Object skull) throws IllegalArgumentExcepti
profileField.setAccessible(true);
return profileField;
}
private static Method getCBProfileMethod(Object skull) throws IllegalArgumentException,NoSuchMethodException,SecurityException,IllegalAccessException{
if(!(skull instanceof SkullMeta || skull instanceof Skull)) throw new IllegalArgumentException("Object is not a supported type: SkullMeta or Skull (blockstate)");
Method profileMethod = skull.getClass().getDeclaredMethod("setProfile", GameProfile.class);
profileMethod.setAccessible(true);
return profileMethod;
}

//-------------------------------------------------------------------------

Expand All @@ -47,8 +52,23 @@ public static CompatibleProfile getProfile(Object skull) throws IllegalStateExce
if(profile==null) return new CompatibleProfileCB();
return new CompatibleProfileCB(profile);
}


private static boolean setInternalProfileCB(Object skull, GameProfile profile) throws IllegalStateException{
try {
getCBProfileMethod(skull).invoke(skull, profile);
return true;
} catch (NoSuchMethodException | InvocationTargetException | SecurityException | IllegalAccessException error) {
return false;
}
}

public static boolean setInternalProfile(Object skull, GameProfile profile) throws IllegalStateException{
try {
/* in some version of Spigot after 1.8, a method was added to set the profile, which also sets serialization data
we shouuld prefer using the craftbukkit method over directly accessing the profile field in order to serialize correctly.
*/
if(setInternalProfileCB(skull,profile)) return true;
getProfileField(skull).set(skull, profile);
return true;
} catch (IllegalArgumentException | NoSuchFieldException | SecurityException | IllegalAccessException error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>PlayerHeads-compatibility</artifactId>
<groupId>org.shininet.bukkit</groupId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</parent>

<organization>
Expand Down Expand Up @@ -83,7 +83,7 @@
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-paperapi-support</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>PlayerHeads-compatibility</artifactId>
<groupId>org.shininet.bukkit</groupId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</parent>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.destroystokyo.paper.profile.PlayerProfile;
import com.github.crashdemons.playerheads.compatibility.CompatibleProfile;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;
import org.bukkit.block.Skull;
import org.bukkit.inventory.meta.SkullMeta;
Expand All @@ -29,6 +31,12 @@ private static Field getProfileField(Object skull) throws IllegalArgumentExcepti
profileField.setAccessible(true);
return profileField;
}
private static Method getPAProfileMethod(Object skull) throws IllegalArgumentException,NoSuchMethodException,SecurityException,IllegalAccessException{
if(!(skull instanceof SkullMeta || skull instanceof Skull)) throw new IllegalArgumentException("Object is not a supported type: SkullMeta or Skull (blockstate)");
Method profileMethod = skull.getClass().getDeclaredMethod("setProfile", PlayerProfile.class);
profileMethod.setAccessible(true);
return profileMethod;
}

//-------------------------------------------------------------------------

Expand All @@ -44,8 +52,22 @@ public static CompatibleProfile getProfile(Object skull) throws IllegalStateExce
if(profile==null) return new CompatibleProfilePA();
return new CompatibleProfilePA(profile);
}

private static boolean setInternalProfilePA(Object skull, PlayerProfile profile) throws IllegalStateException{
try {
getPAProfileMethod(skull).invoke(skull, profile);
return true;
} catch (NoSuchMethodException | InvocationTargetException | SecurityException | IllegalAccessException error) {
return false;
}
}

public static boolean setInternalProfile(Object skull, PlayerProfile profile) throws IllegalStateException{
try {
/* in some version of Spigot after 1.8, a method was added to set the profile, which also sets serialization data
we shouuld prefer using the craftbukkit method over directly accessing the profile field in order to serialize correctly.
*/
if(setInternalProfilePA(skull,profile)) return true;
getProfileField(skull).set(skull, profile);
return true;
} catch (IllegalArgumentException | NoSuchFieldException | SecurityException | IllegalAccessException error) {
Expand Down
2 changes: 1 addition & 1 deletion PlayerHeads-compatibility/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.shininet.bukkit</groupId>
<artifactId>PlayerHeads</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</parent>
<artifactId>PlayerHeads-compatibility</artifactId>
<packaging>pom</packaging>
Expand Down
10 changes: 9 additions & 1 deletion PlayerHeads-crossversion-plugin/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>PlayerHeads</artifactId>
<groupId>org.shininet.bukkit</groupId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.shininet.bukkit</groupId>
Expand Down Expand Up @@ -190,6 +190,14 @@
<artifactId>commons-lang</artifactId>
<groupId>commons-lang</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
<exclusion>
<artifactId>gson</artifactId>
<groupId>com.google.code.gson</groupId>
</exclusion>
<exclusion>
<artifactId>bungeecord-chat</artifactId>
<groupId>net.md-5</groupId>
Expand Down
4 changes: 2 additions & 2 deletions PlayerHeads-crossversion-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-core</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
Expand All @@ -59,7 +59,7 @@
<!-- This shouldn't be a dep which is packaging: pom -->
<groupId>${project.groupId}</groupId>
<artifactId>PlayerHeads-compatibility-lib</artifactId>
<version>5.2.14-SNAPSHOT</version>
<version>5.2.15-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
Expand Down

0 comments on commit 33c0056

Please sign in to comment.