From 7d89f0c3765a2eee6cec575235826335f19c0406 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 20 Feb 2024 14:21:58 +0100 Subject: [PATCH] Update to use new 1.20.4 API - hide score number in autogenerated scoreboard sidebar and use score entries custom name - remove now useless reflection wrapper for DamageSource since there is a proper API for that --- .../lib/paper/players/PaperOnlinePlayer.java | 25 ------------------- .../paper/reflect/PandalibPaperReflect.java | 4 --- .../minecraft/server/ServerPlayer.java | 9 +------ .../wrapper/minecraft/world/DamageSource.java | 14 ----------- .../minecraft/world/DamageSources.java | 14 ----------- .../lib/paper/util/ScoreboardUtil.java | 23 +++-------------- 6 files changed, 4 insertions(+), 85 deletions(-) delete mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/DamageSource.java delete mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/DamageSources.java diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java index d321473..c96ef64 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java @@ -15,7 +15,6 @@ import net.kyori.adventure.util.Ticks; import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.Sound; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.MainHand; @@ -269,28 +268,6 @@ public interface PaperOnlinePlayer extends PaperOffPlayer, AbstractOnlinePlayer - /* - * Custom damage - */ - - /** - * Deals damages to this player. - * @param amount the amount of damage to deal. - */ - default void damage(double amount) { - getBukkitPlayer().damage(amount); // uses DamageSource.GENERIC - } - - /** - * Deals damages to this player, from the provided entity. - * @param amount the amount of damage to deal. - * @param source the entity from which the damage comes from. - */ - default void damage(double amount, LivingEntity source) { - getBukkitPlayer().damage(amount, source); // uses appropriate DamageSource according to provided player or entity - } - - @@ -305,7 +282,6 @@ public interface PaperOnlinePlayer extends PaperOffPlayer, AbstractOnlinePlayer * @param relY the relative y coordinate. * @param relZ the relative z coordinate. */ - @SuppressWarnings("UnstableApiUsage") default void teleportRelatively(float relX, float relY, float relZ) { getBukkitPlayer().teleport(getBukkitPlayer().getLocation().add(relX, relY, relZ), Relative.X, Relative.Y, Relative.Z, Relative.YAW, Relative.PITCH); } @@ -314,7 +290,6 @@ public interface PaperOnlinePlayer extends PaperOffPlayer, AbstractOnlinePlayer * Teleports this player to the specified location, using the {@link Relative} flags. * @param destination the destination. */ - @SuppressWarnings("UnstableApiUsage") default void teleportRelatively(Location destination) { getBukkitPlayer().teleport(destination, Relative.X, Relative.Y, Relative.Z, Relative.YAW, Relative.PITCH); } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java index 9f9bb50..1b0725a 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java @@ -58,8 +58,6 @@ import fr.pandacube.lib.paper.reflect.wrapper.minecraft.util.ProgressListener; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.AABB; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ChunkPos; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ChunkStorage; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.DamageSource; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.DamageSources; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.DataVersion; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Entity; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ItemStack; @@ -182,8 +180,6 @@ public class PandalibPaperReflect { thAcc.catchThrowable(() -> initWrapper(AABB.class, AABB.MAPPING.runtimeClass())); thAcc.catchThrowable(() -> initWrapper(ChunkPos.class, ChunkPos.MAPPING.runtimeClass())); thAcc.catchThrowable(() -> initWrapper(ChunkStorage.class, ChunkStorage.MAPPING.runtimeClass())); - thAcc.catchThrowable(() -> initWrapper(DamageSource.class, DamageSource.MAPPING.runtimeClass())); - thAcc.catchThrowable(() -> initWrapper(DamageSources.class, DamageSources.MAPPING.runtimeClass())); thAcc.catchThrowable(() -> initWrapper(DataVersion.class, DataVersion.MAPPING.runtimeClass())); thAcc.catchThrowable(() -> initWrapper(Entity.class, Entity.MAPPING.runtimeClass())); thAcc.catchThrowable(() -> initWrapper(ItemStack.class, ItemStack.MAPPING.runtimeClass())); diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/ServerPlayer.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/ServerPlayer.java index d5da469..8ee222f 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/ServerPlayer.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/ServerPlayer.java @@ -1,12 +1,10 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.server; -import org.bukkit.entity.Player; - import fr.pandacube.lib.paper.reflect.NMSReflect; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.DamageSource; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Entity; import fr.pandacube.lib.reflect.ReflectField; import fr.pandacube.lib.reflect.ReflectMethod; +import org.bukkit.entity.Player; import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; @@ -14,14 +12,9 @@ import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; public class ServerPlayer extends Entity { // in NMS, ServerPlayer is not a direct subclass of Entity public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.server.level.ServerPlayer")); public static final ReflectField connection = wrapEx(() -> MAPPING.mojField("connection")); - public static final ReflectMethod hurt = wrapEx(() -> MAPPING.mojMethod("hurt", DamageSource.MAPPING, float.class)); public static final ReflectMethod isTextFilteringEnabled = wrapEx(() -> MAPPING.mojMethod("isTextFilteringEnabled")); public static final ReflectMethod allowsListing = wrapEx(() -> MAPPING.mojMethod("allowsListing")); - public boolean hurt(DamageSource source, float amount) { - return (boolean) wrapReflectEx(() -> hurt.invoke(__getRuntimeInstance(), unwrap(source), amount)); - } - public boolean isTextFilteringEnabled() { return (boolean) wrapReflectEx(() -> isTextFilteringEnabled.invoke(__getRuntimeInstance())); } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/DamageSource.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/DamageSource.java deleted file mode 100644 index aa1e88c..0000000 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/DamageSource.java +++ /dev/null @@ -1,14 +0,0 @@ -package fr.pandacube.lib.paper.reflect.wrapper.minecraft.world; - -import fr.pandacube.lib.paper.reflect.NMSReflect; -import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; - -import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; - -public class DamageSource extends ReflectWrapper { - public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.world.damagesource.DamageSource")); - - protected DamageSource(Object obj) { - super(obj); - } -} diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/DamageSources.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/DamageSources.java deleted file mode 100644 index 8bb6434..0000000 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/DamageSources.java +++ /dev/null @@ -1,14 +0,0 @@ -package fr.pandacube.lib.paper.reflect.wrapper.minecraft.world; - -import fr.pandacube.lib.paper.reflect.NMSReflect; -import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; - -import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; - -public class DamageSources extends ReflectWrapper { - public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.world.damagesource.DamageSources")); - - protected DamageSources(Object obj) { - super(obj); - } -} diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ScoreboardUtil.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ScoreboardUtil.java index e640df9..e3d663d 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ScoreboardUtil.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ScoreboardUtil.java @@ -1,6 +1,7 @@ package fr.pandacube.lib.paper.util; import fr.pandacube.lib.chat.Chat; +import io.papermc.paper.scoreboard.numbers.NumberFormat; import net.kyori.adventure.text.Component; import org.bukkit.ChatColor; import org.bukkit.scoreboard.Criteria; @@ -8,7 +9,6 @@ import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Score; import org.bukkit.scoreboard.Scoreboard; -import org.bukkit.scoreboard.Team; import java.util.List; @@ -42,6 +42,7 @@ public class ScoreboardUtil { if (obj == null) { obj = scBrd.registerNewObjective("sidebar_autogen", Criteria.DUMMY, title); obj.setDisplaySlot(DisplaySlot.SIDEBAR); + obj.numberFormat(NumberFormat.blank()); } else { // only update title if needed @@ -62,21 +63,9 @@ public class ScoreboardUtil { */ int score = 1, i = 0; for (int lineIndex = Math.min(lines.length, 15) - 1; lineIndex >= 0; i++, score++, lineIndex--) { - String teamId = "sidebar_team" + score; String sbEntry = colors[i].toString(); - Team tLine = scBrd.getTeam(teamId); - if (tLine == null) { - tLine = scBrd.registerNewTeam(teamId); - } - if (!tLine.hasEntry(sbEntry)) { - tLine.addEntry(sbEntry); - } - - if (!tLine.prefix().equals(lines[lineIndex])) { - tLine.prefix(lines[lineIndex]); - } - Score scoreEntry = obj.getScore(sbEntry); + scoreEntry.customName(lines[lineIndex]); if (scoreEntry.getScore() != score) { scoreEntry.setScore(score); } @@ -84,17 +73,11 @@ public class ScoreboardUtil { // clean older data when we are reducing the number of line displayed for (; i < colors.length; i++, score++) { - String teamId = "sidebar_team" + score; String sbEntry = colors[i].toString(); if (obj.getScore(sbEntry).isScoreSet()) { scBrd.resetScores(sbEntry); } - - Team tLine = scBrd.getTeam(teamId); - if (tLine != null && !tLine.prefix().equals(Component.empty())) { - tLine.prefix(Component.empty()); - } } }