diff --git a/common/src/main/java/dev/mayaqq/estrogen/registry/blockEntities/CookieJarBlockEntity.java b/common/src/main/java/dev/mayaqq/estrogen/registry/blockEntities/CookieJarBlockEntity.java index 4a2b4df1..0fba7d7c 100644 --- a/common/src/main/java/dev/mayaqq/estrogen/registry/blockEntities/CookieJarBlockEntity.java +++ b/common/src/main/java/dev/mayaqq/estrogen/registry/blockEntities/CookieJarBlockEntity.java @@ -24,27 +24,6 @@ public CookieJarBlockEntity(BlockEntityType type, BlockPos blockPos, BlockSta super(type, blockPos, blockState); } - /** - * Add 1 count of the item to the jar. - * Does not decrement the input itemStack. - */ - public void add1Item(ItemStack itemStack) { - int i = 0; - for (ItemStack jarItemStack : items) { - if (jarItemStack.isEmpty()) { - items.set(i, itemStack.copyWithCount(1)); - notifyUpdate(); - return; - } - if (ItemStack.isSameItemSameTags(jarItemStack, itemStack) && jarItemStack.getCount() < jarItemStack.getMaxStackSize()) { - jarItemStack.grow(1); - notifyUpdate(); - return; - } - i++; - } - } - /** * Remove and returns 1 count of the last-most item from the jar. * returns ItemStack.EMPTY if jar was empty @@ -63,30 +42,53 @@ public ItemStack remove1Item() { } /** - * Returns true if 1 of the item can be added to the jar - * and that item is a cookie - * and that cookie matches the cookie already in the jar. + * Adds the item stack to the jar, and returns the item stack not added to the jar. + * @param itemStack The stack to be added to the jar. + * @return The stack not added to the jar. */ - public boolean canAddItem(ItemStack itemStack) { - // cookie condition - if (!canPlaceItem(0, itemStack)) { - return false; - } + public ItemStack addItemStack(ItemStack itemStack) { + if (!canPlaceItem(0, itemStack)) return itemStack; - // if the top condition is removed, then the jar can work for any item + ItemStack itemStackCopy = itemStack.copy(); + int i = 0; for (ItemStack jarItemStack : items) { + if (!jarItemStack.isEmpty() && !ItemStack.isSameItemSameTags(jarItemStack, itemStackCopy)) continue; + + ItemStack addToJar = itemStackCopy.split(itemStackCopy.getMaxStackSize() - jarItemStack.getCount()); + addToJar.grow(jarItemStack.getCount()); + items.set(i, addToJar); + + if (itemStackCopy.isEmpty()) break; + + i++; + } + notifyUpdate(); + return itemStackCopy; + } + + /** + * Removes and returns a stack of items from the jar. + * @return The stack taken from the jar. + */ + public ItemStack removeItemStack() { + ItemStack result = ItemStack.EMPTY; + for (int i = items.size() - 1; i >= 0; i--) { + ItemStack jarItemStack = items.get(i); if (jarItemStack.isEmpty()) { - return true; - } - if (!ItemStack.isSameItemSameTags(jarItemStack, itemStack)) { continue; } - if (jarItemStack.getCount() >= jarItemStack.getMaxStackSize()) { + if (result.isEmpty()) { + result = jarItemStack.split(jarItemStack.getMaxStackSize()); continue; } - return true; + + if (!ItemStack.isSameItemSameTags(jarItemStack, result)) break; + ItemStack fromJarStack = jarItemStack.split(result.getMaxStackSize() - result.getCount()); + result.grow(fromJarStack.getCount()); + if (result.getCount() == result.getMaxStackSize()) break; } - return false; + notifyUpdate(); + return result; } /** @@ -121,11 +123,7 @@ public boolean matchesFirstItem(ItemStack itemStack) { if (jarItemStack.isEmpty()) { continue; } - if (!ItemStack.isSameItemSameTags(jarItemStack, itemStack)) { - return false; - } else { - return true; - } + return ItemStack.isSameItemSameTags(jarItemStack, itemStack); } return true; } @@ -205,10 +203,7 @@ public boolean canPlaceItem(int slot, ItemStack itemStack) { if (!itemStack.is(EstrogenTags.Items.COOKIES)) { return false; } - if (!matchesFirstItem(itemStack)) { - return false; - } - return true; + return matchesFirstItem(itemStack); } @Override diff --git a/common/src/main/java/dev/mayaqq/estrogen/registry/blocks/CookieJarBlock.java b/common/src/main/java/dev/mayaqq/estrogen/registry/blocks/CookieJarBlock.java index 8331c258..05beffe3 100644 --- a/common/src/main/java/dev/mayaqq/estrogen/registry/blocks/CookieJarBlock.java +++ b/common/src/main/java/dev/mayaqq/estrogen/registry/blocks/CookieJarBlock.java @@ -74,28 +74,29 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player ItemStack handItem = player.getItemInHand(hand); if (!handItem.isEmpty()) { - if (cookieJarBlockEntity.canAddItem(handItem)) { - // adding item to jar - cookieJarBlockEntity.add1Item(handItem); - if (!player.isCreative()) handItem.shrink(1); + ItemStack remainder = cookieJarBlockEntity.addItemStack(handItem); + if (ItemStack.matches(handItem, remainder)) { + // jar was full, couldn't add item to jar + level.playSound(null, pos, EstrogenSounds.JAR_FULL.get(), SoundSource.BLOCKS, 1.0F, 1.0F); + } else { + if (!player.isCreative()) handItem.setCount(remainder.getCount()); player.awardStat(Stats.ITEM_USED.get(handItem.getItem())); level.playSound(null, pos, EstrogenSounds.JAR_INSERT.get(), SoundSource.BLOCKS, 1.0F, 0.7F + 0.5F * ((float) cookieJarBlockEntity.getCount() / 512)); if (level instanceof ServerLevel serverLevel) { EstrogenAdvancementCriteria.INSERT_JAR.trigger((ServerPlayer) player); - serverLevel.sendParticles(ParticleTypes.CRIT, (double)pos.getX() + 0.5, (double)pos.getY() + 1.2, (double)pos.getZ() + 0.5, 7, 0.0, 0.0, 0.0, 0.0); + serverLevel.sendParticles(ParticleTypes.CRIT, (double) pos.getX() + 0.5, (double) pos.getY() + 1.2, (double) pos.getZ() + 0.5, 7, 0.0, 0.0, 0.0, 0.0); } - } else { - // jar was full, couldn't add item to jar - level.playSound(null, pos, EstrogenSounds.JAR_FULL.get(), SoundSource.BLOCKS, 1.0F, 1.0F); } } else { - ItemStack itemStack = cookieJarBlockEntity.remove1Item(); - if (!itemStack.isEmpty()) { + // take whole stack if crouching + ItemStack jarItemStack = player.isShiftKeyDown() ? cookieJarBlockEntity.removeItemStack() : cookieJarBlockEntity.remove1Item(); + + if (!jarItemStack.isEmpty()) { // removing item from jar level.playSound(null, pos, EstrogenSounds.JAR_INSERT.get(), SoundSource.BLOCKS, 1.0F, 0.7F + 0.5F * ((float) cookieJarBlockEntity.getCount() / 512)); if (level instanceof ServerLevel) { - player.getInventory().placeItemBackInInventory(itemStack); + player.getInventory().placeItemBackInInventory(jarItemStack); } } else { // jar was empty, couldn't remove item from jar