Add DamageType RegistryEvent (#11783)

This commit is contained in:
Chaosdave34 2024-12-28 00:13:27 +01:00 committed by GitHub
parent 4b893907f6
commit 5c7537cb5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 295 additions and 9 deletions

View file

@ -0,0 +1,39 @@
package io.papermc.paper;
import net.kyori.adventure.util.Services;
import org.bukkit.damage.DamageEffect;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;
/**
* Static bridge to the server internals.
* <p>
* Any and all methods in here are *not* to be called by plugin developers, may change at any time and may generally
* cause issues when called under unexpected circumstances.
*/
@ApiStatus.Internal
@NullMarked
public interface InternalAPIBridge {
/**
* Yields the instance of this API bridge by lazily requesting it from the java service loader API.
*
* @return the instance.
*/
static InternalAPIBridge get() {
class Holder {
public static final InternalAPIBridge INSTANCE = Services.service(InternalAPIBridge.class).orElseThrow();
}
return Holder.INSTANCE;
}
/**
* Creates a damage effect instance for the passed key.
*
* @param key the string key.
* @return the damage effect.
*/
DamageEffect getDamageEffect(String key);
}

View file

@ -0,0 +1,120 @@
package io.papermc.paper.registry.data;
import io.papermc.paper.registry.RegistryBuilder;
import org.bukkit.damage.DamageEffect;
import org.bukkit.damage.DamageScaling;
import org.bukkit.damage.DamageType;
import org.bukkit.damage.DeathMessageType;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
/**
* A data-centric version-specific registry entry for the {@link DamageType} type.
*/
@ApiStatus.Experimental
@ApiStatus.NonExtendable
public interface DamageTypeRegistryEntry {
/**
* Provides part of the death message translation key. (death.attack.&lt;message_id&gt;)
* <p>
* <strong>Note</strong> The translation key is only used if
* {@link #deathMessageType()} is {@link DeathMessageType#DEFAULT}
*
* @return part of the translation key
*/
String messageId();
/**
* Provides the amount of hunger exhaustion caused by this damage type.
*
* @return the exhaustion
*/
float exhaustion();
/**
* Provides the {@link DamageScaling} for this damage type.
*
* @return the damage scaling
*/
DamageScaling damageScaling();
/**
* Provides the {@link DamageEffect} for this damage type.
*
* @return the damage effect
*/
DamageEffect damageEffect();
/**
* Provides the {@link DeathMessageType} for this damage type.
*
* @return the death message type
*/
DeathMessageType deathMessageType();
/**
* A mutable builder for the {@link DamageTypeRegistryEntry} plugins may change in applicable registry events.
* <p>
* The following values are required for each builder:
* <ul>
* <li>{@link #messageId(String)}</li>
* <li>{@link #exhaustion(float)}</li>
* <li>{@link #damageScaling(DamageScaling)}</li>
* </ul>
*/
@ApiStatus.Experimental
@ApiStatus.NonExtendable
interface Builder extends DamageTypeRegistryEntry, RegistryBuilder<DamageType> {
/**
* Sets part of the death message translation key.
*
* @return this builder instance.
* @see DamageTypeRegistryEntry#messageId()
* @see DamageType#getTranslationKey()
*/
@Contract(value = "_ -> this", mutates = "this")
Builder messageId(String messageId);
/**
* Sets the amount of hunger exhaustion caused by this damage type.
*
* @return this builder instance.
* @see DamageTypeRegistryEntry#exhaustion()
* @see DamageType#getExhaustion()
*/
@Contract(value = "_ -> this", mutates = "this")
Builder exhaustion(float exhaustion);
/**
* Sets the {@link DamageScaling} for this damage type.
*
* @return this builder instance.
* @see DamageTypeRegistryEntry#damageScaling()
* @see DamageType#getDamageScaling()
*/
@Contract(value = "_ -> this", mutates = "this")
Builder damageScaling(DamageScaling scaling);
/**
* Sets the {@link DamageEffect} for this damage type.
*
* @return this builder instance.
* @see DamageTypeRegistryEntry#damageEffect()
* @see DamageType#getDamageEffect()
*/
@Contract(value = "_ -> this", mutates = "this")
Builder damageEffect(DamageEffect effect);
/**
* Sets the {@link DeathMessageType} for this damage type.
*
* @return this builder instance.
* @see DamageTypeRegistryEntry#deathMessageType()
* @see DamageType#getDeathMessageType()
*/
@Contract(value = "_ -> this", mutates = "this")
Builder deathMessageType(DeathMessageType deathMessageType);
}
}

View file

@ -2,12 +2,14 @@ package io.papermc.paper.registry.event;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.data.BannerPatternRegistryEntry;
import io.papermc.paper.registry.data.DamageTypeRegistryEntry;
import io.papermc.paper.registry.data.EnchantmentRegistryEntry;
import io.papermc.paper.registry.data.GameEventRegistryEntry;
import io.papermc.paper.registry.data.PaintingVariantRegistryEntry;
import org.bukkit.Art;
import org.bukkit.GameEvent;
import org.bukkit.block.banner.PatternType;
import org.bukkit.damage.DamageType;
import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;
@ -26,6 +28,7 @@ public final class RegistryEvents {
public static final RegistryEventProvider<Enchantment, EnchantmentRegistryEntry.Builder> ENCHANTMENT = create(RegistryKey.ENCHANTMENT);
public static final RegistryEventProvider<Art, PaintingVariantRegistryEntry.Builder> PAINTING_VARIANT = create(RegistryKey.PAINTING_VARIANT);
public static final RegistryEventProvider<PatternType, BannerPatternRegistryEntry.Builder> BANNER_PATTERN = create(RegistryKey.BANNER_PATTERN);
public static final RegistryEventProvider<DamageType, DamageTypeRegistryEntry.Builder> DAMAGE_TYPE = create(RegistryKey.DAMAGE_TYPE);
private RegistryEvents() {
}

View file

@ -126,10 +126,6 @@ public interface UnsafeValues {
@Deprecated(since = "1.20.2", forRemoval = true)
PotionType.InternalPotionData getInternalPotionData(NamespacedKey key);
@ApiStatus.Internal
@Nullable
DamageEffect getDamageEffect(@NotNull String key);
/**
* Create a new {@link DamageSource.Builder}.
*

View file

@ -1,7 +1,7 @@
package org.bukkit.damage;
import com.google.common.base.Preconditions;
import org.bukkit.Bukkit;
import io.papermc.paper.InternalAPIBridge;
import org.bukkit.Sound;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
@ -40,7 +40,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(InternalAPIBridge.get().getDamageEffect(key), "No DamageEffect found for %s. This is a bug.", key);
}
/**

View file

@ -0,0 +1,15 @@
package io.papermc.paper;
import org.bukkit.craftbukkit.damage.CraftDamageEffect;
import org.bukkit.damage.DamageEffect;
import org.jspecify.annotations.NullMarked;
@NullMarked
public class PaperServerInternalAPIBridge implements InternalAPIBridge {
public static final PaperServerInternalAPIBridge INSTANCE = new PaperServerInternalAPIBridge();
@Override
public DamageEffect getDamageEffect(final String key) {
return CraftDamageEffect.getById(key);
}
}

View file

@ -6,10 +6,9 @@ import io.papermc.paper.plugin.storage.ProviderStorage;
import io.papermc.paper.plugin.storage.ServerPluginProviderStorage;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import org.jetbrains.annotations.ApiStatus;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
/**
* Used by the server to register/load plugin bootstrappers and plugins.

View file

@ -5,6 +5,7 @@ import io.papermc.paper.adventure.PaperAdventure;
import io.papermc.paper.datacomponent.DataComponentTypes;
import io.papermc.paper.datacomponent.PaperDataComponentType;
import io.papermc.paper.registry.data.PaperBannerPatternRegistryEntry;
import io.papermc.paper.registry.data.PaperDamageTypeRegistryEntry;
import io.papermc.paper.registry.data.PaperEnchantmentRegistryEntry;
import io.papermc.paper.registry.data.PaperGameEventRegistryEntry;
import io.papermc.paper.registry.data.PaperPaintingVariantRegistryEntry;
@ -103,7 +104,7 @@ public final class PaperRegistries {
start(Registries.STRUCTURE, RegistryKey.STRUCTURE).craft(Structure.class, CraftStructure::new).build().delayed(),
start(Registries.TRIM_MATERIAL, RegistryKey.TRIM_MATERIAL).craft(TrimMaterial.class, CraftTrimMaterial::new).build().delayed(),
start(Registries.TRIM_PATTERN, RegistryKey.TRIM_PATTERN).craft(TrimPattern.class, CraftTrimPattern::new).build().delayed(),
start(Registries.DAMAGE_TYPE, RegistryKey.DAMAGE_TYPE).craft(DamageType.class, CraftDamageType::new).build().delayed(),
start(Registries.DAMAGE_TYPE, RegistryKey.DAMAGE_TYPE).craft(DamageType.class, CraftDamageType::new).writable(PaperDamageTypeRegistryEntry.PaperBuilder::new).delayed(),
start(Registries.WOLF_VARIANT, RegistryKey.WOLF_VARIANT).craft(Wolf.Variant.class, CraftWolf.CraftVariant::new).build().delayed(),
start(Registries.ENCHANTMENT, RegistryKey.ENCHANTMENT).craft(Enchantment.class, CraftEnchantment::new).serializationUpdater(FieldRename.ENCHANTMENT_RENAME).writable(PaperEnchantmentRegistryEntry.PaperBuilder::new).delayed(),
start(Registries.JUKEBOX_SONG, RegistryKey.JUKEBOX_SONG).craft(JukeboxSong.class, CraftJukeboxSong::new).build().delayed(),

View file

@ -0,0 +1,112 @@
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.DamageEffects;
import net.minecraft.world.damagesource.DamageScaling;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.damagesource.DeathMessageType;
import org.bukkit.craftbukkit.damage.CraftDamageEffect;
import org.bukkit.craftbukkit.damage.CraftDamageType;
import org.bukkit.damage.DamageEffect;
import org.jspecify.annotations.Nullable;
import static io.papermc.paper.registry.data.util.Checks.asConfigured;
public class PaperDamageTypeRegistryEntry implements DamageTypeRegistryEntry {
protected @Nullable String messageId;
protected @Nullable Float exhaustion;
protected @Nullable DamageScaling damageScaling;
protected DamageEffects damageEffects = DamageEffects.HURT;
protected DeathMessageType deathMessageType = DeathMessageType.DEFAULT;
protected final Conversions conversions;
public PaperDamageTypeRegistryEntry(
final Conversions conversions,
final @Nullable DamageType internal
) {
this.conversions = conversions;
if (internal == null) return;
this.messageId = internal.msgId();
this.exhaustion = internal.exhaustion();
this.damageScaling = internal.scaling();
this.damageEffects = internal.effects();
this.deathMessageType = internal.deathMessageType();
}
@Override
public String messageId() {
return asConfigured(messageId, "messsageId");
}
@Override
public float exhaustion() {
return asConfigured(exhaustion, "exhaustion");
}
@Override
public org.bukkit.damage.DamageScaling damageScaling() {
return CraftDamageType.damageScalingToBukkit(asConfigured(this.damageScaling, "damageScaling"));
}
@Override
public DamageEffect damageEffect() {
return CraftDamageEffect.toBukkit(damageEffects);
}
@Override
public org.bukkit.damage.DeathMessageType deathMessageType() {
return CraftDamageType.deathMessageTypeToBukkit(deathMessageType);
}
public static final class PaperBuilder extends PaperDamageTypeRegistryEntry implements DamageTypeRegistryEntry.Builder, PaperRegistryBuilder<DamageType, org.bukkit.damage.DamageType> {
public PaperBuilder(final Conversions conversions, final @Nullable DamageType internal) {
super(conversions, internal);
}
@Override
public Builder messageId(final String messageId) {
this.messageId = messageId;
return this;
}
@Override
public Builder exhaustion(final float exhaustion) {
this.exhaustion = exhaustion;
return this;
}
@Override
public Builder damageScaling(final org.bukkit.damage.DamageScaling scaling) {
this.damageScaling = CraftDamageType.damageScalingToNMS(scaling);
return this;
}
@Override
public Builder damageEffect(final DamageEffect effect) {
this.damageEffects = ((CraftDamageEffect) effect).getHandle();
return this;
}
@Override
public Builder deathMessageType(final org.bukkit.damage.DeathMessageType deathMessageType) {
this.deathMessageType = CraftDamageType.deathMessageTypeToNMS(deathMessageType);
return this;
}
@Override
public DamageType build() {
return new DamageType(
asConfigured(this.messageId, "messsageId"),
asConfigured(this.damageScaling, "scaling"),
asConfigured(this.exhaustion, "exhaustion"),
this.damageEffects,
this.deathMessageType
);
}
}
}

View file

@ -0,0 +1 @@
io.papermc.paper.PaperServerInternalAPIBridge