Skip to content

Commit 6a08bca

Browse files
committed
finish TagKey -> HolderSet migration
1 parent 062aadc commit 6a08bca

File tree

7 files changed

+153
-48
lines changed

7 files changed

+153
-48
lines changed

paper-api/src/main/java/io/papermc/paper/datacomponent/item/DamageResistant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static DamageResistant damageResistant(final RegistryKeySet<DamageType> types) {
2323
/**
2424
* The types that this damage type is invincible to.
2525
*
26-
* @return the key of the tag holding the respective damage types.
26+
* @return the registry key set holding the respective damage types.
2727
*/
2828
@Contract(value = "-> new", pure = true)
2929
RegistryKeySet<DamageType> types();

paper-api/src/main/java/org/bukkit/inventory/meta/ItemMeta.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.bukkit.inventory.meta;
22

33
import com.google.common.collect.Multimap;
4+
import io.papermc.paper.datacomponent.DataComponentType;
5+
import io.papermc.paper.datacomponent.DataComponentTypes;
6+
import io.papermc.paper.datacomponent.item.ItemAdventurePredicate;
7+
import io.papermc.paper.registry.keys.tags.DamageTypeTagKeys;
8+
import io.papermc.paper.registry.set.RegistryKeySet;
49
import java.util.Collection;
510
import java.util.List;
611
import java.util.Map;
712
import java.util.Set;
8-
import io.papermc.paper.datacomponent.DataComponentType;
9-
import io.papermc.paper.datacomponent.DataComponentTypes;
10-
import io.papermc.paper.datacomponent.item.ItemAdventurePredicate;
1113
import net.kyori.adventure.text.Component;
1214
import org.bukkit.NamespacedKey;
1315
import org.bukkit.Tag;
@@ -29,7 +31,6 @@
2931
import org.bukkit.inventory.meta.components.UseCooldownComponent;
3032
import org.bukkit.inventory.meta.tags.CustomItemTagContainer;
3133
import org.bukkit.persistence.PersistentDataHolder;
32-
import org.bukkit.tag.DamageTypeTags;
3334
import org.jetbrains.annotations.ApiStatus;
3435
import org.jetbrains.annotations.NotNull;
3536
import org.jetbrains.annotations.Nullable;
@@ -634,8 +635,7 @@ default void displayName(final net.kyori.adventure.text.@Nullable Component disp
634635
* or lava.
635636
*
636637
* @return fire_resistant
637-
* @deprecated use {@link #getDamageResistant()} and
638-
* {@link DamageTypeTags#IS_FIRE}
638+
* @deprecated use {@link #getDamageResistance()} and check if it matches any {@link DamageTypeTagKeys#IS_FIRE fire damage type}
639639
*/
640640
@Deprecated(since = "1.21.2")
641641
boolean isFireResistant();
@@ -645,8 +645,7 @@ default void displayName(final net.kyori.adventure.text.@Nullable Component disp
645645
* or lava.
646646
*
647647
* @param fireResistant fire_resistant
648-
* @deprecated use {@link #setDamageResistant(org.bukkit.Tag)} and
649-
* {@link DamageTypeTags#IS_FIRE}
648+
* @deprecated use {@link #setDamageResistance(RegistryKeySet)} with {@link DamageTypeTagKeys#IS_FIRE}
650649
*/
651650
@Deprecated(since = "1.21.2")
652651
void setFireResistant(boolean fireResistant);
@@ -662,22 +661,39 @@ default void displayName(final net.kyori.adventure.text.@Nullable Component disp
662661
* Gets the type of damage this item will be resistant to when in entity
663662
* form.
664663
*
665-
* Plugins should check {@link #hasDamageResistant()} before calling this
666-
* method.
667-
*
668-
* @return damage type
664+
* @return damage type tag
665+
* @deprecated use {@link #getDamageResistance()}
669666
*/
670667
@Nullable
668+
@Deprecated(since = "26.1")
671669
Tag<DamageType> getDamageResistant();
672670

673671
/**
674672
* Sets the type of damage this item will be resistant to when in entity
675673
* form.
676674
*
677675
* @param tag the tag, or null to clear
676+
* @deprecated use {@link #setDamageResistance(RegistryKeySet)}
678677
*/
678+
@Deprecated(since = "26.1")
679679
void setDamageResistant(@Nullable Tag<DamageType> tag);
680680

681+
/**
682+
* Gets the type of damage this item will be resistant to when in entity
683+
* form.
684+
*
685+
* @return the registry key set holding the respective damage types.
686+
*/
687+
@Nullable RegistryKeySet<DamageType> getDamageResistance();
688+
689+
/**
690+
* Sets the type of damage this item will be resistant to when in entity
691+
* form.
692+
*
693+
* @param types the registry key set, or null to clear
694+
*/
695+
void setDamageResistance(@Nullable RegistryKeySet<DamageType> types);
696+
681697
/**
682698
* Gets if the max_stack_size is set.
683699
*
@@ -1039,9 +1055,9 @@ default void displayName(final net.kyori.adventure.text.@Nullable Component disp
10391055
* assert itemStack.isSimilar(recreatedItemStack); // Should be true*
10401056
* </pre>
10411057
* <p>
1042-
* *Components not represented or explicitly overridden by this ItemMeta instance
1043-
* will not be included in the resulting string and therefore may result in ItemStacks
1044-
* that do not match <em>exactly</em>. For example, if {@link #setDisplayName(String)}
1058+
* *Components not represented or explicitly overridden by this ItemMeta instance and
1059+
* transient components will not be included in the resulting string and therefore may
1060+
* result in ItemStacks that do not match <em>exactly</em>. For example, if {@link #setDisplayName(String)}
10451061
* is not set, then the custom name component will not be included. Or if this ItemMeta
10461062
* is a PotionMeta, it will not include any components related to lodestone compasses,
10471063
* banners, or books, etc., only components modifiable by a PotionMeta instance.

paper-api/src/main/java/org/bukkit/inventory/meta/components/EquippableComponent.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,32 @@ public interface EquippableComponent extends ConfigurationSerializable {
164164
* @param equip whether the item equips on interact
165165
*/
166166
void setEquipOnInteract(boolean equip);
167+
168+
/**
169+
* Checks if the item can be unequipped when interacting with an entity using shears.
170+
*
171+
* @return whether the item can be unequipped using shears
172+
*/
173+
boolean canBeSheared();
174+
175+
/**
176+
* Sets if the item can be unequipped when interacting with an entity using shears.
177+
*
178+
* @param sheared whether the item can be unequipped using shears
179+
*/
180+
void setCanBeSheared(boolean sheared);
181+
182+
/**
183+
* Gets the sound to play when the item is sheared.
184+
*
185+
* @return the sound
186+
*/
187+
@Nullable Sound getShearingSound();
188+
189+
/**
190+
* Sets the sound to play when the item is sheared.
191+
*
192+
* @param sound sound or null for current default
193+
*/
194+
void setShearingSound(@Nullable Sound sound);
167195
}

paper-server/src/main/java/io/papermc/paper/datacomponent/item/PaperBlocksAttacks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Builder itemDamage(final ItemDamageFunction function) {
106106
}
107107

108108
@Override
109-
public Builder bypassedBy(@Nullable final RegistryKeySet<DamageType> bypassedBy) {
109+
public Builder bypassedBy(final @Nullable RegistryKeySet<DamageType> bypassedBy) {
110110
this.bypassedBy = bypassedBy;
111111
return this;
112112
}

paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
import com.google.common.collect.ImmutableList;
66
import com.google.common.collect.ImmutableMap;
77
import com.google.common.collect.ImmutableMultimap;
8-
import com.google.common.collect.LinkedHashMultimap;
98
import com.google.common.collect.ImmutableSortedMap;
9+
import com.google.common.collect.LinkedHashMultimap;
1010
import com.google.common.collect.Lists;
1111
import com.google.common.collect.Multimap;
1212
import com.google.common.collect.SetMultimap;
1313
import com.google.common.collect.Sets;
1414
import com.mojang.logging.LogUtils;
1515
import com.mojang.serialization.DynamicOps;
16+
import io.papermc.paper.registry.RegistryKey;
17+
import io.papermc.paper.registry.data.util.Conversions;
18+
import io.papermc.paper.registry.set.PaperRegistrySets;
19+
import io.papermc.paper.registry.set.RegistryKeySet;
20+
import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet;
1621
import java.io.ByteArrayInputStream;
1722
import java.io.ByteArrayOutputStream;
1823
import java.io.IOException;
@@ -37,7 +42,6 @@
3742
import java.util.Optional;
3843
import java.util.Set;
3944
import java.util.StringJoiner;
40-
import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet;
4145
import javax.annotation.Nonnull;
4246
import javax.annotation.Nullable;
4347
import net.minecraft.core.Holder;
@@ -47,6 +51,7 @@
4751
import net.minecraft.core.component.DataComponentPatch;
4852
import net.minecraft.core.component.DataComponentType;
4953
import net.minecraft.core.component.DataComponents;
54+
import net.minecraft.core.registries.BuiltInRegistries;
5055
import net.minecraft.core.registries.Registries;
5156
import net.minecraft.nbt.CompoundTag;
5257
import net.minecraft.nbt.ListTag;
@@ -131,6 +136,8 @@
131136
import org.bukkit.tag.DamageTypeTags;
132137
import org.slf4j.Logger;
133138

139+
import static java.util.Objects.requireNonNull;
140+
134141
/**
135142
* Children must include the following:
136143
*
@@ -1550,7 +1557,11 @@ public boolean isFireResistant() {
15501557

15511558
@Override
15521559
public void setFireResistant(boolean fireResistant) {
1553-
this.damageResistant = CraftRegistry.getMinecraftRegistry(Registries.DAMAGE_TYPE).get(net.minecraft.tags.DamageTypeTags.IS_FIRE).orElseThrow(IllegalStateException::new);
1560+
if (fireResistant) {
1561+
this.damageResistant = CraftRegistry.getMinecraftRegistry(Registries.DAMAGE_TYPE).getOrThrow(net.minecraft.tags.DamageTypeTags.IS_FIRE);
1562+
} else if (this.isFireResistant()) {
1563+
this.damageResistant = null;
1564+
}
15541565
}
15551566

15561567
@Override
@@ -1560,7 +1571,6 @@ public boolean hasDamageResistant() {
15601571

15611572
@Override
15621573
public Tag<DamageType> getDamageResistant() {
1563-
// TODO - snapshot - 26.1 API changes for holder set
15641574
return this.hasDamageResistant() && this.damageResistant.unwrapKey().isPresent() ? Bukkit.getTag(DamageTypeTags.REGISTRY_DAMAGE_TYPES, CraftNamespacedKey.fromMinecraft(this.damageResistant.unwrapKey().get().location()), DamageType.class) : null;
15651575
}
15661576

@@ -1569,6 +1579,20 @@ public void setDamageResistant(Tag<DamageType> tag) {
15691579
this.damageResistant = (tag != null) ? CraftRegistry.getMinecraftRegistry(Registries.DAMAGE_TYPE).get(((CraftDamageTag) tag).getHandle().key()).orElseThrow(IllegalStateException::new) : null;
15701580
}
15711581

1582+
@Override
1583+
public RegistryKeySet<DamageType> getDamageResistance() {
1584+
return this.damageResistant == null ? null : PaperRegistrySets.convertToApi(RegistryKey.DAMAGE_TYPE, this.damageResistant);
1585+
}
1586+
1587+
@Override
1588+
public void setDamageResistance(RegistryKeySet<DamageType> types) {
1589+
if (types == null) {
1590+
this.damageResistant = null;
1591+
} else {
1592+
this.damageResistant = PaperRegistrySets.convertToNms(Registries.DAMAGE_TYPE, Conversions.global().lookup(), types);
1593+
}
1594+
}
1595+
15721596
@Override
15731597
public boolean hasMaxStackSize() {
15741598
return this.maxStackSize != null;
@@ -1836,18 +1860,20 @@ public String getAsComponentString() {
18361860
this.applyToItem(tag);
18371861
DataComponentPatch patch = tag.build();
18381862

1839-
RegistryAccess registryAccess = CraftRegistry.getMinecraftRegistry();
1840-
DynamicOps<net.minecraft.nbt.Tag> ops = registryAccess.createSerializationContext(NbtOps.INSTANCE);
1841-
Registry<DataComponentType<?>> componentTypeRegistry = registryAccess.lookupOrThrow(Registries.DATA_COMPONENT_TYPE);
1842-
1863+
DynamicOps<net.minecraft.nbt.Tag> ops = CraftRegistry.getMinecraftRegistry().createSerializationContext(NbtOps.INSTANCE);
18431864
StringJoiner componentString = new StringJoiner(",", "[", "]");
1865+
18441866
for (Entry<DataComponentType<?>, Optional<?>> entry : patch.entrySet()) {
1845-
DataComponentType<?> componentType = entry.getKey();
1867+
DataComponentType<?> type = entry.getKey();
1868+
if (type.isTransient()) {
1869+
continue;
1870+
}
1871+
18461872
Optional<?> componentValue = entry.getValue();
1847-
String componentKey = componentTypeRegistry.getResourceKey(componentType).orElseThrow().identifier().toString();
1873+
String componentKey = requireNonNull(BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(type)).toString();
18481874

18491875
if (componentValue.isPresent()) {
1850-
net.minecraft.nbt.Tag componentValueAsNBT = (net.minecraft.nbt.Tag) ((DataComponentType) componentType).codecOrThrow().encodeStart(ops, componentValue.get()).getOrThrow();
1876+
net.minecraft.nbt.Tag componentValueAsNBT = (net.minecraft.nbt.Tag) ((DataComponentType) type).codecOrThrow().encodeStart(ops, componentValue.get()).getOrThrow();
18511877
String componentValueAsNBTString = new SnbtPrinterTagVisitor("", 0, new ArrayList<>()).visit(componentValueAsNBT);
18521878
componentString.add(componentKey + "=" + componentValueAsNBTString);
18531879
} else {
@@ -2564,7 +2590,7 @@ private static List<net.minecraft.advancements.criterion.BlockPredicate> convert
25642590
final net.minecraft.core.Registry<net.minecraft.world.level.block.Block> blockRegistry = CraftRegistry.getMinecraftRegistry().lookupOrThrow(net.minecraft.core.registries.Registries.BLOCK);
25652591
for (final com.destroystokyo.paper.Namespaced namespaced : namespaceds) {
25662592
if (namespaced instanceof final org.bukkit.NamespacedKey key) {
2567-
predicates.add(net.minecraft.advancements.criterion.BlockPredicate.Builder.block().of(blockRegistry, CraftBlockType.bukkitToMinecraft(Objects.requireNonNull(org.bukkit.Registry.MATERIAL.get(key)))).build());
2593+
predicates.add(net.minecraft.advancements.criterion.BlockPredicate.Builder.block().of(blockRegistry, CraftBlockType.bukkitToMinecraft(requireNonNull(org.bukkit.Registry.MATERIAL.get(key)))).build());
25682594
} else if (namespaced instanceof final com.destroystokyo.paper.NamespacedTag tag) {
25692595
predicates.add(net.minecraft.advancements.criterion.BlockPredicate.Builder.block().of(blockRegistry, net.minecraft.tags.TagKey.create(Registries.BLOCK, Identifier.fromNamespaceAndPath(tag.getNamespace(), tag.getKey()))).build());
25702596
}

0 commit comments

Comments
 (0)