Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipe Config Card to extract blocks from items containing items (such as Banks from Bank Storage) #1022

Open
wants to merge 1 commit into
base: 1.21.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ public ItemStack getCamouflageStack() {
/**
* Set the camouflage directly. The camouflage block should be consumed from the player before calling this.
*/
private void setCamouflage(Player player, @Nullable BlockState newCamouflage) {
private void setCamouflage(@Nullable BlockState newCamouflage) {
if (level.isClientSide) {
throw new IllegalStateException("Cannot call setCamouflage on the client");
}

boolean hadCamouflage = hasCamouflage();

if (camouflage != null) {
Expand All @@ -267,16 +271,14 @@ private void setCamouflage(Player player, @Nullable BlockState newCamouflage) {
if (newCamouflage == null) {
var group = camouflage.getSoundType();
var sound = group.getBreakSound();
level.playSound(player, worldPosition, sound, SoundSource.BLOCKS, (group.getVolume() + 1.0F) / 4.0F, group.getPitch() * 0.8F);
level.playSound(null, worldPosition, sound, SoundSource.BLOCKS, (group.getVolume() + 1.0F) / 4.0F, group.getPitch() * 0.8F);
}

// Remove camouflage
camouflage = null;
setChanged();
if (!level.isClientSide()) {
sync();
rebuildCollisionShape();
}
sync();
rebuildCollisionShape();
}

camouflage = newCamouflage;
Expand All @@ -285,13 +287,11 @@ private void setCamouflage(Player player, @Nullable BlockState newCamouflage) {
// Play a cool sound
var group = newCamouflage.getSoundType();
var sound = group.getPlaceSound();
level.playSound(player, worldPosition, sound, SoundSource.BLOCKS, (group.getVolume() + 1.0F) / 4.0F, group.getPitch() * 0.8F);
level.playSound(null, worldPosition, sound, SoundSource.BLOCKS, (group.getVolume() + 1.0F) / 4.0F, group.getPitch() * 0.8F);

setChanged();
if (!level.isClientSide()) {
sync();
rebuildCollisionShape();
}
sync();
rebuildCollisionShape();
}

if (hadCamouflage != hasCamouflage()) {
Expand All @@ -310,7 +310,9 @@ public boolean tryRemoveCamouflage(Player player, InteractionHand hand) {
return false;
}

setCamouflage(player, null);
if (!player.level().isClientSide) {
setCamouflage(null);
}

return true;
}
Expand Down Expand Up @@ -340,11 +342,16 @@ public boolean tryApplyCamouflage(Player player, InteractionHand hand) {
return true;
}

// Item capabilities shouldn't be messed with (and in some cases cannot - leading to inconsistent behavior) on the client side
if (player.level().isClientSide) {
return true;
}

boolean itemChanged = camouflage == null || newCamouflage.getBlock() != camouflage.getBlock();

if (!player.getAbilities().instabuild && itemChanged) {
var itemToUse = newCamouflage.getBlock().asItem();
var extracted = TransferHelper.extractMatching(new PlayerInvWrapper(player.getInventory()), s -> s.is(itemToUse), 1);
var extracted = TransferHelper.extractMatching(new PlayerInvWrapper(player.getInventory()), s -> s.is(itemToUse), 1, true);

if (extracted.isEmpty()) {
player.displayClientMessage(MITooltips.line(MIText.ConfigCardNoCamouflageInInventory)
Expand All @@ -353,7 +360,7 @@ public boolean tryApplyCamouflage(Player player, InteractionHand hand) {
}
}

setCamouflage(player, newCamouflage);
setCamouflage(newCamouflage);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ void applyConfig(PipeBlockEntity pipe, @Nullable SavedItemPipeConfig config, Pla
}

private int fetchItems(Player player, ItemVariant what, int maxAmount) {
return TransferHelper.extractMatching(new PlayerInvWrapper(player.getInventory()), what::matches, maxAmount).getCount();
return TransferHelper.extractMatching(new PlayerInvWrapper(player.getInventory()), what::matches, maxAmount, false).getCount();
}

private class ScreenHandlerFactory implements IPipeMenuProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import aztech.modern_industrialization.MI;
import java.util.function.Predicate;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.ItemHandlerHelper;

Expand Down Expand Up @@ -67,7 +68,7 @@ public static void moveAll(IItemHandler src, IItemHandler target, boolean stackI
}
}

public static ItemStack extractMatching(IItemHandler src, Predicate<ItemStack> predicate, int maxAmount) {
public static ItemStack extractMatching(IItemHandler src, Predicate<ItemStack> predicate, int maxAmount, boolean recursive) {
int srcSlots = src.getSlots();

// Find first stack
Expand All @@ -77,19 +78,32 @@ public static ItemStack extractMatching(IItemHandler src, Predicate<ItemStack> p
var stack = src.getStackInSlot(slot);
if (predicate.test(stack)) {
ret = src.extractItem(slot, maxAmount, false);
} else if (recursive) {
var capability = stack.getCapability(Capabilities.ItemHandler.ITEM);
if (capability != null) {
ret = extractMatching(capability, predicate, maxAmount, false);
}
}
}
if (ret.isEmpty()) {
return ItemStack.EMPTY;
}
final ItemStack finalRet = ret;

// Try to extract more
++slot;
for (; slot < srcSlots && maxAmount < ret.getCount(); ++slot) {
var stack = src.getStackInSlot(slot);
if (ItemStack.matches(stack, ret)) {
if (ItemStack.isSameItemSameComponents(stack, ret)) {
var extracted = src.extractItem(slot, maxAmount - ret.getCount(), true);
ret.grow(extracted.getCount());
} else if (recursive) {
var capability = stack.getCapability(Capabilities.ItemHandler.ITEM);
if (capability != null) {
var extracted = extractMatching(capability, s -> ItemStack.isSameItemSameComponents(s, finalRet), maxAmount - ret.getCount(),
false);
ret.grow(extracted.getCount());
}
}
}

Expand Down
Loading