Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package org.bukkit.inventory.meta;

import org.bukkit.entity.Axolotl;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.NullMarked;

/**
* Represents a bucket of axolotl.
*/
@NullMarked
public interface AxolotlBucketMeta extends ItemMeta {

/**
* Get the variant of the axolotl in the bucket.
* <p>
* Plugins should check that hasVariant() returns <code>true</code> before
* Plugins should check that hasVariant() returns {@code true} before
* calling this method.
*
* @return axolotl variant
* @throws IllegalStateException if hasVariant() returns {@code false}
*/
@NotNull
Axolotl.Variant getVariant();

/**
* Set the variant of this axolotl in the bucket.
*
* @param variant axolotl variant
*/
void setVariant(@NotNull Axolotl.Variant variant);
void setVariant(Axolotl.Variant variant);

/**
* Checks for existence of a variant tag indicating a specific axolotl will be
* Checks for the existence of a variant indicating a specific axolotl will be
* spawned.
*
* @return if there is a variant
*/
boolean hasVariant();

@Override
@NotNull
AxolotlBucketMeta clone();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,99 @@

import org.bukkit.DyeColor;
import org.bukkit.entity.TropicalFish;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.NullMarked;

/**
* Represents a bucket of tropical fish.
*/
@NullMarked
public interface TropicalFishBucketMeta extends ItemMeta {

/**
* Gets the color of the fish's pattern.
* <p>
* Plugins should check that hasVariant() returns <code>true</code> before
* Plugins should check that hasPatternColor() returns {@code true} before
* calling this method.
*
* @return pattern color
* @throws IllegalStateException if no pattern color is set
*/
@NotNull
DyeColor getPatternColor();

/**
* Sets the color of the fish's pattern.
* <p>
* Setting this when hasVariant() returns <code>false</code> will initialize
* all other values to unspecified defaults.
*
* @param color pattern color
*/
void setPatternColor(@NotNull DyeColor color);
void setPatternColor(DyeColor color);

/**
* Gets the color of the fish's body.
* <p>
* Plugins should check that hasVariant() returns <code>true</code> before
* Plugins should check that hasBodyColor() returns {@code true} before
* calling this method.
*
* @return pattern color
* @throws IllegalStateException if no body color is set
*/
@NotNull
DyeColor getBodyColor();

/**
* Sets the color of the fish's body.
* <p>
* Setting this when hasVariant() returns <code>false</code> will initialize
* all other values to unspecified defaults.
*
* @param color body color
*/
void setBodyColor(@NotNull DyeColor color);
void setBodyColor(DyeColor color);

/**
* Gets the fish's pattern.
* <p>
* Plugins should check that hasVariant() returns <code>true</code> before
* Plugins should check that hasPattern() returns {@code true} before
* calling this method.
*
* @return pattern
* @throws IllegalStateException if no pattern is set
*/
@NotNull
TropicalFish.Pattern getPattern();

/**
* Sets the fish's pattern.
* <p>
* Setting this when hasVariant() returns <code>false</code> will initialize
* all other values to unspecified defaults.
*
* @param pattern new pattern
*/
void setPattern(@NotNull TropicalFish.Pattern pattern);
void setPattern(TropicalFish.Pattern pattern);

/**
* Checks for the existence of a pattern.
*
* @return if there is a pattern
*/
boolean hasPattern();

/**
* Checks for the existence of a body color.
*
* @return if there is a body color
*/
boolean hasBodyColor();

/**
* Checks for the existence of a pattern color.
*
* @return if there is a pattern color
*/
boolean hasPatternColor();

/**
* Checks for existence of a variant tag indicating a specific fish will be
* Checks for the existence of a variant tag indicating a specific fish will be
* spawned.
*
* @return if there is a variant
* @deprecated the variant tag is no longer used and instead split into its own set of components
*/
@Deprecated(forRemoval = true, since = "1.21.10")
boolean hasVariant();

@Override
@NotNull
TropicalFishBucketMeta clone();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.entity.Axolotl;
import org.bukkit.inventory.meta.AxolotlBucketMeta;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
@DelegateDeserialization(SerializableMeta.class)
public class CraftMetaAxolotlBucket extends CraftMetaItem implements AxolotlBucketMeta {

@Deprecated
static final ItemMetaKey VARIANT = new ItemMetaKey("Variant", "axolotl-variant");

static final ItemMetaKeyType<TypedEntityData<EntityType<?>>> ENTITY_TAG = new ItemMetaKeyType<>(DataComponents.ENTITY_DATA, "entity-tag");
static final ItemMetaKeyType<CustomData> BUCKET_ENTITY_TAG = new ItemMetaKeyType<>(DataComponents.BUCKET_ENTITY_DATA, "bucket-entity-tag");

private Integer variant;
private CompoundTag entityTag;
private CompoundTag bucketEntityTag;
static final ItemMetaKeyType<net.minecraft.world.entity.animal.axolotl.Axolotl.Variant> AXOLOTL_VARIANT = new ItemMetaKeyType<>(DataComponents.AXOLOTL_VARIANT, "axolotl-variant");

private net.minecraft.world.entity.animal.axolotl.Axolotl.@Nullable Variant variant;
private @Nullable CompoundTag entityTag;
private @Nullable CompoundTag bucketEntityTag;

CraftMetaAxolotlBucket(CraftMetaItem meta) {
super(meta);
Expand All @@ -42,20 +49,29 @@ public class CraftMetaAxolotlBucket extends CraftMetaItem implements AxolotlBuck

getOrEmpty(tag, CraftMetaAxolotlBucket.ENTITY_TAG).ifPresent((nbt) -> {
this.entityTag = nbt.copyTagWithEntityId();
this.entityTag.getInt(CraftMetaAxolotlBucket.VARIANT.NBT).ifPresent(variant -> this.variant = variant);
this.entityTag.getInt(CraftMetaAxolotlBucket.VARIANT.NBT).ifPresent(variantId -> {
this.variant = net.minecraft.world.entity.animal.axolotl.Axolotl.Variant.byId(variantId);
});
});
getOrEmpty(tag, CraftMetaAxolotlBucket.BUCKET_ENTITY_TAG).ifPresent((nbt) -> {
this.bucketEntityTag = nbt.copyTag();
this.bucketEntityTag.getInt(CraftMetaAxolotlBucket.VARIANT.NBT).ifPresent(variant -> this.variant = variant);
this.bucketEntityTag.getInt(CraftMetaAxolotlBucket.VARIANT.NBT).ifPresent(variantId -> {
this.variant = net.minecraft.world.entity.animal.axolotl.Axolotl.Variant.byId(variantId);
});
});
getOrEmpty(tag, CraftMetaAxolotlBucket.AXOLOTL_VARIANT).ifPresent((variant) -> {
this.variant = variant;
});
}

CraftMetaAxolotlBucket(Map<String, Object> map) {
super(map);

Integer variant = SerializableMeta.getObject(Integer.class, map, CraftMetaAxolotlBucket.VARIANT.BUKKIT, true);
if (variant != null) {
this.variant = variant;
Object variant = SerializableMeta.getObject(Object.class, map, CraftMetaAxolotlBucket.AXOLOTL_VARIANT.BUKKIT, true);
if (variant instanceof String variantName) {
this.variant = net.minecraft.world.entity.animal.axolotl.Axolotl.Variant.valueOf(variantName);
} else if (variant instanceof Integer variantId) { // legacy
this.variant = net.minecraft.world.entity.animal.axolotl.Axolotl.Variant.byId(variantId);
}
}

Expand Down Expand Up @@ -85,16 +101,12 @@ void applyToItem(CraftMetaItem.Applicator tag) {
tag.put(CraftMetaAxolotlBucket.ENTITY_TAG, TypedEntityData.decodeEntity(this.entityTag));
}

CompoundTag bucketEntityTag = (this.bucketEntityTag != null) ? this.bucketEntityTag.copy() : null;
if (this.hasVariant()) {
if (bucketEntityTag == null) {
bucketEntityTag = new CompoundTag();
}
bucketEntityTag.putInt(CraftMetaAxolotlBucket.VARIANT.NBT, this.variant);
if (this.bucketEntityTag != null) {
tag.put(CraftMetaAxolotlBucket.BUCKET_ENTITY_TAG, CustomData.of(this.bucketEntityTag));
}

if (bucketEntityTag != null) {
tag.put(CraftMetaAxolotlBucket.BUCKET_ENTITY_TAG, CustomData.of(bucketEntityTag));
if (this.variant != null) {
tag.put(CraftMetaAxolotlBucket.AXOLOTL_VARIANT, this.variant);
}
}

Expand All @@ -110,13 +122,13 @@ boolean isBucketEmpty() {
@Override
public Axolotl.Variant getVariant() {
com.google.common.base.Preconditions.checkState(this.hasVariant(), "Variant is absent, check hasVariant first!");
return Axolotl.Variant.values()[this.variant];
return Axolotl.Variant.values()[this.variant.ordinal()];
}

@Override
public void setVariant(Axolotl.Variant variant) {
com.google.common.base.Preconditions.checkArgument(variant != null, "Variant cannot be null!");
this.variant = variant.ordinal();
this.variant = net.minecraft.world.entity.animal.axolotl.Axolotl.Variant.byId(variant.ordinal());
}

@Override
Expand Down Expand Up @@ -147,8 +159,8 @@ int applyHash() {
final int original;
int hash = original = super.applyHash();

if (this.hasVariant()) {
hash = 61 * hash + this.variant;
if (this.variant != null) {
hash = 61 * hash + this.variant.hashCode();
}
if (this.entityTag != null) {
hash = 61 * hash + this.entityTag.hashCode();
Expand All @@ -170,6 +182,7 @@ public CraftMetaAxolotlBucket clone() {
if (this.bucketEntityTag != null) {
clone.bucketEntityTag = this.bucketEntityTag.copy();
}
clone.variant = this.variant;

return clone;
}
Expand All @@ -178,8 +191,8 @@ public CraftMetaAxolotlBucket clone() {
ImmutableMap.Builder<String, Object> serialize(ImmutableMap.Builder<String, Object> builder) {
super.serialize(builder);

if (this.hasVariant()) {
builder.put(CraftMetaAxolotlBucket.VARIANT.BUKKIT, this.variant);
if (this.variant != null) {
builder.put(CraftMetaAxolotlBucket.AXOLOTL_VARIANT.BUKKIT, this.variant.name());
}

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2436,7 +2436,7 @@ public static Set<DataComponentType<?>> getTopLevelHandledDcts(final Class<? ext
final Map<Class<? extends CraftMetaItem>, Set<DataComponentType<?>>> map = new HashMap<>();
map.put(CraftMetaArmor.class, Set.of(CraftMetaArmor.TRIM.TYPE));
map.put(CraftMetaArmorStand.class, Set.of(CraftMetaArmorStand.ENTITY_TAG.TYPE));
map.put(CraftMetaAxolotlBucket.class, Set.of(CraftMetaAxolotlBucket.ENTITY_TAG.TYPE, CraftMetaAxolotlBucket.BUCKET_ENTITY_TAG.TYPE));
map.put(CraftMetaAxolotlBucket.class, Set.of(CraftMetaAxolotlBucket.ENTITY_TAG.TYPE, CraftMetaAxolotlBucket.BUCKET_ENTITY_TAG.TYPE, CraftMetaAxolotlBucket.AXOLOTL_VARIANT.TYPE));
map.put(CraftMetaBanner.class, Set.of(CraftMetaBanner.PATTERNS.TYPE)); // banner uses same tag as block state
map.put(CraftMetaShield.class, Set.of(CraftMetaShield.BASE_COLOR.TYPE, CraftMetaBanner.PATTERNS.TYPE));
map.put(CraftMetaBlockState.class, Set.of(CraftMetaBlockState.BLOCK_ENTITY_TAG.TYPE));
Expand All @@ -2459,7 +2459,7 @@ public static Set<DataComponentType<?>> getTopLevelHandledDcts(final Class<? ext
map.put(CraftMetaSkull.class, Set.of(CraftMetaSkull.SKULL_PROFILE.TYPE, CraftMetaSkull.NOTE_BLOCK_SOUND.TYPE));
map.put(CraftMetaSpawnEgg.class, Set.of(CraftMetaSpawnEgg.ENTITY_TAG.TYPE));
map.put(CraftMetaSuspiciousStew.class, Set.of(CraftMetaSuspiciousStew.EFFECTS.TYPE));
map.put(CraftMetaTropicalFishBucket.class, Set.of(CraftMetaTropicalFishBucket.ENTITY_TAG.TYPE, CraftMetaTropicalFishBucket.BUCKET_ENTITY_TAG.TYPE));
map.put(CraftMetaTropicalFishBucket.class, Set.of(CraftMetaTropicalFishBucket.ENTITY_TAG.TYPE, CraftMetaTropicalFishBucket.BUCKET_ENTITY_TAG.TYPE, CraftMetaTropicalFishBucket.PATTERN.TYPE, CraftMetaTropicalFishBucket.PATTERN_COLOR.TYPE, CraftMetaTropicalFishBucket.BASE_COLOR.TYPE));

for (final Map.Entry<Class<? extends CraftMetaItem>, Set<DataComponentType<?>>> entry : map.entrySet()) {
final ArrayList<DataComponentType<?>> topLevelTags = new ArrayList<>(entry.getValue());
Expand Down
Loading
Loading