Finish moving over to Holderable (#12646)

This commit is contained in:
Jake Potrebic 2025-06-10 16:29:10 -07:00 committed by GitHub
parent 519e4224b1
commit ba7fb23ddd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 171 additions and 723 deletions

View File

@ -1,14 +1,13 @@
package io.papermc.paper.datacomponent;
import java.util.function.Function;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.util.NullOps;
import net.minecraft.util.Unit;
import org.bukkit.craftbukkit.CraftRegistry;
public record DataComponentAdapter<NMS, API>(
DataComponentType<NMS> type,
Function<API, NMS> apiToVanilla,
Function<NMS, API> vanillaToApi,
boolean codecValidation
@ -27,11 +26,11 @@ public record DataComponentAdapter<NMS, API>(
return this.apiToVanilla == API_TO_UNIMPLEMENTED_CONVERTER;
}
public NMS toVanilla(final API value) {
public NMS toVanilla(final API value, final Holder<? extends DataComponentType<NMS>> type) {
final NMS nms = this.apiToVanilla.apply(value);
if (this.codecValidation) {
this.type.codecOrThrow().encodeStart(CraftRegistry.getMinecraftRegistry().createSerializationContext(NullOps.INSTANCE), nms).ifError(error -> {
throw new IllegalArgumentException("Failed to encode data component %s (%s)".formatted(BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(this.type), error.message()));
if (this.codecValidation && !type.value().isTransient()) {
type.value().codecOrThrow().encodeStart(CraftRegistry.getMinecraftRegistry().createSerializationContext(NullOps.INSTANCE), nms).ifError(error -> {
throw new IllegalArgumentException("Failed to encode data component %s (%s)".formatted(type.unwrapKey().orElseThrow(), error.message()));
});
}

View File

@ -40,10 +40,10 @@ import io.papermc.paper.datacomponent.item.PaperUseRemainder;
import io.papermc.paper.datacomponent.item.PaperWeapon;
import io.papermc.paper.datacomponent.item.PaperWritableBookContent;
import io.papermc.paper.datacomponent.item.PaperWrittenBookContent;
import io.papermc.paper.registry.PaperRegistries;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import io.papermc.paper.registry.PaperRegistries;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
@ -147,7 +147,8 @@ public final class DataComponentAdapters {
register(DataComponents.OMINOUS_BOTTLE_AMPLIFIER, PaperOminousBottleAmplifier::new);
register(DataComponents.JUKEBOX_PLAYABLE, PaperJukeboxPlayable::new);
register(DataComponents.PROVIDES_BANNER_PATTERNS, PaperRegistries::fromNms, PaperRegistries::toNms);
register(DataComponents.RECIPES,
register(
DataComponents.RECIPES,
nms -> transformUnmodifiable(nms, PaperAdventure::asAdventureKey),
api -> transformUnmodifiable(api, key -> PaperAdventure.asVanilla(Registries.RECIPE, key))
);
@ -164,7 +165,7 @@ public final class DataComponentAdapters {
// bees
// register(DataComponents.LOCK, PaperLockCode::new);
register(DataComponents.CONTAINER_LOOT, PaperSeededContainerLoot::new);
register(DataComponents.BREAK_SOUND, nms -> PaperAdventure.asAdventure(nms.value().location()), PaperAdventure::resolveSound);
register(DataComponents.BREAK_SOUND, nms -> PaperAdventure.asAdventure(nms.value().location()), PaperAdventure::resolveSound);
register(DataComponents.TOOLTIP_DISPLAY, PaperTooltipDisplay::new);
register(DataComponents.WEAPON, PaperWeapon::new);
register(DataComponents.BLOCKS_ATTACKS, PaperBlocksAttacks::new);
@ -195,38 +196,42 @@ public final class DataComponentAdapters {
register(DataComponents.SHEEP_COLOR, nms -> DyeColor.getByWoolData((byte) nms.getId()), api -> net.minecraft.world.item.DyeColor.byId(api.getWoolData()));
register(DataComponents.SHULKER_COLOR, nms -> DyeColor.getByWoolData((byte) nms.getId()), api -> net.minecraft.world.item.DyeColor.byId(api.getWoolData()));
for (final Map.Entry<ResourceKey<DataComponentType<?>>, DataComponentType<?>> componentType : BuiltInRegistries.DATA_COMPONENT_TYPE.entrySet()) {
if (!ADAPTERS.containsKey(componentType.getKey())) {
registerUnimplemented(componentType.getValue());
for (final ResourceKey<DataComponentType<?>> key : BuiltInRegistries.DATA_COMPONENT_TYPE.registryKeySet()) {
if (!ADAPTERS.containsKey(key)) {
registerUnimplemented(key);
}
}
}
private static <NMS> ResourceKey<DataComponentType<?>> getKey(final DataComponentType<NMS> type) {
return BuiltInRegistries.DATA_COMPONENT_TYPE.getResourceKey(type).orElseThrow();
}
public static void registerUntyped(final DataComponentType<Unit> type) {
registerInternal(type, UNIT_TO_API_CONVERTER, DataComponentAdapter.API_TO_UNIT_CONVERTER, false);
registerInternal(getKey(type), UNIT_TO_API_CONVERTER, DataComponentAdapter.API_TO_UNIT_CONVERTER, false);
}
private static <COMMON> void registerIdentity(final DataComponentType<COMMON> type) {
registerInternal(type, Function.identity(), Function.identity(), true);
registerInternal(getKey(type), Function.identity(), Function.identity(), true);
}
public static <NMS> void registerUnimplemented(final DataComponentType<NMS> type) {
registerInternal(type, UNIMPLEMENTED_TO_API_CONVERTER, DataComponentAdapter.API_TO_UNIMPLEMENTED_CONVERTER, false);
@SuppressWarnings("unchecked")
public static void registerUnimplemented(final ResourceKey<DataComponentType<?>> key) {
registerInternal(key, UNIMPLEMENTED_TO_API_CONVERTER, DataComponentAdapter.API_TO_UNIMPLEMENTED_CONVERTER, false);
}
private static <NMS, API extends Handleable<NMS>> void register(final DataComponentType<NMS> type, final Function<NMS, API> vanillaToApi) {
registerInternal(type, vanillaToApi, Handleable::getHandle, false);
registerInternal(getKey(type), vanillaToApi, Handleable::getHandle, false);
}
private static <NMS, API> void register(final DataComponentType<NMS> type, final Function<NMS, API> vanillaToApi, final Function<API, NMS> apiToVanilla) {
registerInternal(type, vanillaToApi, apiToVanilla, false);
registerInternal(getKey(type), vanillaToApi, apiToVanilla, false);
}
private static <NMS, API> void registerInternal(final DataComponentType<NMS> type, final Function<NMS, API> vanillaToApi, final Function<API, NMS> apiToVanilla, final boolean codecValidation) {
final ResourceKey<DataComponentType<?>> key = BuiltInRegistries.DATA_COMPONENT_TYPE.getResourceKey(type).orElseThrow();
private static <NMS, API> void registerInternal(final ResourceKey<DataComponentType<?>> key, final Function<NMS, API> vanillaToApi, final Function<API, NMS> apiToVanilla, final boolean codecValidation) {
if (ADAPTERS.containsKey(key)) {
throw new IllegalStateException("Duplicate adapter registration for " + key);
}
ADAPTERS.put(key, new DataComponentAdapter<>(type, apiToVanilla, vanillaToApi, codecValidation && !type.isTransient()));
ADAPTERS.put(key, new DataComponentAdapter<>(apiToVanilla, vanillaToApi, codecValidation));
}
}

View File

@ -1,17 +1,16 @@
package io.papermc.paper.datacomponent;
import io.papermc.paper.registry.HolderableBase;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.Handleable;
import org.jspecify.annotations.Nullable;
public abstract class PaperDataComponentType<T, NMS> implements DataComponentType, Handleable<net.minecraft.core.component.DataComponentType<NMS>> {
public abstract class PaperDataComponentType<T, NMS> extends HolderableBase<net.minecraft.core.component.DataComponentType<NMS>> implements DataComponentType {
static {
DataComponentAdapters.bootstrap();
@ -42,80 +41,64 @@ public abstract class PaperDataComponentType<T, NMS> implements DataComponentTyp
return type.getAdapter().fromVanilla(nmsValue);
}
private final NamespacedKey key;
private final net.minecraft.core.component.DataComponentType<NMS> type;
private final DataComponentAdapter<NMS, T> adapter;
public PaperDataComponentType(final NamespacedKey key, final net.minecraft.core.component.DataComponentType<NMS> type, final DataComponentAdapter<NMS, T> adapter) {
this.key = key;
this.type = type;
private PaperDataComponentType(final Holder<net.minecraft.core.component.DataComponentType<NMS>> holder, final DataComponentAdapter<NMS, T> adapter) {
super(holder);
this.adapter = adapter;
}
@Override
public NamespacedKey getKey() {
return this.key;
}
@Override
public boolean isPersistent() {
return !this.type.isTransient();
return !this.getHandle().isTransient();
}
public DataComponentAdapter<NMS, T> getAdapter() {
return this.adapter;
}
@Override
public net.minecraft.core.component.DataComponentType<NMS> getHandle() {
return this.type;
}
@SuppressWarnings("unchecked")
public static <NMS> DataComponentType of(final NamespacedKey key, final net.minecraft.core.component.DataComponentType<NMS> type) {
final DataComponentAdapter<NMS, ?> adapter = (DataComponentAdapter<NMS, ?>) DataComponentAdapters.ADAPTERS.get(BuiltInRegistries.DATA_COMPONENT_TYPE.getResourceKey(type).orElseThrow());
@SuppressWarnings({"unchecked"})
public static <NMS> DataComponentType of(final Holder<?> holder) {
final DataComponentAdapter<NMS, ?> adapter = (DataComponentAdapter<NMS, ?>) DataComponentAdapters.ADAPTERS.get(holder.unwrapKey().orElseThrow());
if (adapter == null) {
throw new IllegalArgumentException("No adapter found for " + key);
throw new IllegalArgumentException("No adapter found for " + holder);
}
if (adapter.isUnimplemented()) {
return new Unimplemented<>(key, type, adapter);
return new Unimplemented<>((Holder<net.minecraft.core.component.DataComponentType<NMS>>) holder, adapter);
} else if (adapter.isValued()) {
return new ValuedImpl<>(key, type, adapter);
return new ValuedImpl<>((Holder<net.minecraft.core.component.DataComponentType<NMS>>) holder, adapter);
} else {
return new NonValuedImpl<>(key, type, adapter);
return new NonValuedImpl<>((Holder<net.minecraft.core.component.DataComponentType<NMS>>) holder, adapter);
}
}
public static final class NonValuedImpl<T, NMS> extends PaperDataComponentType<T, NMS> implements NonValued {
NonValuedImpl(
final NamespacedKey key,
final net.minecraft.core.component.DataComponentType<NMS> type,
final Holder<net.minecraft.core.component.DataComponentType<NMS>> holder,
final DataComponentAdapter<NMS, T> adapter
) {
super(key, type, adapter);
super(holder, adapter);
}
}
public static final class ValuedImpl<T, NMS> extends PaperDataComponentType<T, NMS> implements Valued<T> {
ValuedImpl(
final NamespacedKey key,
final net.minecraft.core.component.DataComponentType<NMS> type,
final Holder<net.minecraft.core.component.DataComponentType<NMS>> holder,
final DataComponentAdapter<NMS, T> adapter
) {
super(key, type, adapter);
super(holder, adapter);
}
}
public static final class Unimplemented<T, NMS> extends PaperDataComponentType<T, NMS> {
public Unimplemented(
final NamespacedKey key,
final net.minecraft.core.component.DataComponentType<NMS> type,
final Holder<net.minecraft.core.component.DataComponentType<NMS>> holder,
final DataComponentAdapter<NMS, T> adapter
) {
super(key, type, adapter);
super(holder, adapter);
}
}
}

View File

@ -12,11 +12,17 @@ public abstract class HolderableBase<M> implements Holderable<M> {
this.holder = holder;
}
// methods below are overridden to make final
@Override
public final Holder<M> getHolder() {
return this.holder;
}
@Override
public final M getHandle() {
return Holderable.super.getHandle();
}
@Override
public final int hashCode() {
return Holderable.super.implHashCode();
@ -28,7 +34,7 @@ public abstract class HolderableBase<M> implements Holderable<M> {
}
@Override
public final String toString() {
public String toString() {
return Holderable.super.implToString();
}

View File

@ -1,6 +1,7 @@
package io.papermc.paper.util;
import com.google.common.base.Preconditions;
import io.papermc.paper.registry.HolderableBase;
import java.util.Locale;
import net.minecraft.core.Holder;
import org.bukkit.Keyed;
@ -12,14 +13,13 @@ import org.jspecify.annotations.Nullable;
@SuppressWarnings({"removal", "DeprecatedIsStillUsed"})
@Deprecated
@NullMarked
public abstract class OldEnumHolderable<A extends OldEnum<A>, M> implements Holderable<M>, OldEnum<A>, Keyed {
public abstract class OldEnumHolderable<A extends OldEnum<A>, M> extends HolderableBase<M> implements Holderable<M>, OldEnum<A>, Keyed {
private final Holder<M> holder;
private final int ordinal;
private final @Nullable String name;
protected OldEnumHolderable(final Holder<M> holder, final int ordinal) {
this.holder = holder;
super(holder);
this.ordinal = ordinal;
if (holder instanceof final Holder.Reference<M> reference) {
// For backwards compatibility, minecraft values will stile return the uppercase name without the namespace,
@ -36,11 +36,6 @@ public abstract class OldEnumHolderable<A extends OldEnum<A>, M> implements Hold
}
}
@Override
public Holder<M> getHolder() {
return this.holder;
}
@Override
@Deprecated
public int compareTo(final A other) {
@ -66,21 +61,6 @@ public abstract class OldEnumHolderable<A extends OldEnum<A>, M> implements Hold
Preconditions.checkState(this.holder.kind() == Holder.Kind.REFERENCE, "Cannot call method for this registry item, because it is not registered.");
}
@Override
public NamespacedKey getKey() {
return MCUtil.fromResourceKey(this.holder.unwrapKey().orElseThrow(() -> new IllegalStateException("Cannot get key for this registry item, because it is not registered.")));
}
@Override
public boolean equals(final Object obj) {
return this.implEquals(obj);
}
@Override
public int hashCode() {
return this.implHashCode();
}
@Override
public String toString() {
if (this.name != null) {

View File

@ -1,12 +1,12 @@
package org.bukkit.craftbukkit;
import io.papermc.paper.util.Holderable;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import org.bukkit.GameEvent;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.jetbrains.annotations.NotNull;
public class CraftGameEvent extends GameEvent implements Handleable<net.minecraft.world.level.gameevent.GameEvent> {
public class CraftGameEvent extends GameEvent implements Holderable<net.minecraft.world.level.gameevent.GameEvent> {
public static GameEvent minecraftToBukkit(net.minecraft.world.level.gameevent.GameEvent minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.GAME_EVENT);
@ -16,57 +16,44 @@ public class CraftGameEvent extends GameEvent implements Handleable<net.minecraf
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
private final net.minecraft.resources.ResourceKey<net.minecraft.world.level.gameevent.GameEvent> handleKey; // Paper
private final net.minecraft.world.level.gameevent.GameEvent handle;
private final Holder<net.minecraft.world.level.gameevent.GameEvent> holder;
public CraftGameEvent(NamespacedKey key, net.minecraft.world.level.gameevent.GameEvent handle) {
this.key = key;
this.handleKey = net.minecraft.resources.ResourceKey.create(net.minecraft.core.registries.Registries.GAME_EVENT, org.bukkit.craftbukkit.util.CraftNamespacedKey.toMinecraft(key)); // Paper
this.handle = handle;
public CraftGameEvent(final Holder<net.minecraft.world.level.gameevent.GameEvent> holder) {
this.holder = holder;
}
@Override
public net.minecraft.world.level.gameevent.GameEvent getHandle() {
return this.handle;
public Holder<net.minecraft.world.level.gameevent.GameEvent> getHolder() {
return this.holder;
}
@Override
public int getRange() {
return this.handle.notificationRadius();
return this.getHandle().notificationRadius();
}
@Override
public int getVibrationLevel() {
return net.minecraft.world.level.gameevent.vibrations.VibrationSystem.getGameEventFrequency(this.handleKey);
return net.minecraft.world.level.gameevent.vibrations.VibrationSystem.getGameEventFrequency(this.getHolder().unwrapKey().orElseThrow());
}
@NotNull
@Override
public NamespacedKey getKey() {
return this.key;
return Holderable.super.getKey();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftGameEvent)) {
return false;
}
return this.getKey().equals(((GameEvent) other).getKey());
return Holderable.super.implEquals(other);
}
@Override
public int hashCode() {
return this.getKey().hashCode();
return Holderable.super.implHashCode();
}
@Override
public String toString() {
return "CraftGameEvent{key=" + this.key + "}";
return Holderable.super.implToString();
}
}

View File

@ -1,22 +1,19 @@
package org.bukkit.craftbukkit;
import com.google.common.base.Preconditions;
import io.papermc.paper.registry.HolderableBase;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.contents.TranslatableContents;
import org.bukkit.JukeboxSong;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.jetbrains.annotations.NotNull;
public class CraftJukeboxSong implements JukeboxSong, Handleable<net.minecraft.world.item.JukeboxSong> {
public class CraftJukeboxSong extends HolderableBase<net.minecraft.world.item.JukeboxSong> implements JukeboxSong {
public static JukeboxSong minecraftToBukkit(net.minecraft.world.item.JukeboxSong minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.JUKEBOX_SONG);
}
public static JukeboxSong minecraftHolderToBukkit(Holder<net.minecraft.world.item.JukeboxSong> minecraft) {
return CraftJukeboxSong.minecraftToBukkit(minecraft.value());
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registries.JUKEBOX_SONG);
}
public static net.minecraft.world.item.JukeboxSong bukkitToMinecraft(JukeboxSong bukkit) {
@ -24,41 +21,16 @@ public class CraftJukeboxSong implements JukeboxSong, Handleable<net.minecraft.w
}
public static Holder<net.minecraft.world.item.JukeboxSong> bukkitToMinecraftHolder(JukeboxSong bukkit) {
Preconditions.checkArgument(bukkit != null);
net.minecraft.core.Registry<net.minecraft.world.item.JukeboxSong> registry = CraftRegistry.getMinecraftRegistry(Registries.JUKEBOX_SONG);
if (registry.wrapAsHolder(CraftJukeboxSong.bukkitToMinecraft(bukkit)) instanceof Holder.Reference<net.minecraft.world.item.JukeboxSong> holder) {
return holder;
}
throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own trim pattern without properly registering it.");
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.JUKEBOX_SONG);
}
private final NamespacedKey key;
private final net.minecraft.world.item.JukeboxSong handle;
public CraftJukeboxSong(NamespacedKey key, net.minecraft.world.item.JukeboxSong handle) {
this.key = key;
this.handle = handle;
public CraftJukeboxSong(final Holder<net.minecraft.world.item.JukeboxSong> holder) {
super(holder);
}
@Override
public net.minecraft.world.item.JukeboxSong getHandle() {
return this.handle;
}
@Override
@NotNull
public NamespacedKey getKey() {
return this.key;
}
@NotNull
@Override
public String getTranslationKey() {
if (!(this.handle.description().getContents() instanceof TranslatableContents)) throw new UnsupportedOperationException("Description isn't translatable!"); // Paper
return ((TranslatableContents) this.handle.description().getContents()).getKey();
if (!(this.getHandle().description().getContents() instanceof TranslatableContents)) throw new UnsupportedOperationException("Description isn't translatable!"); // Paper
return ((TranslatableContents) this.getHandle().description().getContents()).getKey();
}
}

View File

@ -2,6 +2,7 @@ package org.bukkit.craftbukkit.attribute;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import org.bukkit.NamespacedKey;
import org.jetbrains.annotations.NotNull;
@ -54,11 +55,7 @@ final class AttributeMappings {
public static @NotNull NamespacedKey uuidToKey(final UUID uuid) {
final NamespacedKey key = ATTRIBUTE_MODIFIER_IDS.get(uuid);
if (key != null) {
return key;
} else {
return NamespacedKey.minecraft(uuid.toString());
}
return Objects.requireNonNullElseGet(key, () -> NamespacedKey.minecraft(uuid.toString()));
}
private static void add(final long msb, final long lsb, final String id) {

View File

@ -2,6 +2,7 @@ package org.bukkit.craftbukkit.attribute;
import com.google.common.base.Preconditions;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.util.OldEnumHolderable;
import java.util.Locale;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
@ -10,10 +11,8 @@ import org.bukkit.attribute.Attribute;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.legacy.FieldRename;
import org.bukkit.craftbukkit.util.ApiVersion;
import org.bukkit.craftbukkit.util.Handleable;
import org.jetbrains.annotations.NotNull;
public class CraftAttribute implements Attribute, Handleable<net.minecraft.world.entity.ai.attributes.Attribute> {
public class CraftAttribute extends OldEnumHolderable<Attribute, net.minecraft.world.entity.ai.attributes.Attribute> implements Attribute {
private static int count = 0;
@ -22,7 +21,7 @@ public class CraftAttribute implements Attribute, Handleable<net.minecraft.world
}
public static Attribute minecraftHolderToBukkit(Holder<net.minecraft.world.entity.ai.attributes.Attribute> minecraft) {
return CraftAttribute.minecraftToBukkit(minecraft.value());
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registries.ATTRIBUTE);
}
public static Attribute stringToBukkit(String string) {
@ -44,16 +43,7 @@ public class CraftAttribute implements Attribute, Handleable<net.minecraft.world
}
public static Holder<net.minecraft.world.entity.ai.attributes.Attribute> bukkitToMinecraftHolder(Attribute bukkit) {
Preconditions.checkArgument(bukkit != null);
net.minecraft.core.Registry<net.minecraft.world.entity.ai.attributes.Attribute> registry = CraftRegistry.getMinecraftRegistry(Registries.ATTRIBUTE);
if (registry.wrapAsHolder(CraftAttribute.bukkitToMinecraft(bukkit)) instanceof Holder.Reference<net.minecraft.world.entity.ai.attributes.Attribute> holder) {
return holder;
}
throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own sound effect with out properly registering it.");
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.ATTRIBUTE);
}
public static String bukkitToString(Attribute bukkit) {
@ -62,85 +52,17 @@ public class CraftAttribute implements Attribute, Handleable<net.minecraft.world
return bukkit.getKey().toString();
}
private final NamespacedKey key;
private final net.minecraft.world.entity.ai.attributes.Attribute attributeBase;
private final String name;
private final int ordinal;
public CraftAttribute(NamespacedKey key, net.minecraft.world.entity.ai.attributes.Attribute attributeBase) {
this.key = key;
this.attributeBase = attributeBase;
// For backwards compatibility, minecraft values will stile return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive attribute specific values.
// Custom attributes will return the key with namespace. For a plugin this should look than like a new attribute
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT);
} else {
this.name = key.toString();
}
this.ordinal = CraftAttribute.count++;
public CraftAttribute(final Holder<net.minecraft.world.entity.ai.attributes.Attribute> holder) {
super(holder, count++);
}
@Override
public net.minecraft.world.entity.ai.attributes.Attribute getHandle() {
return this.attributeBase;
}
@NotNull
@Override
public NamespacedKey getKey() {
return this.key;
}
@NotNull
@Override
public String getTranslationKey() {
return this.attributeBase.getDescriptionId();
return this.getHandle().getDescriptionId();
}
@Override
public @NotNull String translationKey() {
return this.attributeBase.getDescriptionId();
}
@Override
public int compareTo(@NotNull Attribute attribute) {
return this.ordinal - attribute.ordinal();
}
@NotNull
@Override
public String name() {
return this.name;
}
@Override
public int ordinal() {
return this.ordinal;
}
@Override
public String toString() {
// For backwards compatibility
return this.name();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftAttribute otherAttribute)) {
return false;
}
return this.getKey().equals(otherAttribute.getKey());
}
@Override
public int hashCode() {
return this.getKey().hashCode();
public String translationKey() {
return this.getHandle().getDescriptionId();
}
}

View File

@ -1,15 +1,13 @@
package org.bukkit.craftbukkit.entity;
import com.google.common.base.Preconditions;
import java.util.Locale;
import io.papermc.paper.util.OldEnumHolderable;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.animal.CatVariant;
import org.bukkit.DyeColor;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.entity.Cat;
public class CraftCat extends CraftTameableAnimal implements Cat {
@ -45,7 +43,7 @@ public class CraftCat extends CraftTameableAnimal implements Cat {
this.getHandle().setCollarColor(net.minecraft.world.item.DyeColor.byId(color.getWoolData()));
}
public static class CraftType implements Type, Handleable<CatVariant> {
public static class CraftType extends OldEnumHolderable<Type, CatVariant> implements Type {
private static int count = 0;
public static Type minecraftToBukkit(CatVariant minecraft) {
@ -53,7 +51,7 @@ public class CraftCat extends CraftTameableAnimal implements Cat {
}
public static Type minecraftHolderToBukkit(Holder<CatVariant> minecraft) {
return CraftType.minecraftToBukkit(minecraft.value());
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registries.CAT_VARIANT);
}
public static CatVariant bukkitToMinecraft(Type bukkit) {
@ -64,73 +62,8 @@ public class CraftCat extends CraftTameableAnimal implements Cat {
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.CAT_VARIANT);
}
private final NamespacedKey key;
private final CatVariant catVariant;
private final String name;
private final int ordinal;
public CraftType(NamespacedKey key, CatVariant catVariant) {
this.key = key;
this.catVariant = catVariant;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive type specific values.
// Custom types will return the key with namespace. For a plugin this should look than like a new type
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT);
} else {
this.name = key.toString();
}
this.ordinal = CraftType.count++;
}
@Override
public CatVariant getHandle() {
return this.catVariant;
}
@Override
public NamespacedKey getKey() {
return this.key;
}
@Override
public int compareTo(Type variant) {
return this.ordinal - variant.ordinal();
}
@Override
public String name() {
return this.name;
}
@Override
public int ordinal() {
return this.ordinal;
}
@Override
public String toString() {
// For backwards compatibility
return this.name();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftType)) {
return false;
}
return this.getKey().equals(((CraftType) other).getKey());
}
@Override
public int hashCode() {
return this.getKey().hashCode();
public CraftType(final Holder<CatVariant> holder) {
super(holder, count++);
}
}

View File

@ -1,15 +1,13 @@
package org.bukkit.craftbukkit.entity;
import com.google.common.base.Preconditions;
import java.util.Locale;
import io.papermc.paper.util.OldEnumHolderable;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.animal.frog.Frog;
import net.minecraft.world.entity.animal.frog.FrogVariant;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.entity.Entity;
public class CraftFrog extends CraftAnimals implements org.bukkit.entity.Frog {
@ -49,7 +47,7 @@ public class CraftFrog extends CraftAnimals implements org.bukkit.entity.Frog {
this.getHandle().setVariant(CraftVariant.bukkitToMinecraftHolder(variant));
}
public static class CraftVariant implements Variant, Handleable<FrogVariant> {
public static class CraftVariant extends OldEnumHolderable<Variant, FrogVariant> implements Variant {
private static int count = 0;
public static Variant minecraftToBukkit(FrogVariant minecraft) {
@ -57,7 +55,7 @@ public class CraftFrog extends CraftAnimals implements org.bukkit.entity.Frog {
}
public static Variant minecraftHolderToBukkit(Holder<FrogVariant> minecraft) {
return CraftVariant.minecraftToBukkit(minecraft.value());
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registries.FROG_VARIANT);
}
public static FrogVariant bukkitToMinecraft(Variant bukkit) {
@ -67,74 +65,8 @@ public class CraftFrog extends CraftAnimals implements org.bukkit.entity.Frog {
public static Holder<FrogVariant> bukkitToMinecraftHolder(Variant bukkit) {
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.FROG_VARIANT);
}
private final NamespacedKey key;
private final FrogVariant frogVariant;
private final String name;
private final int ordinal;
public CraftVariant(NamespacedKey key, FrogVariant frogVariant) {
this.key = key;
this.frogVariant = frogVariant;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive variant specific values.
// Custom variants will return the key with namespace. For a plugin this should look than like a new variant
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT);
} else {
this.name = key.toString();
}
this.ordinal = CraftVariant.count++;
}
@Override
public FrogVariant getHandle() {
return this.frogVariant;
}
@Override
public NamespacedKey getKey() {
return this.key;
}
@Override
public int compareTo(Variant variant) {
return this.ordinal - variant.ordinal();
}
@Override
public String name() {
return this.name;
}
@Override
public int ordinal() {
return this.ordinal;
}
@Override
public String toString() {
// For backwards compatibility
return this.name();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftVariant)) {
return false;
}
return this.getKey().equals(((Variant) other).getKey());
}
@Override
public int hashCode() {
return this.getKey().hashCode();
public CraftVariant(final Holder<FrogVariant> holder) {
super(holder, count++);
}
}
}

View File

@ -2,7 +2,7 @@ package org.bukkit.craftbukkit.entity;
import com.destroystokyo.paper.entity.villager.Reputation;
import com.google.common.base.Preconditions;
import java.util.Locale;
import io.papermc.paper.util.OldEnumHolderable;
import java.util.Map;
import java.util.UUID;
import net.minecraft.core.BlockPos;
@ -14,11 +14,9 @@ import net.minecraft.world.entity.npc.VillagerType;
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.world.level.block.state.BlockState;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.entity.Villager;
import org.bukkit.entity.ZombieVillager;
import org.bukkit.event.entity.CreatureSpawnEvent;
@ -164,7 +162,7 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
return (entityzombievillager != null) ? (ZombieVillager) entityzombievillager.getBukkitEntity() : null;
}
public static class CraftType implements Type, Handleable<VillagerType> {
public static class CraftType extends OldEnumHolderable<Type, VillagerType> implements Type {
private static int count = 0;
public static Type minecraftToBukkit(VillagerType minecraft) {
@ -172,7 +170,7 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
}
public static Type minecraftHolderToBukkit(Holder<VillagerType> minecraft) {
return CraftType.minecraftToBukkit(minecraft.value());
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registries.VILLAGER_TYPE);
}
public static VillagerType bukkitToMinecraft(Type bukkit) {
@ -183,77 +181,12 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.VILLAGER_TYPE);
}
private final NamespacedKey key;
private final VillagerType villagerType;
private final String name;
private final int ordinal;
public CraftType(NamespacedKey key, VillagerType villagerType) {
this.key = key;
this.villagerType = villagerType;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive type specific values.
// Custom types will return the key with namespace. For a plugin this should look than like a new type
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT);
} else {
this.name = key.toString();
}
this.ordinal = CraftType.count++;
}
@Override
public VillagerType getHandle() {
return this.villagerType;
}
@Override
public NamespacedKey getKey() {
return this.key;
}
@Override
public int compareTo(Type type) {
return this.ordinal - type.ordinal();
}
@Override
public String name() {
return this.name;
}
@Override
public int ordinal() {
return this.ordinal;
}
@Override
public String toString() {
// For backwards compatibility
return this.name();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftType)) {
return false;
}
return this.getKey().equals(((Type) other).getKey());
}
@Override
public int hashCode() {
return this.getKey().hashCode();
public CraftType(final Holder<VillagerType> holder){
super(holder, count++);
}
}
public static class CraftProfession implements Profession, Handleable<VillagerProfession> {
public static class CraftProfession extends OldEnumHolderable<Profession, VillagerProfession> implements Profession {
private static int count = 0;
public static Profession minecraftHolderToBukkit(Holder<VillagerProfession> minecraft) {
@ -272,73 +205,8 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
private final VillagerProfession villagerProfession;
private final String name;
private final int ordinal;
public CraftProfession(NamespacedKey key, VillagerProfession villagerProfession) {
this.key = key;
this.villagerProfession = villagerProfession;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive profession specific values.
// Custom professions will return the key with namespace. For a plugin this should look than like a new profession
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT);
} else {
this.name = key.toString();
}
this.ordinal = CraftProfession.count++;
}
@Override
public VillagerProfession getHandle() {
return this.villagerProfession;
}
@Override
public NamespacedKey getKey() {
return this.key;
}
@Override
public int compareTo(Profession profession) {
return this.ordinal - profession.ordinal();
}
@Override
public String name() {
return this.name;
}
@Override
public int ordinal() {
return this.ordinal;
}
@Override
public String toString() {
// For backwards compatibility
return this.name();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftProfession)) {
return false;
}
return this.getKey().equals(((Profession) other).getKey());
}
@Override
public int hashCode() {
return this.getKey().hashCode();
public CraftProfession(final Holder<VillagerProfession> holder) {
super(holder, count++);
}
}

View File

@ -1,15 +1,14 @@
package org.bukkit.craftbukkit.entity;
import com.google.common.base.Preconditions;
import io.papermc.paper.registry.HolderableBase;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.animal.wolf.WolfSoundVariant;
import net.minecraft.world.entity.animal.wolf.WolfVariant;
import org.bukkit.DyeColor;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.entity.Wolf;
public class CraftWolf extends CraftTameableAnimal implements Wolf {
@ -91,14 +90,14 @@ public class CraftWolf extends CraftTameableAnimal implements Wolf {
this.getHandle().setSoundVariant(CraftSoundVariant.bukkitToMinecraftHolder(soundVariant));
}
public static class CraftVariant implements Variant, Handleable<WolfVariant> {
public static class CraftVariant extends HolderableBase<WolfVariant> implements Variant {
public static Variant minecraftToBukkit(WolfVariant minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.WOLF_VARIANT);
}
public static Variant minecraftHolderToBukkit(Holder<WolfVariant> minecraft) {
return CraftVariant.minecraftToBukkit(minecraft.value());
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registries.WOLF_VARIANT);
}
public static WolfVariant bukkitToMinecraft(Variant bukkit) {
@ -106,68 +105,22 @@ public class CraftWolf extends CraftTameableAnimal implements Wolf {
}
public static Holder<WolfVariant> bukkitToMinecraftHolder(Variant bukkit) {
Preconditions.checkArgument(bukkit != null);
net.minecraft.core.Registry<WolfVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.WOLF_VARIANT);
if (registry.wrapAsHolder(CraftVariant.bukkitToMinecraft(bukkit)) instanceof Holder.Reference<WolfVariant> holder) {
return holder;
}
throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own wolf variant with out properly registering it.");
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.WOLF_VARIANT);
}
private final NamespacedKey key;
private final WolfVariant variant;
public CraftVariant(NamespacedKey key, WolfVariant variant) {
this.key = key;
this.variant = variant;
}
@Override
public WolfVariant getHandle() {
return this.variant;
}
@Override
public NamespacedKey getKey() {
return this.key;
}
@Override
public String toString() {
return this.key.toString();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftVariant otherVariant)) {
return false;
}
return this.getKey().equals(otherVariant.getKey());
}
@Override
public int hashCode() {
return this.getKey().hashCode();
public CraftVariant(final Holder<WolfVariant> holder) {
super(holder);
}
}
public static class CraftSoundVariant implements SoundVariant, Handleable<WolfSoundVariant> {
public static class CraftSoundVariant extends HolderableBase<WolfSoundVariant> implements SoundVariant {
public static SoundVariant minecraftToBukkit(WolfSoundVariant minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.WOLF_SOUND_VARIANT);
}
public static SoundVariant minecraftHolderToBukkit(Holder<WolfSoundVariant> minecraft) {
return CraftSoundVariant.minecraftToBukkit(minecraft.value());
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registries.WOLF_SOUND_VARIANT);
}
public static WolfSoundVariant bukkitToMinecraft(SoundVariant bukkit) {
@ -175,57 +128,11 @@ public class CraftWolf extends CraftTameableAnimal implements Wolf {
}
public static Holder<WolfSoundVariant> bukkitToMinecraftHolder(SoundVariant bukkit) {
Preconditions.checkArgument(bukkit != null);
net.minecraft.core.Registry<WolfSoundVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.WOLF_SOUND_VARIANT);
if (registry.wrapAsHolder(CraftSoundVariant.bukkitToMinecraft(bukkit)) instanceof Holder.Reference<WolfSoundVariant> holder) {
return holder;
}
throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own wolf sound variant with out properly registering it.");
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.WOLF_SOUND_VARIANT);
}
private final NamespacedKey key;
private final WolfSoundVariant soundVariant;
public CraftSoundVariant(NamespacedKey key, WolfSoundVariant soundVariant) {
this.key = key;
this.soundVariant = soundVariant;
}
@Override
public WolfSoundVariant getHandle() {
return this.soundVariant;
}
@Override
public NamespacedKey getKey() {
return this.key;
}
@Override
public String toString() {
return this.key.toString();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftSoundVariant otherVariant)) {
return false;
}
return this.getKey().equals(otherVariant.getKey());
}
@Override
public int hashCode() {
return this.getKey().hashCode();
public CraftSoundVariant(final Holder<WolfSoundVariant> holder) {
super(holder);
}
}
}

View File

@ -1,15 +1,14 @@
package org.bukkit.craftbukkit.generator.structure;
import com.google.common.base.Suppliers;
import java.util.function.Supplier;
import io.papermc.paper.util.Holderable;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.generator.structure.Structure;
import org.bukkit.generator.structure.StructureType;
public class CraftStructure extends Structure implements Handleable<net.minecraft.world.level.levelgen.structure.Structure> {
public class CraftStructure extends Structure implements Holderable<net.minecraft.world.level.levelgen.structure.Structure> {
public static Structure minecraftToBukkit(net.minecraft.world.level.levelgen.structure.Structure minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.STRUCTURE);
@ -19,28 +18,39 @@ public class CraftStructure extends Structure implements Handleable<net.minecraf
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
private final net.minecraft.world.level.levelgen.structure.Structure structure;
private final Supplier<StructureType> structureType;
private final Holder<net.minecraft.world.level.levelgen.structure.Structure> holder;
public CraftStructure(NamespacedKey key, net.minecraft.world.level.levelgen.structure.Structure structure) {
this.key = key;
this.structure = structure;
this.structureType = Suppliers.memoize(() -> CraftStructureType.minecraftToBukkit(structure.type()));
public CraftStructure(Holder<net.minecraft.world.level.levelgen.structure.Structure> holder) {
this.holder = holder;
}
@Override
public net.minecraft.world.level.levelgen.structure.Structure getHandle() {
return this.structure;
public Holder<net.minecraft.world.level.levelgen.structure.Structure> getHolder() {
return this.holder;
}
@Override
public StructureType getStructureType() {
return this.structureType.get();
return CraftStructureType.minecraftToBukkit(this.getHandle().type());
}
@Override
public NamespacedKey getKey() {
return this.key;
return Holderable.super.getKey();
}
@Override
public int hashCode() {
return Holderable.super.implHashCode();
}
@Override
public boolean equals(final Object obj) {
return Holderable.super.implEquals(obj);
}
@Override
public String toString() {
return Holderable.super.implToString();
}
}

View File

@ -1,12 +1,13 @@
package org.bukkit.craftbukkit.generator.structure;
import io.papermc.paper.util.Holderable;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.generator.structure.StructureType;
public class CraftStructureType extends StructureType implements Handleable<net.minecraft.world.level.levelgen.structure.StructureType<?>> {
public class CraftStructureType extends StructureType implements Holderable<net.minecraft.world.level.levelgen.structure.StructureType<?>> {
public static StructureType minecraftToBukkit(net.minecraft.world.level.levelgen.structure.StructureType<?> minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.STRUCTURE_TYPE);
@ -16,21 +17,34 @@ public class CraftStructureType extends StructureType implements Handleable<net.
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
private final net.minecraft.world.level.levelgen.structure.StructureType<?> structureType;
private final Holder<net.minecraft.world.level.levelgen.structure.StructureType<?>> holder;
public CraftStructureType(NamespacedKey key, net.minecraft.world.level.levelgen.structure.StructureType<?> structureType) {
this.key = key;
this.structureType = structureType;
public CraftStructureType(final Holder<net.minecraft.world.level.levelgen.structure.StructureType<?>> holder) {
this.holder = holder;
}
@Override
public net.minecraft.world.level.levelgen.structure.StructureType<?> getHandle() {
return this.structureType;
public Holder<net.minecraft.world.level.levelgen.structure.StructureType<?>> getHolder() {
return this.holder;
}
@Override
public NamespacedKey getKey() {
return this.key;
return Holderable.super.getKey();
}
@Override
public int hashCode() {
return Holderable.super.implHashCode();
}
@Override
public boolean equals(final Object obj) {
return Holderable.super.implEquals(obj);
}
@Override
public String toString() {
return Holderable.super.implToString();
}
}

View File

@ -577,7 +577,7 @@ public final class CraftItemStack extends ItemStack {
}
private <A, V> void setDataInternal(final io.papermc.paper.datacomponent.PaperDataComponentType<A, V> type, final A value) {
this.handle.set(type.getHandle(), type.getAdapter().toVanilla(value));
this.handle.set(type.getHandle(), type.getAdapter().toVanilla(value, type.getHolder()));
}
@Override

View File

@ -1,17 +1,15 @@
package org.bukkit.craftbukkit.map;
import java.util.Locale;
import io.papermc.paper.util.OldEnumHolderable;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.saveddata.maps.MapDecorationType;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.map.MapCursor;
public final class CraftMapCursor {
public static final class CraftType implements MapCursor.Type, Handleable<MapDecorationType> {
public static final class CraftType extends OldEnumHolderable<MapCursor.Type, MapDecorationType> implements MapCursor.Type {
private static int count = 0;
@ -20,7 +18,7 @@ public final class CraftMapCursor {
}
public static MapCursor.Type minecraftHolderToBukkit(Holder<MapDecorationType> minecraft) {
return CraftType.minecraftToBukkit(minecraft.value());
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registries.MAP_DECORATION_TYPE);
}
public static MapDecorationType bukkitToMinecraft(MapCursor.Type bukkit) {
@ -31,73 +29,8 @@ public final class CraftMapCursor {
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.MAP_DECORATION_TYPE);
}
private final NamespacedKey key;
private final MapDecorationType mapDecorationType;
private final String name;
private final int ordinal;
public CraftType(NamespacedKey key, MapDecorationType mapDecorationType) {
this.key = key;
this.mapDecorationType = mapDecorationType;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive type specific values.
// Custom types will return the key with namespace. For a plugin this should look than like a new type
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT);
} else {
this.name = key.toString();
}
this.ordinal = CraftType.count++;
}
@Override
public MapDecorationType getHandle() {
return this.mapDecorationType;
}
@Override
public NamespacedKey getKey() {
return this.key;
}
@Override
public int compareTo(MapCursor.Type type) {
return this.ordinal - type.ordinal();
}
@Override
public String name() {
return this.name;
}
@Override
public int ordinal() {
return this.ordinal;
}
@Override
public String toString() {
// For backwards compatibility
return this.name();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftType)) {
return false;
}
return this.getKey().equals(((MapCursor.Type) other).getKey());
}
@Override
public int hashCode() {
return this.getKey().hashCode();
public CraftType(final Holder<MapDecorationType> holder) {
super(holder, count++);
}
@Override