Buckets:
| package com.example.examplemod; | |
| import com.mojang.serialization.Codec; | |
| import com.mojang.serialization.codecs.RecordCodecBuilder; | |
| import net.minecraft.component.ComponentType; | |
| import net.minecraft.component.type.NbtComponent; | |
| import net.minecraft.item.ItemStack; | |
| import net.minecraft.network.codec.PacketCodecs; | |
| import net.minecraft.registry.Registries; | |
| import net.minecraft.registry.Registry; | |
| import net.minecraft.text.Text; | |
| import net.minecraft.util.Identifier; | |
| /** | |
| * The 1.20.5+ DataComponent system replaced NBT-based item data and is the | |
| * standard way to attach custom data to ItemStacks in 1.21.7 - 1.21.11. | |
| * | |
| * Example: a "Charge Level" component for a custom tool that tracks | |
| * remaining charges and shows it in the tooltip. | |
| */ | |
| public final class ExampleComponents { | |
| private ExampleComponents() {} | |
| // A simple integer component: how many "charges" remain on the item. | |
| public static final ComponentType<Integer> CHARGE_LEVEL = Registry.register( | |
| Registries.DATA_COMPONENT_TYPE, | |
| Identifier.of(ExampleMod.MOD_ID, "charge_level"), | |
| ComponentType.<Integer>builder() | |
| .codec(Codec.INT) | |
| .packetCodec(PacketCodecs.VAR_INT) | |
| .build() | |
| ); | |
| /** Set initial charge on item creation. */ | |
| public static ItemStack withCharges(ItemStack stack, int charges) { | |
| stack.set(CHARGE_LEVEL, charges); | |
| return stack; | |
| } | |
| /** Consume one charge; returns true if there was a charge to consume. */ | |
| public static boolean consumeCharge(ItemStack stack) { | |
| Integer current = stack.get(CHARGE_LEVEL); | |
| if (current == null || current <= 0) return false; | |
| stack.set(CHARGE_LEVEL, current - 1); | |
| return true; | |
| } | |
| /** Build a tooltip line showing remaining charges (call from an Item subclass override). */ | |
| public static Text getChargeTooltip(ItemStack stack) { | |
| Integer current = stack.getOrDefault(CHARGE_LEVEL, 0); | |
| return Text.literal("Charges: " + current).formatted(net.minecraft.util.Formatting.AQUA); | |
| } | |
| } | |
Xet Storage Details
- Size:
- 2.09 kB
- Xet hash:
- 1f080070a3fef0bfcefba24d5de2401bdedca95c9c6afe804c6069b72ed0a552
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.