| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import java.util.Arrays;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.function.Consumer;
|
| 14 | import net.fabricmc.fabric.api.item.v1.FabricItem;
|
| 15 | import net.minecraft.SharedConstants;
|
| 16 | import net.minecraft.core.BlockPos;
|
| 17 | import net.minecraft.core.Holder;
|
| 18 | import net.minecraft.core.HolderGetter;
|
| 19 | import net.minecraft.core.HolderLookup;
|
| 20 | import net.minecraft.core.HolderSet;
|
| 21 | import net.minecraft.core.component.BlockTransformer;
|
| 22 | import net.minecraft.core.component.DataComponentInitializers;
|
| 23 | import net.minecraft.core.component.DataComponentMap;
|
| 24 | import net.minecraft.core.component.DataComponentType;
|
| 25 | import net.minecraft.core.component.DataComponents;
|
| 26 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 27 | import net.minecraft.core.registries.Registries;
|
| 28 | import net.minecraft.nbt.CompoundTag;
|
| 29 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 30 | import net.minecraft.network.chat.CommonComponents;
|
| 31 | import net.minecraft.network.chat.Component;
|
| 32 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 33 | import net.minecraft.network.codec.StreamCodec;
|
| 34 | import net.minecraft.resources.DependantName;
|
| 35 | import net.minecraft.resources.Identifier;
|
| 36 | import net.minecraft.resources.ResourceKey;
|
| 37 | import net.minecraft.server.level.ServerLevel;
|
| 38 | import net.minecraft.sounds.SoundEvents;
|
| 39 | import net.minecraft.tags.BlockTags;
|
| 40 | import net.minecraft.tags.DamageTypeTags;
|
| 41 | import net.minecraft.tags.EntityTypeTags;
|
| 42 | import net.minecraft.tags.TagKey;
|
| 43 | import net.minecraft.util.Mth;
|
| 44 | import net.minecraft.util.Util;
|
| 45 | import net.minecraft.world.Difficulty;
|
| 46 | import net.minecraft.world.InteractionHand;
|
| 47 | import net.minecraft.world.InteractionResult;
|
| 48 | import net.minecraft.world.damagesource.DamageSource;
|
| 49 | import net.minecraft.world.damagesource.DamageTypes;
|
| 50 | import net.minecraft.world.entity.Entity;
|
| 51 | import net.minecraft.world.entity.EntityType;
|
| 52 | import net.minecraft.world.entity.EntityTypes;
|
| 53 | import net.minecraft.world.entity.EquipmentSlot;
|
| 54 | import net.minecraft.world.entity.EquipmentSlotGroup;
|
| 55 | import net.minecraft.world.entity.LivingEntity;
|
| 56 | import net.minecraft.world.entity.SlotAccess;
|
| 57 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 58 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 59 | import net.minecraft.world.entity.item.ItemEntity;
|
| 60 | import net.minecraft.world.entity.player.Player;
|
| 61 | import net.minecraft.world.flag.FeatureElement;
|
| 62 | import net.minecraft.world.flag.FeatureFlag;
|
| 63 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 64 | import net.minecraft.world.flag.FeatureFlags;
|
| 65 | import net.minecraft.world.food.FoodProperties;
|
| 66 | import net.minecraft.world.food.VillagerFood;
|
| 67 | import net.minecraft.world.inventory.ClickAction;
|
| 68 | import net.minecraft.world.inventory.Slot;
|
| 69 | import net.minecraft.world.inventory.tooltip.TooltipComponent;
|
| 70 | import net.minecraft.world.item.component.AttackRange;
|
| 71 | import net.minecraft.world.item.component.BlockTransformers;
|
| 72 | import net.minecraft.world.item.component.BrewingFuel;
|
| 73 | import net.minecraft.world.item.component.Compostable;
|
| 74 | import net.minecraft.world.item.component.Consumable;
|
| 75 | import net.minecraft.world.item.component.Consumables;
|
| 76 | import net.minecraft.world.item.component.CookingFuel;
|
| 77 | import net.minecraft.world.item.component.DamageResistant;
|
| 78 | import net.minecraft.world.item.component.ItemAttributeModifiers;
|
| 79 | import net.minecraft.world.item.component.KineticWeapon;
|
| 80 | import net.minecraft.world.item.component.MobVisibility;
|
| 81 | import net.minecraft.world.item.component.PiercingWeapon;
|
| 82 | import net.minecraft.world.item.component.SwingAnimation;
|
| 83 | import net.minecraft.world.item.component.Tool;
|
| 84 | import net.minecraft.world.item.component.TooltipDisplay;
|
| 85 | import net.minecraft.world.item.component.TypedEntityData;
|
| 86 | import net.minecraft.world.item.component.UseCooldown;
|
| 87 | import net.minecraft.world.item.component.UseEffects;
|
| 88 | import net.minecraft.world.item.component.UseRemainder;
|
| 89 | import net.minecraft.world.item.component.Weapon;
|
| 90 | import net.minecraft.world.item.context.UseOnContext;
|
| 91 | import net.minecraft.world.item.enchantment.Enchantable;
|
| 92 | import net.minecraft.world.item.enchantment.Repairable;
|
| 93 | import net.minecraft.world.item.equipment.ArmorMaterial;
|
| 94 | import net.minecraft.world.item.equipment.ArmorType;
|
| 95 | import net.minecraft.world.item.equipment.Equippable;
|
| 96 | import net.minecraft.world.item.equipment.trim.TrimMaterial;
|
| 97 | import net.minecraft.world.level.ClipContext;
|
| 98 | import net.minecraft.world.level.ItemLike;
|
| 99 | import net.minecraft.world.level.Level;
|
| 100 | import net.minecraft.world.level.block.Block;
|
| 101 | import net.minecraft.world.level.block.entity.DecoratedPotPattern;
|
| 102 | import net.minecraft.world.level.block.entity.SignText;
|
| 103 | import net.minecraft.world.level.block.state.BlockState;
|
| 104 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 105 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 106 | import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProviders;
|
| 107 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProvider;
|
| 108 | import net.minecraft.world.phys.BlockHitResult;
|
| 109 | import net.minecraft.world.phys.Vec3;
|
| 110 | import org.jspecify.annotations.Nullable;
|
| 111 | import org.slf4j.Logger;
|
| 112 |
|
| 113 | public class Item implements ItemLike, FeatureElement, FabricItem {
|
| 114 | public static final Codec<Holder<Item>> CODEC = BuiltInRegistries.ITEM
|
| 115 | .holderByNameCodec()
|
| 116 | .validate(item -> item.is(Items.AIR.builtInRegistryHolder()) ? DataResult.error(() -> "Item must not be minecraft:air") : DataResult.success(item));
|
| 117 | public static final StreamCodec<RegistryFriendlyByteBuf, Holder<Item>> STREAM_CODEC = ByteBufCodecs.holderRegistry(Registries.ITEM);
|
| 118 | public static final Codec<Holder<Item>> CODEC_WITH_BOUND_COMPONENTS = CODEC.validate(
|
| 119 | item -> !item.areComponentsBound() ? DataResult.error(() -> "Item " + item.getRegisteredName() + " does not have components yet") : DataResult.success(item)
|
| 120 | );
|
| 121 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 122 | public static final Map<Block, Item> BY_BLOCK = Maps.newHashMap();
|
| 123 | public static final Identifier BASE_ATTACK_DAMAGE_ID = Identifier.withDefaultNamespace("base_attack_damage");
|
| 124 | public static final Identifier BASE_ATTACK_SPEED_ID = Identifier.withDefaultNamespace("base_attack_speed");
|
| 125 | public static final int DEFAULT_MAX_STACK_SIZE = 64;
|
| 126 | public static final int ABSOLUTE_MAX_STACK_SIZE = 99;
|
| 127 | public static final int MAX_BAR_WIDTH = 13;
|
| 128 | protected static final int APPROXIMATELY_INFINITE_USE_DURATION = 72000;
|
| 129 | private final Holder.Reference<Item> builtInRegistryHolder = BuiltInRegistries.ITEM.createIntrusiveHolder(this);
|
| 130 | private final @Nullable ItemStackTemplate craftingRemainingItem;
|
| 131 | protected final String descriptionId;
|
| 132 | private final FeatureFlagSet requiredFeatures;
|
| 133 |
|
| 134 | public static int getId(final Item item) {
|
| 135 | return item == null ? 0 : BuiltInRegistries.ITEM.getId(item);
|
| 136 | }
|
| 137 |
|
| 138 | public static Item byId(final int id) {
|
| 139 | return BuiltInRegistries.ITEM.byId(id);
|
| 140 | }
|
| 141 |
|
| 142 | @Deprecated
|
| 143 | public static Item byBlock(final Block block) {
|
| 144 | return BY_BLOCK.getOrDefault(block, Items.AIR);
|
| 145 | }
|
| 146 |
|
| 147 | public Item(final Item.Properties properties) {
|
| 148 | this.descriptionId = properties.effectiveDescriptionId();
|
| 149 | DataComponentInitializers.Initializer<Item> componentInitializer = properties.finalizeInitializer(
|
| 150 | Component.translatable(this.descriptionId), properties.effectiveModel()
|
| 151 | );
|
| 152 | BuiltInRegistries.DATA_COMPONENT_INITIALIZERS.add(properties.itemIdOrThrow(), componentInitializer);
|
| 153 | this.craftingRemainingItem = properties.craftingRemainingItem;
|
| 154 | this.requiredFeatures = properties.requiredFeatures;
|
| 155 | if (SharedConstants.IS_RUNNING_IN_IDE) {
|
| 156 | String className = this.getClass().getSimpleName();
|
| 157 | if (!className.endsWith("Item")) {
|
| 158 | LOGGER.error("Item classes should end with Item and {} doesn't.", className);
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | @Deprecated
|
| 164 | public Holder.Reference<Item> builtInRegistryHolder() {
|
| 165 | return this.builtInRegistryHolder;
|
| 166 | }
|
| 167 |
|
| 168 | public DataComponentMap components() {
|
| 169 | return this.builtInRegistryHolder.components();
|
| 170 | }
|
| 171 |
|
| 172 | public int getDefaultMaxStackSize() {
|
| 173 | return this.components().getOrDefault(DataComponents.MAX_STACK_SIZE, 1);
|
| 174 | }
|
| 175 |
|
| 176 | public void onUseTick(final Level level, final LivingEntity livingEntity, final ItemStack itemStack, final int ticksRemaining) {
|
| 177 | }
|
| 178 |
|
| 179 | public void onDestroyed(final ItemEntity itemEntity) {
|
| 180 | }
|
| 181 |
|
| 182 | public boolean canDestroyBlock(final ItemStack itemStack, final BlockState state, final Level level, final BlockPos pos, final LivingEntity user) {
|
| 183 | Tool tool = itemStack.get(DataComponents.TOOL);
|
| 184 | return tool != null && !tool.canDestroyBlocksInCreative() ? !(user instanceof Player player && player.getAbilities().instabuild) : true;
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public Item asItem() {
|
| 189 | return this;
|
| 190 | }
|
| 191 |
|
| 192 | public InteractionResult useOn(final UseOnContext context) {
|
| 193 | ItemStack stack = context.getItemInHand();
|
| 194 | Holder<BlockTransformer> blockTransformer = stack.get(DataComponents.BLOCK_TRANSFORMER);
|
| 195 | return blockTransformer != null ? blockTransformer.value().transformBlock(context) : InteractionResult.PASS;
|
| 196 | }
|
| 197 |
|
| 198 | public float getDestroySpeed(final ItemStack itemStack, final BlockState state) {
|
| 199 | Tool tool = itemStack.get(DataComponents.TOOL);
|
| 200 | return tool != null ? tool.getMiningSpeed(state) : 1.0F;
|
| 201 | }
|
| 202 |
|
| 203 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 204 | ItemStack stack = player.getItemInHand(hand);
|
| 205 | Consumable consumable = stack.get(DataComponents.CONSUMABLE);
|
| 206 | if (consumable != null) {
|
| 207 | return consumable.startConsuming(player, stack, hand);
|
| 208 | } else {
|
| 209 | Equippable equippable = stack.get(DataComponents.EQUIPPABLE);
|
| 210 | if (equippable != null && equippable.swappable()) {
|
| 211 | return equippable.swapWithEquipmentSlot(stack, player);
|
| 212 | } else if (stack.has(DataComponents.BLOCKS_ATTACKS)) {
|
| 213 | player.startUsingItem(hand);
|
| 214 | return InteractionResult.CONSUME;
|
| 215 | } else {
|
| 216 | KineticWeapon kineticWeapon = stack.get(DataComponents.KINETIC_WEAPON);
|
| 217 | if (kineticWeapon != null) {
|
| 218 | player.startUsingItem(hand);
|
| 219 | kineticWeapon.makeSound(player);
|
| 220 | return InteractionResult.CONSUME;
|
| 221 | } else {
|
| 222 | return InteractionResult.PASS;
|
| 223 | }
|
| 224 | }
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | public ItemStack finishUsingItem(final ItemStack itemStack, final Level level, final LivingEntity entity) {
|
| 229 | Consumable consumable = itemStack.get(DataComponents.CONSUMABLE);
|
| 230 | return consumable != null ? consumable.onConsume(level, entity, itemStack) : itemStack;
|
| 231 | }
|
| 232 |
|
| 233 | public boolean isBarVisible(final ItemStack stack) {
|
| 234 | return stack.isDamaged();
|
| 235 | }
|
| 236 |
|
| 237 | public int getBarWidth(final ItemStack stack) {
|
| 238 | return Mth.clamp(Math.round(13.0F - stack.getDamageValue() * 13.0F / stack.getMaxDamage()), 0, 13);
|
| 239 | }
|
| 240 |
|
| 241 | public int getBarColor(final ItemStack stack) {
|
| 242 | int maxDamage = stack.getMaxDamage();
|
| 243 | float healthPercentage = Math.max(0.0F, ((float)maxDamage - stack.getDamageValue()) / maxDamage);
|
| 244 | return Mth.hsvToRgb(healthPercentage / 3.0F, 1.0F, 1.0F);
|
| 245 | }
|
| 246 |
|
| 247 | public boolean overrideStackedOnOther(final ItemStack self, final Slot slot, final ClickAction clickAction, final Player player) {
|
| 248 | return false;
|
| 249 | }
|
| 250 |
|
| 251 | public boolean overrideOtherStackedOnMe(
|
| 252 | final ItemStack self, final ItemStack other, final Slot slot, final ClickAction clickAction, final Player player, final SlotAccess carriedItem
|
| 253 | ) {
|
| 254 | return false;
|
| 255 | }
|
| 256 |
|
| 257 | public float getAttackDamageBonus(final Entity victim, final float damage, final DamageSource damageSource) {
|
| 258 | return 0.0F;
|
| 259 | }
|
| 260 |
|
| 261 | @Deprecated
|
| 262 | public @Nullable DamageSource getItemDamageSource(final LivingEntity attacker) {
|
| 263 | return null;
|
| 264 | }
|
| 265 |
|
| 266 | public void hurtEnemy(final ItemStack itemStack, final LivingEntity mob, final LivingEntity attacker) {
|
| 267 | }
|
| 268 |
|
| 269 | public void postHurtEnemy(final ItemStack itemStack, final LivingEntity mob, final LivingEntity attacker) {
|
| 270 | }
|
| 271 |
|
| 272 | public boolean mineBlock(final ItemStack itemStack, final Level level, final BlockState state, final BlockPos pos, final LivingEntity owner) {
|
| 273 | Tool tool = itemStack.get(DataComponents.TOOL);
|
| 274 | if (tool == null) {
|
| 275 | return false;
|
| 276 | }
|
| 277 |
|
| 278 | if (!level.isClientSide() && state.getDestroySpeed(level, pos) != 0.0F && tool.damagePerBlock() > 0) {
|
| 279 | itemStack.hurtAndBreak(tool.damagePerBlock(), owner, EquipmentSlot.MAINHAND);
|
| 280 | }
|
| 281 |
|
| 282 | return true;
|
| 283 | }
|
| 284 |
|
| 285 | public boolean isCorrectToolForDrops(final ItemStack itemStack, final BlockState state) {
|
| 286 | Tool tool = itemStack.get(DataComponents.TOOL);
|
| 287 | return tool != null && tool.isCorrectForDrops(state);
|
| 288 | }
|
| 289 |
|
| 290 | public InteractionResult interactLivingEntity(final ItemStack itemStack, final Player player, final LivingEntity target, final InteractionHand type) {
|
| 291 | return InteractionResult.PASS;
|
| 292 | }
|
| 293 |
|
| 294 | @Override
|
| 295 | public String toString() {
|
| 296 | return BuiltInRegistries.ITEM.wrapAsHolder(this).getRegisteredName();
|
| 297 | }
|
| 298 |
|
| 299 | public final @Nullable ItemStackTemplate getCraftingRemainder() {
|
| 300 | return this.craftingRemainingItem;
|
| 301 | }
|
| 302 |
|
| 303 | public void inventoryTick(final ItemStack itemStack, final ServerLevel level, final Entity owner, final @Nullable EquipmentSlot slot) {
|
| 304 | }
|
| 305 |
|
| 306 | public void onCraftedBy(final ItemStack itemStack, final Player player) {
|
| 307 | this.onCraftedPostProcess(itemStack, player.level());
|
| 308 | }
|
| 309 |
|
| 310 | public void onCraftedPostProcess(final ItemStack itemStack, final Level level) {
|
| 311 | }
|
| 312 |
|
| 313 | public ItemUseAnimation getUseAnimation(final ItemStack itemStack) {
|
| 314 | Consumable consumable = itemStack.get(DataComponents.CONSUMABLE);
|
| 315 | if (consumable != null) {
|
| 316 | return consumable.animation();
|
| 317 | } else if (itemStack.has(DataComponents.BLOCKS_ATTACKS)) {
|
| 318 | return ItemUseAnimation.BLOCK;
|
| 319 | } else {
|
| 320 | return itemStack.has(DataComponents.KINETIC_WEAPON) ? ItemUseAnimation.SPEAR : ItemUseAnimation.NONE;
|
| 321 | }
|
| 322 | }
|
| 323 |
|
| 324 | public int getUseDuration(final ItemStack itemStack, final LivingEntity user) {
|
| 325 | Consumable consumable = itemStack.get(DataComponents.CONSUMABLE);
|
| 326 | if (consumable != null) {
|
| 327 | return consumable.consumeTicks();
|
| 328 | } else {
|
| 329 | return !itemStack.has(DataComponents.BLOCKS_ATTACKS) && !itemStack.has(DataComponents.KINETIC_WEAPON) ? 0 : 72000;
|
| 330 | }
|
| 331 | }
|
| 332 |
|
| 333 | public boolean releaseUsing(final ItemStack itemStack, final Level level, final LivingEntity entity, final int remainingTime) {
|
| 334 | return false;
|
| 335 | }
|
| 336 |
|
| 337 | @Deprecated
|
| 338 | public void appendHoverText(
|
| 339 | final ItemStack itemStack, final Item.TooltipContext context, final TooltipDisplay display, final Consumer<Component> builder, final TooltipFlag tooltipFlag
|
| 340 | ) {
|
| 341 | }
|
| 342 |
|
| 343 | public Optional<TooltipComponent> getTooltipImage(final ItemStack itemStack) {
|
| 344 | return Optional.empty();
|
| 345 | }
|
| 346 |
|
| 347 | @VisibleForTesting
|
| 348 | public final String getDescriptionId() {
|
| 349 | return this.descriptionId;
|
| 350 | }
|
| 351 |
|
| 352 | public Component getName(final ItemStack itemStack) {
|
| 353 | return itemStack.getComponents().getOrDefault(DataComponents.ITEM_NAME, CommonComponents.EMPTY);
|
| 354 | }
|
| 355 |
|
| 356 | public boolean isFoil(final ItemStack itemStack) {
|
| 357 | return itemStack.isEnchanted();
|
| 358 | }
|
| 359 |
|
| 360 | protected static BlockHitResult getPlayerPOVHitResult(final Level level, final Player player, final ClipContext.Fluid fluid) {
|
| 361 | Vec3 from = player.getEyePosition();
|
| 362 | Vec3 to = from.add(Player.calculateViewVector(player.getXRot(), player.getYRot()).scale(player.blockInteractionRange()));
|
| 363 | return level.clip(new ClipContext(from, to, ClipContext.Block.OUTLINE, fluid, player));
|
| 364 | }
|
| 365 |
|
| 366 | public boolean useOnRelease(final ItemStack itemStack) {
|
| 367 | return false;
|
| 368 | }
|
| 369 |
|
| 370 | public ItemStack getDefaultInstance() {
|
| 371 | return new ItemStack(this);
|
| 372 | }
|
| 373 |
|
| 374 | public boolean canFitInsideContainerItems() {
|
| 375 | return true;
|
| 376 | }
|
| 377 |
|
| 378 | @Override
|
| 379 | public final FeatureFlagSet requiredFeatures() {
|
| 380 | return this.requiredFeatures;
|
| 381 | }
|
| 382 |
|
| 383 | public boolean shouldPrintOpWarning(final ItemStack stack, final @Nullable Player player) {
|
| 384 | return false;
|
| 385 | }
|
| 386 |
|
| 387 | /**
|
| 388 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 389 | */
|
| 390 | public static class Properties implements net.fabricmc.fabric.api.item.v1.FabricItem.Properties {
|
| 391 | private static final DependantName<Item, String> BLOCK_DESCRIPTION_ID = id -> Util.makeDescriptionId("block", id.identifier());
|
| 392 | private static final DependantName<Item, String> ITEM_DESCRIPTION_ID = id -> Util.makeDescriptionId("item", id.identifier());
|
| 393 | private DataComponentInitializers.Initializer<Item> componentInitializer = (builder, var1, var2) -> builder.addAll(DataComponents.COMMON_ITEM_COMPONENTS);
|
| 394 | private @Nullable ItemStackTemplate craftingRemainingItem;
|
| 395 | private FeatureFlagSet requiredFeatures = FeatureFlags.VANILLA_SET;
|
| 396 | private @Nullable ResourceKey<Item> id;
|
| 397 | private DependantName<Item, String> descriptionId = ITEM_DESCRIPTION_ID;
|
| 398 | private final DependantName<Item, Identifier> model = ResourceKey::identifier;
|
| 399 |
|
| 400 | public Item.Properties food(final FoodProperties foodProperties) {
|
| 401 | return this.food(foodProperties, Consumables.DEFAULT_FOOD);
|
| 402 | }
|
| 403 |
|
| 404 | public Item.Properties food(final FoodProperties foodProperties, final Consumable consumable) {
|
| 405 | return this.component(DataComponents.FOOD, foodProperties).component(DataComponents.CONSUMABLE, consumable);
|
| 406 | }
|
| 407 |
|
| 408 | public Item.Properties villagerFood(final int nutrition) {
|
| 409 | return this.component(DataComponents.VILLAGER_FOOD, new VillagerFood(nutrition));
|
| 410 | }
|
| 411 |
|
| 412 | public Item.Properties usingConvertsTo(final Item item) {
|
| 413 | return this.component(DataComponents.USE_REMAINDER, new UseRemainder(new ItemStackTemplate(item)));
|
| 414 | }
|
| 415 |
|
| 416 | public Item.Properties useCooldown(final float seconds) {
|
| 417 | return this.component(DataComponents.USE_COOLDOWN, new UseCooldown(seconds));
|
| 418 | }
|
| 419 |
|
| 420 | public Item.Properties stacksTo(final int max) {
|
| 421 | return this.component(DataComponents.MAX_STACK_SIZE, max);
|
| 422 | }
|
| 423 |
|
| 424 | public Item.Properties durability(final int maxDamage) {
|
| 425 | this.component(DataComponents.MAX_DAMAGE, maxDamage);
|
| 426 | this.component(DataComponents.MAX_STACK_SIZE, 1);
|
| 427 | this.component(DataComponents.DAMAGE, 0);
|
| 428 | return this;
|
| 429 | }
|
| 430 |
|
| 431 | public Item.Properties craftRemainder(final Item craftingRemainingItem) {
|
| 432 | return this.craftRemainder(new ItemStackTemplate(craftingRemainingItem));
|
| 433 | }
|
| 434 |
|
| 435 | public Item.Properties craftRemainder(final ItemStackTemplate craftingRemainingItem) {
|
| 436 | this.craftingRemainingItem = craftingRemainingItem;
|
| 437 | return this;
|
| 438 | }
|
| 439 |
|
| 440 | public Item.Properties rarity(final Rarity rarity) {
|
| 441 | return this.component(DataComponents.RARITY, rarity);
|
| 442 | }
|
| 443 |
|
| 444 | public Item.Properties fireResistant() {
|
| 445 | return this.delayedComponent(DataComponents.DAMAGE_RESISTANT, context -> new DamageResistant(context.getOrThrow(DamageTypeTags.IS_FIRE)));
|
| 446 | }
|
| 447 |
|
| 448 | public Item.Properties jukeboxPlayable(final ResourceKey<JukeboxSong> song) {
|
| 449 | return this.delayedComponent(DataComponents.JUKEBOX_PLAYABLE, context -> new JukeboxPlayable(context.getOrThrow(song)));
|
| 450 | }
|
| 451 |
|
| 452 | public Item.Properties enchantable(final int value) {
|
| 453 | return this.component(DataComponents.ENCHANTABLE, new Enchantable(value));
|
| 454 | }
|
| 455 |
|
| 456 | public Item.Properties potPattern(final ResourceKey<DecoratedPotPattern> pattern) {
|
| 457 | return this.delayedComponent(DataComponents.PROVIDES_POTTERY_PATTERN, context -> context.getOrThrow(pattern));
|
| 458 | }
|
| 459 |
|
| 460 | public Item.Properties repairable(final Item repairItem) {
|
| 461 | return this.component(DataComponents.REPAIRABLE, new Repairable(HolderSet.direct(repairItem.builtInRegistryHolder())));
|
| 462 | }
|
| 463 |
|
| 464 | public Item.Properties repairable(final TagKey<Item> repairItems) {
|
| 465 | HolderGetter<Item> registrationLookup = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.ITEM);
|
| 466 | return this.component(DataComponents.REPAIRABLE, new Repairable(registrationLookup.getOrThrow(repairItems)));
|
| 467 | }
|
| 468 |
|
| 469 | public Item.Properties equippable(final EquipmentSlot slot) {
|
| 470 | return this.component(DataComponents.EQUIPPABLE, Equippable.builder(slot).build());
|
| 471 | }
|
| 472 |
|
| 473 | public Item.Properties equippableUnswappable(final EquipmentSlot slot) {
|
| 474 | return this.component(DataComponents.EQUIPPABLE, Equippable.builder(slot).setSwappable(false).build());
|
| 475 | }
|
| 476 |
|
| 477 | public Item.Properties tool(
|
| 478 | final ToolMaterial material,
|
| 479 | final TagKey<Block> minesEfficiently,
|
| 480 | final float attackDamageBaseline,
|
| 481 | final float attackSpeedBaseline,
|
| 482 | final float disableBlockingSeconds
|
| 483 | ) {
|
| 484 | return material.applyToolProperties(this, minesEfficiently, attackDamageBaseline, attackSpeedBaseline, disableBlockingSeconds);
|
| 485 | }
|
| 486 |
|
| 487 | public Item.Properties pickaxe(final ToolMaterial material, final float attackDamageBaseline, final float attackSpeedBaseline) {
|
| 488 | return this.tool(material, BlockTags.MINEABLE_WITH_PICKAXE, attackDamageBaseline, attackSpeedBaseline, 0.0F);
|
| 489 | }
|
| 490 |
|
| 491 | public Item.Properties axe(final ToolMaterial material, final float attackDamageBaseline, final float attackSpeedBaseline) {
|
| 492 | return this.tool(material, BlockTags.MINEABLE_WITH_AXE, attackDamageBaseline, attackSpeedBaseline, 5.0F)
|
| 493 | .delayedComponent(DataComponents.BLOCK_TRANSFORMER, context -> context.getOrThrow(BlockTransformers.AXE));
|
| 494 | }
|
| 495 |
|
| 496 | public Item.Properties hoe(final ToolMaterial material, final float attackDamageBaseline, final float attackSpeedBaseline) {
|
| 497 | return this.tool(material, BlockTags.MINEABLE_WITH_HOE, attackDamageBaseline, attackSpeedBaseline, 0.0F)
|
| 498 | .delayedComponent(DataComponents.BLOCK_TRANSFORMER, context -> context.getOrThrow(BlockTransformers.HOE));
|
| 499 | }
|
| 500 |
|
| 501 | public Item.Properties shovel(final ToolMaterial material, final float attackDamageBaseline, final float attackSpeedBaseline) {
|
| 502 | return this.tool(material, BlockTags.MINEABLE_WITH_SHOVEL, attackDamageBaseline, attackSpeedBaseline, 0.0F)
|
| 503 | .delayedComponent(DataComponents.BLOCK_TRANSFORMER, context -> context.getOrThrow(BlockTransformers.SHOVEL));
|
| 504 | }
|
| 505 |
|
| 506 | public Item.Properties sword(final ToolMaterial material, final float attackDamageBaseline, final float attackSpeedBaseline) {
|
| 507 | return material.applySwordProperties(this, attackDamageBaseline, attackSpeedBaseline);
|
| 508 | }
|
| 509 |
|
| 510 | public Item.Properties spear(
|
| 511 | final ToolMaterial material,
|
| 512 | final float attackDuration,
|
| 513 | final float damageMultiplier,
|
| 514 | final float delay,
|
| 515 | final float dismountTime,
|
| 516 | final float dismountThreshold,
|
| 517 | final float knockbackTime,
|
| 518 | final float knockbackThreshold,
|
| 519 | final float damageTime,
|
| 520 | final float damageThreshold
|
| 521 | ) {
|
| 522 | return this.durability(material.durability())
|
| 523 | .repairable(material.repairItems())
|
| 524 | .enchantable(material.enchantmentValue())
|
| 525 | .delayedHolderComponent(DataComponents.DAMAGE_TYPE, DamageTypes.SPEAR)
|
| 526 | .component(
|
| 527 | DataComponents.KINETIC_WEAPON,
|
| 528 | new KineticWeapon(
|
| 529 | 10,
|
| 530 | (int)(delay * 20.0F),
|
| 531 | KineticWeapon.Condition.ofAttackerSpeed((int)(dismountTime * 20.0F), dismountThreshold),
|
| 532 | KineticWeapon.Condition.ofAttackerSpeed((int)(knockbackTime * 20.0F), knockbackThreshold),
|
| 533 | KineticWeapon.Condition.ofRelativeSpeed((int)(damageTime * 20.0F), damageThreshold),
|
| 534 | 0.38F,
|
| 535 | damageMultiplier,
|
| 536 | Optional.of(material == ToolMaterial.WOOD ? SoundEvents.SPEAR_WOOD_USE : SoundEvents.SPEAR_USE),
|
| 537 | Optional.of(material == ToolMaterial.WOOD ? SoundEvents.SPEAR_WOOD_HIT : SoundEvents.SPEAR_HIT)
|
| 538 | )
|
| 539 | )
|
| 540 | .component(
|
| 541 | DataComponents.PIERCING_WEAPON,
|
| 542 | new PiercingWeapon(
|
| 543 | true,
|
| 544 | false,
|
| 545 | Optional.of(material == ToolMaterial.WOOD ? SoundEvents.SPEAR_WOOD_ATTACK : SoundEvents.SPEAR_ATTACK),
|
| 546 | Optional.of(material == ToolMaterial.WOOD ? SoundEvents.SPEAR_WOOD_HIT : SoundEvents.SPEAR_HIT)
|
| 547 | )
|
| 548 | )
|
| 549 | .component(DataComponents.ATTACK_RANGE, new AttackRange(2.0F, 4.5F, 2.0F, 6.5F, 0.125F, 0.5F))
|
| 550 | .component(DataComponents.MINIMUM_ATTACK_CHARGE, 1.0F)
|
| 551 | .component(DataComponents.ATTACK_ANIMATION, new SwingAnimation(SwingAnimationType.STAB, (int)(attackDuration * 20.0F)))
|
| 552 | .attributes(
|
| 553 | ItemAttributeModifiers.builder()
|
| 554 | .add(
|
| 555 | Attributes.ATTACK_DAMAGE,
|
| 556 | new AttributeModifier(Item.BASE_ATTACK_DAMAGE_ID, 0.0F + material.attackDamageBonus(), AttributeModifier.Operation.ADD_VALUE),
|
| 557 | EquipmentSlotGroup.MAINHAND
|
| 558 | )
|
| 559 | .add(
|
| 560 | Attributes.ATTACK_SPEED,
|
| 561 | new AttributeModifier(Item.BASE_ATTACK_SPEED_ID, 1.0F / attackDuration - 4.0, AttributeModifier.Operation.ADD_VALUE),
|
| 562 | EquipmentSlotGroup.MAINHAND
|
| 563 | )
|
| 564 | .build()
|
| 565 | )
|
| 566 | .component(DataComponents.USE_EFFECTS, new UseEffects(true, false, 1.0F))
|
| 567 | .component(DataComponents.WEAPON, new Weapon(1));
|
| 568 | }
|
| 569 |
|
| 570 | public Item.Properties spawnEgg(final EntityType<?> type) {
|
| 571 | return this.component(DataComponents.ENTITY_DATA, TypedEntityData.of(type, new CompoundTag())).requiredFeatures(type.requiredFeatures());
|
| 572 | }
|
| 573 |
|
| 574 | public Item.Properties humanoidArmor(final ArmorMaterial material, final ArmorType type) {
|
| 575 | return this.durability(type.getDurability(material.durability()))
|
| 576 | .attributes(material.createAttributes(type))
|
| 577 | .enchantable(material.enchantmentValue())
|
| 578 | .component(DataComponents.EQUIPPABLE, Equippable.builder(type.getSlot()).setEquipSound(material.equipSound()).setAsset(material.assetId()).build())
|
| 579 | .repairable(material.repairIngredient());
|
| 580 | }
|
| 581 |
|
| 582 | public Item.Properties wolfArmor(final ArmorMaterial material) {
|
| 583 | return this.durability(ArmorType.BODY.getDurability(material.durability()))
|
| 584 | .attributes(material.createAttributes(ArmorType.BODY))
|
| 585 | .repairable(material.repairIngredient())
|
| 586 | .component(
|
| 587 | DataComponents.EQUIPPABLE,
|
| 588 | Equippable.builder(EquipmentSlot.BODY)
|
| 589 | .setEquipSound(material.equipSound())
|
| 590 | .setAsset(material.assetId())
|
| 591 | .setAllowedEntities(HolderSet.direct(EntityTypes.WOLF.builtInRegistryHolder()))
|
| 592 | .setCanBeSheared(true)
|
| 593 | .setShearingSound(BuiltInRegistries.SOUND_EVENT.wrapAsHolder(SoundEvents.ARMOR_UNEQUIP_WOLF))
|
| 594 | .build()
|
| 595 | )
|
| 596 | .component(DataComponents.BREAK_SOUND, SoundEvents.WOLF_ARMOR_BREAK)
|
| 597 | .stacksTo(1);
|
| 598 | }
|
| 599 |
|
| 600 | public Item.Properties horseArmor(final ArmorMaterial material) {
|
| 601 | HolderGetter<EntityType<?>> entityGetter = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.ENTITY_TYPE);
|
| 602 | return this.attributes(material.createAttributes(ArmorType.BODY))
|
| 603 | .component(
|
| 604 | DataComponents.EQUIPPABLE,
|
| 605 | Equippable.builder(EquipmentSlot.BODY)
|
| 606 | .setEquipSound(SoundEvents.HORSE_ARMOR)
|
| 607 | .setAsset(material.assetId())
|
| 608 | .setAllowedEntities(entityGetter.getOrThrow(EntityTypeTags.CAN_WEAR_HORSE_ARMOR))
|
| 609 | .setDamageOnHurt(false)
|
| 610 | .setCanBeSheared(true)
|
| 611 | .setShearingSound(SoundEvents.HORSE_ARMOR_UNEQUIP)
|
| 612 | .build()
|
| 613 | )
|
| 614 | .stacksTo(1);
|
| 615 | }
|
| 616 |
|
| 617 | public Item.Properties nautilusArmor(final ArmorMaterial material) {
|
| 618 | HolderGetter<EntityType<?>> entityGetter = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.ENTITY_TYPE);
|
| 619 | return this.attributes(material.createAttributes(ArmorType.BODY))
|
| 620 | .component(
|
| 621 | DataComponents.EQUIPPABLE,
|
| 622 | Equippable.builder(EquipmentSlot.BODY)
|
| 623 | .setEquipSound(SoundEvents.ARMOR_EQUIP_NAUTILUS)
|
| 624 | .setAsset(material.assetId())
|
| 625 | .setAllowedEntities(entityGetter.getOrThrow(EntityTypeTags.CAN_WEAR_NAUTILUS_ARMOR))
|
| 626 | .setDamageOnHurt(false)
|
| 627 | .setEquipOnInteract(true)
|
| 628 | .setCanBeSheared(true)
|
| 629 | .setShearingSound(SoundEvents.ARMOR_UNEQUIP_NAUTILUS)
|
| 630 | .build()
|
| 631 | )
|
| 632 | .stacksTo(1);
|
| 633 | }
|
| 634 |
|
| 635 | public Item.Properties trimMaterial(final ResourceKey<TrimMaterial> material) {
|
| 636 | return this.delayedHolderComponent(DataComponents.PROVIDES_TRIM_MATERIAL, material);
|
| 637 | }
|
| 638 |
|
| 639 | public Item.Properties signText() {
|
| 640 | return this.component(DataComponents.SIGN_TEXT_BACK, SignText.EMPTY).component(DataComponents.SIGN_TEXT_FRONT, SignText.EMPTY);
|
| 641 | }
|
| 642 |
|
| 643 | public Item.Properties cookingFuel(final ResourceKey<ContextIntProvider> burnTime) {
|
| 644 | return this.component(DataComponents.COOKING_FUEL, new CookingFuel(burnTime, ContextFloatProviders.COOKING_DEFAULT_SPEED_MULTIPLIER));
|
| 645 | }
|
| 646 |
|
| 647 | public Item.Properties brewingFuel(final ResourceKey<ContextIntProvider> uses) {
|
| 648 | return this.component(DataComponents.BREWING_FUEL, new BrewingFuel(uses, ContextFloatProviders.BREWING_DEFAULT_SPEED_MULTIPLIER));
|
| 649 | }
|
| 650 |
|
| 651 | public Item.Properties requiredFeatures(final FeatureFlag... flags) {
|
| 652 | this.requiredFeatures = FeatureFlags.REGISTRY.subset(flags);
|
| 653 | return this;
|
| 654 | }
|
| 655 |
|
| 656 | public Item.Properties requiredFeatures(final FeatureFlagSet flags) {
|
| 657 | if (!FeatureFlags.REGISTRY.isSubset(flags)) {
|
| 658 | throw new IllegalArgumentException("Mismatched flag sets");
|
| 659 | }
|
| 660 |
|
| 661 | this.requiredFeatures = flags;
|
| 662 | return this;
|
| 663 | }
|
| 664 |
|
| 665 | public Item.Properties setId(final ResourceKey<Item> id) {
|
| 666 | this.id = id;
|
| 667 | return this;
|
| 668 | }
|
| 669 |
|
| 670 | public Item.Properties overrideDescription(final String descriptionId) {
|
| 671 | this.descriptionId = DependantName.fixed(descriptionId);
|
| 672 | return this;
|
| 673 | }
|
| 674 |
|
| 675 | public Item.Properties useBlockDescriptionPrefix() {
|
| 676 | this.descriptionId = BLOCK_DESCRIPTION_ID;
|
| 677 | return this;
|
| 678 | }
|
| 679 |
|
| 680 | public Item.Properties useItemDescriptionPrefix() {
|
| 681 | this.descriptionId = ITEM_DESCRIPTION_ID;
|
| 682 | return this;
|
| 683 | }
|
| 684 |
|
| 685 | /**
|
| 686 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 687 | */
|
| 688 | public final ResourceKey<Item> itemIdOrThrow() {
|
| 689 | return Objects.requireNonNull(this.id, "Item id not set");
|
| 690 | }
|
| 691 |
|
| 692 | protected String effectiveDescriptionId() {
|
| 693 | return this.descriptionId.get(this.itemIdOrThrow());
|
| 694 | }
|
| 695 |
|
| 696 | public Identifier effectiveModel() {
|
| 697 | return this.model.get(this.itemIdOrThrow());
|
| 698 | }
|
| 699 |
|
| 700 | public <T> Item.Properties component(final DataComponentType<T> type, final T value) {
|
| 701 | this.componentInitializer = this.componentInitializer.add(type, value);
|
| 702 | return this;
|
| 703 | }
|
| 704 |
|
| 705 | public <T> Item.Properties delayedComponent(final DataComponentType<T> type, final DataComponentInitializers.SingleComponentInitializer<T> initializer) {
|
| 706 | this.componentInitializer = this.componentInitializer.andThen(initializer.asInitializer(type));
|
| 707 | return this;
|
| 708 | }
|
| 709 |
|
| 710 | public <T> Item.Properties delayedHolderComponent(final DataComponentType<Holder<T>> type, final ResourceKey<T> valueKey) {
|
| 711 | this.componentInitializer = this.componentInitializer.andThen((components, context, var4) -> components.set(type, context.getOrThrow(valueKey)));
|
| 712 | return this;
|
| 713 | }
|
| 714 |
|
| 715 | public Item.Properties attributes(final ItemAttributeModifiers attributes) {
|
| 716 | return this.component(DataComponents.ATTRIBUTE_MODIFIERS, attributes);
|
| 717 | }
|
| 718 |
|
| 719 | public Item.Properties compostable(final ResourceKey<ContextIntProvider> layers) {
|
| 720 | return this.component(DataComponents.COMPOSTABLE, new Compostable(layers));
|
| 721 | }
|
| 722 |
|
| 723 | @SafeVarargs
|
| 724 | public final Item.Properties loweredMobVisibility(final ResourceKey<EntityType<?>>... entityTypeIds) {
|
| 725 | return this.delayedComponent(DataComponents.MOB_VISIBILITY, context -> {
|
| 726 | HolderGetter<EntityType<?>> registry = context.lookupOrThrow(Registries.ENTITY_TYPE);
|
| 727 | List<Holder.Reference<EntityType<?>>> entityTypes = Arrays.stream(entityTypeIds).map(registry::getOrThrow).toList();
|
| 728 | return new MobVisibility(HolderSet.direct(entityTypes), 0.5F);
|
| 729 | });
|
| 730 | }
|
| 731 |
|
| 732 | private DataComponentInitializers.Initializer<Item> finalizeInitializer(final Component name, final Identifier model) {
|
| 733 | return this.componentInitializer
|
| 734 | .andThen((components, var3, var4) -> components.set(DataComponents.ITEM_NAME, name).set(DataComponents.ITEM_MODEL, model).addValidator(c -> {
|
| 735 | if (c.has(DataComponents.DAMAGE) && c.getOrDefault(DataComponents.MAX_STACK_SIZE, 1) > 1) {
|
| 736 | throw new IllegalStateException("Item cannot have both durability and be stackable");
|
| 737 | }
|
| 738 | }));
|
| 739 | }
|
| 740 | }
|
| 741 |
|
| 742 | public interface TooltipContext {
|
| 743 | Item.TooltipContext EMPTY = new Item.TooltipContext() {
|
| 744 | @Override
|
| 745 | public HolderLookup.@Nullable Provider registries() {
|
| 746 | return null;
|
| 747 | }
|
| 748 |
|
| 749 | @Override
|
| 750 | public float tickRate() {
|
| 751 | return 20.0F;
|
| 752 | }
|
| 753 |
|
| 754 | @Override
|
| 755 | public @Nullable MapItemSavedData mapData(final MapId id) {
|
| 756 | return null;
|
| 757 | }
|
| 758 |
|
| 759 | @Override
|
| 760 | public boolean isPeaceful() {
|
| 761 | return false;
|
| 762 | }
|
| 763 | };
|
| 764 |
|
| 765 | HolderLookup.@Nullable Provider registries();
|
| 766 |
|
| 767 | float tickRate();
|
| 768 |
|
| 769 | @Nullable MapItemSavedData mapData(MapId id);
|
| 770 |
|
| 771 | boolean isPeaceful();
|
| 772 |
|
| 773 | static Item.TooltipContext of(final @Nullable Level level) {
|
| 774 | return level == null ? EMPTY : new Item.TooltipContext() {
|
| 775 | @Override
|
| 776 | public HolderLookup.Provider registries() {
|
| 777 | return level.registryAccess();
|
| 778 | }
|
| 779 |
|
| 780 | @Override
|
| 781 | public float tickRate() {
|
| 782 | return level.tickRateManager().tickrate();
|
| 783 | }
|
| 784 |
|
| 785 | @Override
|
| 786 | public @Nullable MapItemSavedData mapData(final MapId id) {
|
| 787 | return level.getMapData(id);
|
| 788 | }
|
| 789 |
|
| 790 | @Override
|
| 791 | public boolean isPeaceful() {
|
| 792 | return level.getDifficulty() == Difficulty.PEACEFUL;
|
| 793 | }
|
| 794 | };
|
| 795 | }
|
| 796 |
|
| 797 | static Item.TooltipContext of(final HolderLookup.Provider registries) {
|
| 798 | return new Item.TooltipContext() {
|
| 799 | @Override
|
| 800 | public HolderLookup.Provider registries() {
|
| 801 | return registries;
|
| 802 | }
|
| 803 |
|
| 804 | @Override
|
| 805 | public float tickRate() {
|
| 806 | return 20.0F;
|
| 807 | }
|
| 808 |
|
| 809 | @Override
|
| 810 | public @Nullable MapItemSavedData mapData(final MapId id) {
|
| 811 | return null;
|
| 812 | }
|
| 813 |
|
| 814 | @Override
|
| 815 | public boolean isPeaceful() {
|
| 816 | return false;
|
| 817 | }
|
| 818 | };
|
| 819 | }
|
| 820 | }
|
| 821 | }
|
| 822 |
|