Use futureproof Paper component serializer when available (#4706)

#plainSerializer will be removed when adventure 5.0.0 releases. This PR prevents this from breaking in the future.

Closes #4705
This commit is contained in:
Josh Roy 2021-12-23 05:57:41 -05:00 committed by GitHub
parent c4e62ae22b
commit 685084219e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -8,8 +8,8 @@ java {
dependencies { dependencies {
implementation project(':providers:BaseProviders') implementation project(':providers:BaseProviders')
compileOnly 'io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT' compileOnly 'io.papermc.paper:paper-api:1.18.1-R0.1-SNAPSHOT'
compileOnly 'io.papermc.paper:paper-mojangapi:1.17.1-R0.1-SNAPSHOT' compileOnly 'io.papermc.paper:paper-mojangapi:1.18.1-R0.1-SNAPSHOT'
} }
essentials { essentials {

View File

@ -1,7 +1,11 @@
package net.ess3.provider.providers; package net.ess3.provider.providers;
import io.papermc.paper.advancement.AdvancementDisplay; import io.papermc.paper.advancement.AdvancementDisplay;
import io.papermc.paper.text.PaperComponents;
import net.ess3.provider.AbstractAchievementEvent; import net.ess3.provider.AbstractAchievementEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -9,12 +13,25 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerAdvancementDoneEvent; import org.bukkit.event.player.PlayerAdvancementDoneEvent;
public class PaperAdvancementListenerProvider implements Listener { public class PaperAdvancementListenerProvider implements Listener {
private final ComponentSerializer<Component, TextComponent, String> serializer;
public PaperAdvancementListenerProvider() {
ComponentSerializer<Component, TextComponent, String> yeOldSerializer;
try {
// This method is only available in Paper 1.18.1+ and replaces the old deprecated method below.
yeOldSerializer = PaperComponents.plainTextSerializer();
} catch (NoSuchMethodError e) {
//noinspection deprecation
yeOldSerializer = PaperComponents.plainSerializer();
}
this.serializer = yeOldSerializer;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onAdvancement(final PlayerAdvancementDoneEvent event) { public void onAdvancement(final PlayerAdvancementDoneEvent event) {
final AdvancementDisplay display = event.getAdvancement().getDisplay(); final AdvancementDisplay display = event.getAdvancement().getDisplay();
if (display != null && display.doesAnnounceToChat()) { if (display != null && display.doesAnnounceToChat()) {
//noinspection deprecation Bukkit.getPluginManager().callEvent(new AbstractAchievementEvent(event.getPlayer(), serializer.serialize(display.title())));
Bukkit.getPluginManager().callEvent(new AbstractAchievementEvent(event.getPlayer(), Bukkit.getUnsafe().plainComponentSerializer().serialize(display.title())));
} }
} }
} }