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
This commit is contained in:
Marc Baloup 2024-02-20 14:21:58 +01:00
parent f494c3bdb3
commit 7d89f0c376
6 changed files with 4 additions and 85 deletions

View File

@ -15,7 +15,6 @@ import net.kyori.adventure.util.Ticks;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.MainHand; 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 relY the relative y coordinate.
* @param relZ the relative z coordinate. * @param relZ the relative z coordinate.
*/ */
@SuppressWarnings("UnstableApiUsage")
default void teleportRelatively(float relX, float relY, float relZ) { 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); 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. * Teleports this player to the specified location, using the {@link Relative} flags.
* @param destination the destination. * @param destination the destination.
*/ */
@SuppressWarnings("UnstableApiUsage")
default void teleportRelatively(Location destination) { default void teleportRelatively(Location destination) {
getBukkitPlayer().teleport(destination, Relative.X, Relative.Y, Relative.Z, Relative.YAW, Relative.PITCH); getBukkitPlayer().teleport(destination, Relative.X, Relative.Y, Relative.Z, Relative.YAW, Relative.PITCH);
} }

View File

@ -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.AABB;
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ChunkPos; 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.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.DataVersion;
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Entity; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Entity;
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ItemStack; 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(AABB.class, AABB.MAPPING.runtimeClass()));
thAcc.catchThrowable(() -> initWrapper(ChunkPos.class, ChunkPos.MAPPING.runtimeClass())); thAcc.catchThrowable(() -> initWrapper(ChunkPos.class, ChunkPos.MAPPING.runtimeClass()));
thAcc.catchThrowable(() -> initWrapper(ChunkStorage.class, ChunkStorage.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(DataVersion.class, DataVersion.MAPPING.runtimeClass()));
thAcc.catchThrowable(() -> initWrapper(Entity.class, Entity.MAPPING.runtimeClass())); thAcc.catchThrowable(() -> initWrapper(Entity.class, Entity.MAPPING.runtimeClass()));
thAcc.catchThrowable(() -> initWrapper(ItemStack.class, ItemStack.MAPPING.runtimeClass())); thAcc.catchThrowable(() -> initWrapper(ItemStack.class, ItemStack.MAPPING.runtimeClass()));

View File

@ -1,12 +1,10 @@
package fr.pandacube.lib.paper.reflect.wrapper.minecraft.server; 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.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.paper.reflect.wrapper.minecraft.world.Entity;
import fr.pandacube.lib.reflect.ReflectField; import fr.pandacube.lib.reflect.ReflectField;
import fr.pandacube.lib.reflect.ReflectMethod; 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.wrapEx;
import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; 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 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 NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.server.level.ServerPlayer"));
public static final ReflectField<?> connection = wrapEx(() -> MAPPING.mojField("connection")); 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<?> isTextFilteringEnabled = wrapEx(() -> MAPPING.mojMethod("isTextFilteringEnabled"));
public static final ReflectMethod<?> allowsListing = wrapEx(() -> MAPPING.mojMethod("allowsListing")); 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() { public boolean isTextFilteringEnabled() {
return (boolean) wrapReflectEx(() -> isTextFilteringEnabled.invoke(__getRuntimeInstance())); return (boolean) wrapReflectEx(() -> isTextFilteringEnabled.invoke(__getRuntimeInstance()));
} }

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -1,6 +1,7 @@
package fr.pandacube.lib.paper.util; package fr.pandacube.lib.paper.util;
import fr.pandacube.lib.chat.Chat; import fr.pandacube.lib.chat.Chat;
import io.papermc.paper.scoreboard.numbers.NumberFormat;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.scoreboard.Criteria; import org.bukkit.scoreboard.Criteria;
@ -8,7 +9,6 @@ import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score; import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import java.util.List; import java.util.List;
@ -42,6 +42,7 @@ public class ScoreboardUtil {
if (obj == null) { if (obj == null) {
obj = scBrd.registerNewObjective("sidebar_autogen", Criteria.DUMMY, title); obj = scBrd.registerNewObjective("sidebar_autogen", Criteria.DUMMY, title);
obj.setDisplaySlot(DisplaySlot.SIDEBAR); obj.setDisplaySlot(DisplaySlot.SIDEBAR);
obj.numberFormat(NumberFormat.blank());
} }
else { else {
// only update title if needed // only update title if needed
@ -62,21 +63,9 @@ public class ScoreboardUtil {
*/ */
int score = 1, i = 0; int score = 1, i = 0;
for (int lineIndex = Math.min(lines.length, 15) - 1; lineIndex >= 0; i++, score++, lineIndex--) { for (int lineIndex = Math.min(lines.length, 15) - 1; lineIndex >= 0; i++, score++, lineIndex--) {
String teamId = "sidebar_team" + score;
String sbEntry = colors[i].toString(); 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); Score scoreEntry = obj.getScore(sbEntry);
scoreEntry.customName(lines[lineIndex]);
if (scoreEntry.getScore() != score) { if (scoreEntry.getScore() != score) {
scoreEntry.setScore(score); scoreEntry.setScore(score);
} }
@ -84,17 +73,11 @@ public class ScoreboardUtil {
// clean older data when we are reducing the number of line displayed // clean older data when we are reducing the number of line displayed
for (; i < colors.length; i++, score++) { for (; i < colors.length; i++, score++) {
String teamId = "sidebar_team" + score;
String sbEntry = colors[i].toString(); String sbEntry = colors[i].toString();
if (obj.getScore(sbEntry).isScoreSet()) { if (obj.getScore(sbEntry).isScoreSet()) {
scBrd.resetScores(sbEntry); scBrd.resetScores(sbEntry);
} }
Team tLine = scBrd.getTeam(teamId);
if (tLine != null && !tLine.prefix().equals(Component.empty())) {
tLine.prefix(Component.empty());
}
} }
} }