Add StaticUnsafeValues

This commit is contained in:
Chaosdave34 2024-12-23 15:16:47 +01:00
parent f37300044b
commit 5570b7bac8
5 changed files with 46 additions and 3 deletions

View file

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

View file

@ -1,6 +1,7 @@
package org.bukkit.damage; package org.bukkit.damage;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import io.papermc.paper.StaticUnsafeValues;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
@ -40,7 +41,7 @@ public interface DamageEffect {
@NotNull @NotNull
private static DamageEffect getDamageEffect(@NotNull String key) { 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);
} }
/** /**

View file

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

View file

@ -1,5 +1,7 @@
package io.papermc.paper.plugin.entrypoint; 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.provider.PluginProvider;
import io.papermc.paper.plugin.storage.BootstrapProviderStorage; import io.papermc.paper.plugin.storage.BootstrapProviderStorage;
import io.papermc.paper.plugin.storage.ProviderStorage; import io.papermc.paper.plugin.storage.ProviderStorage;
@ -32,6 +34,7 @@ public class LaunchEntryPointHandler implements EntrypointHandler {
@Override @Override
public void enter(Entrypoint<?> entrypoint) { public void enter(Entrypoint<?> entrypoint) {
StaticUnsafeValues.setProvider(PaperUnsafeValuesProvider.INSTANCE);
ProviderStorage<?> storage = this.storage.get(entrypoint); ProviderStorage<?> storage = this.storage.get(entrypoint);
if (storage == null) { if (storage == null) {
throw new IllegalArgumentException("No storage registered for entrypoint %s.".formatted(entrypoint)); throw new IllegalArgumentException("No storage registered for entrypoint %s.".formatted(entrypoint));

View file

@ -3,6 +3,7 @@ package io.papermc.paper.registry.data;
import io.papermc.paper.registry.PaperRegistryBuilder; import io.papermc.paper.registry.PaperRegistryBuilder;
import io.papermc.paper.registry.data.util.Conversions; import io.papermc.paper.registry.data.util.Conversions;
import net.minecraft.world.damagesource.*; import net.minecraft.world.damagesource.*;
import org.bukkit.craftbukkit.damage.CraftDamageEffect;
import org.bukkit.craftbukkit.damage.CraftDamageType; import org.bukkit.craftbukkit.damage.CraftDamageType;
import org.bukkit.damage.DamageEffect; import org.bukkit.damage.DamageEffect;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
@ -49,7 +50,7 @@ public class PaperDamageTypeRegistryEntry implements DamageTypeRegistryEntry {
@Override @Override
public @Nullable DamageEffect damageEffect() { public @Nullable DamageEffect damageEffect() {
return damageEffects == null ? null : CraftDamageType.damageEffectToBukkit(damageEffects); return damageEffects == null ? null : CraftDamageEffect.toBukkit(damageEffects);
} }
@Override @Override
@ -83,7 +84,7 @@ public class PaperDamageTypeRegistryEntry implements DamageTypeRegistryEntry {
@Override @Override
public Builder damageEffect(@Nullable DamageEffect effect) { public Builder damageEffect(@Nullable DamageEffect effect) {
this.damageEffects = effect == null ? null : CraftDamageType.damageEffectToNMS(effect); this.damageEffects = effect == null ? null : ((CraftDamageEffect) effect).getHandle();
return this; return this;
} }