| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import com.mojang.serialization.DataResult.Error;
|
| 4 | import com.mojang.serialization.DataResult.Success;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.stats.Stats;
|
| 10 | import net.minecraft.util.ARGB;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.Prediction;
|
| 13 | import net.minecraft.world.InteractionHand;
|
| 14 | import net.minecraft.world.InteractionResult;
|
| 15 | import net.minecraft.world.entity.Entity;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import net.minecraft.world.entity.SlotAccess;
|
| 18 | import net.minecraft.world.entity.item.ItemEntity;
|
| 19 | import net.minecraft.world.entity.player.Player;
|
| 20 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 21 | import net.minecraft.world.inventory.ClickAction;
|
| 22 | import net.minecraft.world.inventory.Slot;
|
| 23 | import net.minecraft.world.inventory.tooltip.BundleTooltip;
|
| 24 | import net.minecraft.world.inventory.tooltip.TooltipComponent;
|
| 25 | import net.minecraft.world.item.component.BundleContents;
|
| 26 | import net.minecraft.world.item.component.TooltipDisplay;
|
| 27 | import net.minecraft.world.level.Level;
|
| 28 | import org.apache.commons.lang3.math.Fraction;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | public class BundleItem extends Item {
|
| 32 | public static final int MAX_SHOWN_GRID_ITEMS_X = 4;
|
| 33 | public static final int MAX_SHOWN_GRID_ITEMS_Y = 3;
|
| 34 | public static final int MAX_SHOWN_GRID_ITEMS = 12;
|
| 35 | public static final int OVERFLOWING_MAX_SHOWN_GRID_ITEMS = 11;
|
| 36 | private static final int FULL_BAR_COLOR = ARGB.colorFromFloat(1.0F, 1.0F, 0.33F, 0.33F);
|
| 37 | private static final int BAR_COLOR = ARGB.colorFromFloat(1.0F, 0.44F, 0.53F, 1.0F);
|
| 38 | private static final int TICKS_AFTER_FIRST_THROW = 10;
|
| 39 | private static final int TICKS_BETWEEN_THROWS = 2;
|
| 40 | private static final int TICKS_MAX_THROW_DURATION = 200;
|
| 41 |
|
| 42 | public BundleItem(final Item.Properties properties) {
|
| 43 | super(properties);
|
| 44 | }
|
| 45 |
|
| 46 | private static Fraction getWeightSafe(final BundleContents contents) {
|
| 47 | return switch (contents.weight()) {
|
| 48 | case Success<Fraction> success -> (Fraction)success.value();
|
| 49 | case Error<?> error -> Fraction.ONE;
|
| 50 | default -> throw new MatchException(null, null);
|
| 51 | };
|
| 52 | }
|
| 53 |
|
| 54 | public static float getFullnessDisplay(final ItemStack itemStack) {
|
| 55 | BundleContents contents = itemStack.getOrDefault(DataComponents.BUNDLE_CONTENTS, BundleContents.EMPTY);
|
| 56 | return getWeightSafe(contents).floatValue();
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public boolean overrideStackedOnOther(final ItemStack self, final Slot slot, final ClickAction clickAction, final Player player) {
|
| 61 | BundleContents initialContents = self.get(DataComponents.BUNDLE_CONTENTS);
|
| 62 | if (initialContents == null) {
|
| 63 | return false;
|
| 64 | }
|
| 65 |
|
| 66 | ItemStack other = slot.getItem();
|
| 67 | BundleContents.Mutable contents = initialContents.asMutable();
|
| 68 | if (clickAction == ClickAction.PRIMARY && !other.isEmpty()) {
|
| 69 | if (contents.tryTransfer(slot, player) > 0) {
|
| 70 | playInsertSound(player);
|
| 71 | } else {
|
| 72 | playInsertFailSound(player);
|
| 73 | }
|
| 74 |
|
| 75 | self.set(DataComponents.BUNDLE_CONTENTS, contents.toImmutable());
|
| 76 | this.broadcastChangesOnContainerMenu(player);
|
| 77 | return true;
|
| 78 | } else if (clickAction == ClickAction.SECONDARY && other.isEmpty()) {
|
| 79 | ItemStack itemStack = contents.removeOne();
|
| 80 | if (itemStack != null) {
|
| 81 | ItemStack remainder = slot.safeInsert(itemStack);
|
| 82 | if (remainder.getCount() > 0) {
|
| 83 | contents.tryInsert(remainder);
|
| 84 | } else {
|
| 85 | playRemoveOneSound(player);
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | self.set(DataComponents.BUNDLE_CONTENTS, contents.toImmutable());
|
| 90 | this.broadcastChangesOnContainerMenu(player);
|
| 91 | return true;
|
| 92 | } else {
|
| 93 | return false;
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public boolean overrideOtherStackedOnMe(
|
| 99 | final ItemStack self, final ItemStack other, final Slot slot, final ClickAction clickAction, final Player player, final SlotAccess carriedItem
|
| 100 | ) {
|
| 101 | if (clickAction == ClickAction.PRIMARY && other.isEmpty()) {
|
| 102 | toggleSelectedItem(self, -1);
|
| 103 | return false;
|
| 104 | }
|
| 105 |
|
| 106 | BundleContents initialContents = self.get(DataComponents.BUNDLE_CONTENTS);
|
| 107 | if (initialContents == null) {
|
| 108 | return false;
|
| 109 | }
|
| 110 |
|
| 111 | BundleContents.Mutable contents = initialContents.asMutable();
|
| 112 | if (clickAction == ClickAction.PRIMARY && !other.isEmpty()) {
|
| 113 | if (slot.allowModification(player) && contents.tryInsert(other) > 0) {
|
| 114 | playInsertSound(player);
|
| 115 | } else {
|
| 116 | playInsertFailSound(player);
|
| 117 | }
|
| 118 |
|
| 119 | self.set(DataComponents.BUNDLE_CONTENTS, contents.toImmutable());
|
| 120 | this.broadcastChangesOnContainerMenu(player);
|
| 121 | return true;
|
| 122 | } else if (clickAction == ClickAction.SECONDARY && other.isEmpty()) {
|
| 123 | if (slot.allowModification(player)) {
|
| 124 | ItemStack removed = contents.removeOne();
|
| 125 | if (removed != null) {
|
| 126 | playRemoveOneSound(player);
|
| 127 | carriedItem.set(removed);
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | self.set(DataComponents.BUNDLE_CONTENTS, contents.toImmutable());
|
| 132 | this.broadcastChangesOnContainerMenu(player);
|
| 133 | return true;
|
| 134 | } else {
|
| 135 | toggleSelectedItem(self, -1);
|
| 136 | return false;
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 142 | player.startUsingItem(hand);
|
| 143 | return InteractionResult.SUCCESS;
|
| 144 | }
|
| 145 |
|
| 146 | private void dropContent(final Level level, final Player player, final ItemStack itemStack) {
|
| 147 | if (this.dropContent(itemStack, player)) {
|
| 148 | playDropContentsSound(level, player);
|
| 149 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public boolean isBarVisible(final ItemStack stack) {
|
| 155 | BundleContents contents = stack.getOrDefault(DataComponents.BUNDLE_CONTENTS, BundleContents.EMPTY);
|
| 156 | return getWeightSafe(contents).compareTo(Fraction.ZERO) > 0;
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | public int getBarWidth(final ItemStack stack) {
|
| 161 | BundleContents contents = stack.getOrDefault(DataComponents.BUNDLE_CONTENTS, BundleContents.EMPTY);
|
| 162 | return Math.min(1 + Mth.mulAndTruncate(getWeightSafe(contents), 12), 13);
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | public int getBarColor(final ItemStack stack) {
|
| 167 | BundleContents contents = stack.getOrDefault(DataComponents.BUNDLE_CONTENTS, BundleContents.EMPTY);
|
| 168 | return getWeightSafe(contents).compareTo(Fraction.ONE) >= 0 ? FULL_BAR_COLOR : BAR_COLOR;
|
| 169 | }
|
| 170 |
|
| 171 | public static void toggleSelectedItem(final ItemStack stack, final int selectedItem) {
|
| 172 | BundleContents initialContents = stack.get(DataComponents.BUNDLE_CONTENTS);
|
| 173 | if (initialContents != null) {
|
| 174 | BundleContents.Mutable contents = initialContents.asMutable();
|
| 175 | contents.toggleSelectedItem(selectedItem);
|
| 176 | stack.set(DataComponents.BUNDLE_CONTENTS, contents.toImmutable());
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | public static int getSelectedItemIndex(final ItemStack stack) {
|
| 181 | return stack.getOrDefault(DataComponents.BUNDLE_CONTENTS, BundleContents.EMPTY).getSelectedItemIndex();
|
| 182 | }
|
| 183 |
|
| 184 | public static @Nullable ItemStackTemplate getSelectedItem(final ItemStack stack) {
|
| 185 | return stack.getOrDefault(DataComponents.BUNDLE_CONTENTS, BundleContents.EMPTY).getSelectedItem();
|
| 186 | }
|
| 187 |
|
| 188 | public static int getNumberOfItemsToShow(final ItemStack stack) {
|
| 189 | BundleContents contents = stack.getOrDefault(DataComponents.BUNDLE_CONTENTS, BundleContents.EMPTY);
|
| 190 | return contents.getNumberOfItemsToShow();
|
| 191 | }
|
| 192 |
|
| 193 | private boolean dropContent(final ItemStack bundle, final Player player) {
|
| 194 | BundleContents contents = bundle.get(DataComponents.BUNDLE_CONTENTS);
|
| 195 | if (contents != null && !contents.isEmpty()) {
|
| 196 | Optional<ItemStack> itemStack = removeOneItemFromBundle(bundle, player, contents);
|
| 197 | if (itemStack.isPresent()) {
|
| 198 | player.drop(itemStack.get(), true, Prediction.PREDICTED);
|
| 199 | return true;
|
| 200 | } else {
|
| 201 | return false;
|
| 202 | }
|
| 203 | } else {
|
| 204 | return false;
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | private static Optional<ItemStack> removeOneItemFromBundle(final ItemStack self, final Player player, final BundleContents initialContents) {
|
| 209 | BundleContents.Mutable contents = initialContents.asMutable();
|
| 210 | ItemStack removed = contents.removeOne();
|
| 211 | if (removed != null) {
|
| 212 | playRemoveOneSound(player);
|
| 213 | self.set(DataComponents.BUNDLE_CONTENTS, contents.toImmutable());
|
| 214 | return Optional.of(removed);
|
| 215 | } else {
|
| 216 | return Optional.empty();
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | public void onUseTick(final Level level, final LivingEntity livingEntity, final ItemStack itemStack, final int ticksRemaining) {
|
| 222 | if (livingEntity instanceof Player player) {
|
| 223 | int useDuration = this.getUseDuration(itemStack, livingEntity);
|
| 224 | boolean isFirstTick = ticksRemaining == useDuration;
|
| 225 | if (isFirstTick || ticksRemaining < useDuration - 10 && ticksRemaining % 2 == 0) {
|
| 226 | this.dropContent(level, player, itemStack);
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | @Override
|
| 232 | public int getUseDuration(final ItemStack itemStack, final LivingEntity entity) {
|
| 233 | return 200;
|
| 234 | }
|
| 235 |
|
| 236 | @Override
|
| 237 | public ItemUseAnimation getUseAnimation(final ItemStack itemStack) {
|
| 238 | return ItemUseAnimation.BUNDLE;
|
| 239 | }
|
| 240 |
|
| 241 | @Override
|
| 242 | public Optional<TooltipComponent> getTooltipImage(final ItemStack bundle) {
|
| 243 | TooltipDisplay display = bundle.getOrDefault(DataComponents.TOOLTIP_DISPLAY, TooltipDisplay.DEFAULT);
|
| 244 | return !display.shows(DataComponents.BUNDLE_CONTENTS)
|
| 245 | ? Optional.empty()
|
| 246 | : Optional.ofNullable(bundle.get(DataComponents.BUNDLE_CONTENTS)).map(BundleTooltip::new);
|
| 247 | }
|
| 248 |
|
| 249 | @Override
|
| 250 | public void onDestroyed(final ItemEntity entity) {
|
| 251 | BundleContents contents = entity.getItem().get(DataComponents.BUNDLE_CONTENTS);
|
| 252 | if (contents != null) {
|
| 253 | entity.getItem().set(DataComponents.BUNDLE_CONTENTS, BundleContents.EMPTY);
|
| 254 | ItemUtils.onContainerDestroyed(entity, contents.itemCopies());
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | private static void playRemoveOneSound(final Entity entity) {
|
| 259 | entity.playSound(SoundEvents.BUNDLE_REMOVE_ONE, 0.8F, 0.8F + entity.level().getRandom().nextFloat() * 0.4F);
|
| 260 | }
|
| 261 |
|
| 262 | private static void playInsertSound(final Entity entity) {
|
| 263 | entity.playSound(SoundEvents.BUNDLE_INSERT, 0.8F, 0.8F + entity.level().getRandom().nextFloat() * 0.4F);
|
| 264 | }
|
| 265 |
|
| 266 | private static void playInsertFailSound(final Entity entity) {
|
| 267 | entity.playSound(SoundEvents.BUNDLE_INSERT_FAIL, 1.0F, 1.0F);
|
| 268 | }
|
| 269 |
|
| 270 | private static void playDropContentsSound(final Level level, final Entity entity) {
|
| 271 | level.playSound(
|
| 272 | null, entity.blockPosition(), SoundEvents.BUNDLE_DROP_CONTENTS, SoundSource.PLAYERS, 0.8F, 0.8F + entity.level().getRandom().nextFloat() * 0.4F
|
| 273 | );
|
| 274 | }
|
| 275 |
|
| 276 | private void broadcastChangesOnContainerMenu(final Player player) {
|
| 277 | AbstractContainerMenu containerMenu = player.containerMenu;
|
| 278 | if (containerMenu != null) {
|
| 279 | containerMenu.slotsChanged(player.getInventory());
|
| 280 | }
|
| 281 | }
|
| 282 | }
|
| 283 |
|