From 5570b7bac8cece5062d35f79d2a9fab2c7716b9b Mon Sep 17 00:00:00 2001 From: Chaosdave34 Date: Mon, 23 Dec 2024 15:16:47 +0100 Subject: [PATCH] Add StaticUnsafeValues --- .../io/papermc/paper/StaticUnsafeValues.java | 23 +++++++++++++++++++ .../java/org/bukkit/damage/DamageEffect.java | 3 ++- .../paper/PaperUnsafeValuesProvider.java | 15 ++++++++++++ .../entrypoint/LaunchEntryPointHandler.java | 3 +++ .../data/PaperDamageTypeRegistryEntry.java | 5 ++-- 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 paper-api/src/main/java/io/papermc/paper/StaticUnsafeValues.java create mode 100644 paper-server/src/main/java/io/papermc/paper/PaperUnsafeValuesProvider.java diff --git a/paper-api/src/main/java/io/papermc/paper/StaticUnsafeValues.java b/paper-api/src/main/java/io/papermc/paper/StaticUnsafeValues.java new file mode 100644 index 0000000000..e2647798f6 --- /dev/null +++ b/paper-api/src/main/java/io/papermc/paper/StaticUnsafeValues.java @@ -0,0 +1,23 @@ +package io.papermc.paper; + +import org.bukkit.damage.DamageEffect; +import org.jspecify.annotations.Nullable; + +public final class StaticUnsafeValues { + private static @Nullable UnsafeValuesProvider provider; + + public static void setProvider(UnsafeValuesProvider provider) { + if (provider != null) { + StaticUnsafeValues.provider = provider; + } + } + + public static UnsafeValuesProvider getProvider() { + if (StaticUnsafeValues.provider != null) return StaticUnsafeValues.provider; + else throw new RuntimeException(); + } + + public interface UnsafeValuesProvider { + DamageEffect getDamageEffect(String key); + } +} diff --git a/paper-api/src/main/java/org/bukkit/damage/DamageEffect.java b/paper-api/src/main/java/org/bukkit/damage/DamageEffect.java index 8cf8fde602..90f0d41e71 100644 --- a/paper-api/src/main/java/org/bukkit/damage/DamageEffect.java +++ b/paper-api/src/main/java/org/bukkit/damage/DamageEffect.java @@ -1,6 +1,7 @@ package org.bukkit.damage; import com.google.common.base.Preconditions; +import io.papermc.paper.StaticUnsafeValues; import org.bukkit.Bukkit; import org.bukkit.Sound; import org.jetbrains.annotations.ApiStatus; @@ -40,7 +41,7 @@ public interface DamageEffect { @NotNull private static DamageEffect getDamageEffect(@NotNull String key) { - return Preconditions.checkNotNull(Bukkit.getUnsafe().getDamageEffect(key), "No DamageEffect found for %s. This is a bug.", key); + return Preconditions.checkNotNull(StaticUnsafeValues.getProvider().getDamageEffect(key), "No DamageEffect found for %s. This is a bug.", key); } /** diff --git a/paper-server/src/main/java/io/papermc/paper/PaperUnsafeValuesProvider.java b/paper-server/src/main/java/io/papermc/paper/PaperUnsafeValuesProvider.java new file mode 100644 index 0000000000..88f048139d --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/PaperUnsafeValuesProvider.java @@ -0,0 +1,15 @@ +package io.papermc.paper; + +import com.google.common.base.Preconditions; +import org.bukkit.craftbukkit.damage.CraftDamageEffect; +import org.bukkit.damage.DamageEffect; + +public class PaperUnsafeValuesProvider implements StaticUnsafeValues.UnsafeValuesProvider { + public static final PaperUnsafeValuesProvider INSTANCE = new PaperUnsafeValuesProvider(); + + @Override + public DamageEffect getDamageEffect(String key) { + Preconditions.checkArgument(key != null, "key cannot be null"); + return CraftDamageEffect.getById(key); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/plugin/entrypoint/LaunchEntryPointHandler.java b/paper-server/src/main/java/io/papermc/paper/plugin/entrypoint/LaunchEntryPointHandler.java index 48bc745ca9..524a160d8a 100644 --- a/paper-server/src/main/java/io/papermc/paper/plugin/entrypoint/LaunchEntryPointHandler.java +++ b/paper-server/src/main/java/io/papermc/paper/plugin/entrypoint/LaunchEntryPointHandler.java @@ -1,5 +1,7 @@ package io.papermc.paper.plugin.entrypoint; +import io.papermc.paper.PaperUnsafeValuesProvider; +import io.papermc.paper.StaticUnsafeValues; import io.papermc.paper.plugin.provider.PluginProvider; import io.papermc.paper.plugin.storage.BootstrapProviderStorage; import io.papermc.paper.plugin.storage.ProviderStorage; @@ -32,6 +34,7 @@ public class LaunchEntryPointHandler implements EntrypointHandler { @Override public void enter(Entrypoint entrypoint) { + StaticUnsafeValues.setProvider(PaperUnsafeValuesProvider.INSTANCE); ProviderStorage storage = this.storage.get(entrypoint); if (storage == null) { throw new IllegalArgumentException("No storage registered for entrypoint %s.".formatted(entrypoint)); diff --git a/paper-server/src/main/java/io/papermc/paper/registry/data/PaperDamageTypeRegistryEntry.java b/paper-server/src/main/java/io/papermc/paper/registry/data/PaperDamageTypeRegistryEntry.java index b2c3685cdb..e5271645ba 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/data/PaperDamageTypeRegistryEntry.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/data/PaperDamageTypeRegistryEntry.java @@ -3,6 +3,7 @@ package io.papermc.paper.registry.data; import io.papermc.paper.registry.PaperRegistryBuilder; import io.papermc.paper.registry.data.util.Conversions; import net.minecraft.world.damagesource.*; +import org.bukkit.craftbukkit.damage.CraftDamageEffect; import org.bukkit.craftbukkit.damage.CraftDamageType; import org.bukkit.damage.DamageEffect; import org.jspecify.annotations.Nullable; @@ -49,7 +50,7 @@ public class PaperDamageTypeRegistryEntry implements DamageTypeRegistryEntry { @Override public @Nullable DamageEffect damageEffect() { - return damageEffects == null ? null : CraftDamageType.damageEffectToBukkit(damageEffects); + return damageEffects == null ? null : CraftDamageEffect.toBukkit(damageEffects); } @Override @@ -83,7 +84,7 @@ public class PaperDamageTypeRegistryEntry implements DamageTypeRegistryEntry { @Override public Builder damageEffect(@Nullable DamageEffect effect) { - this.damageEffects = effect == null ? null : CraftDamageType.damageEffectToNMS(effect); + this.damageEffects = effect == null ? null : ((CraftDamageEffect) effect).getHandle(); return this; }