Skip to content

Commit

Permalink
Add wrench right-click to clear stuffed attachments (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n authored Mar 8, 2024
1 parent 241dcdf commit 6fd5e69
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public List<ItemStack> getDrops() {
return List.of(new ItemStack(item));
}

/**
* Try to clear contents of the attachment,
* for example when the player right-clicks with a wrench.
*
* @return {@code true} if there was something to clear
*/
public boolean tryClearContents(PipeBlockEntity pipe) {
return false;
}

public Component getDisplayName() {
return item.getDescription();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ public List<ItemStack> getDrops() {
return drops;
}

@Override
public boolean tryClearContents(PipeBlockEntity pipe) {
if (isStuffed()) {
List<ItemStack> drops = new ArrayList<>();
for (var entry : stuffedItems.entrySet()) {
DropHelper.splitIntoStacks(entry.getKey(), entry.getValue(), drops::add);
}
stuffedItems.clear();
DropHelper.dropStacks(pipe, drops);
pipe.setChanged();
pipe.sync();
return true;
}
return super.tryClearContents(pipe);
}

public boolean isStuffed() {
return !stuffedItems.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,21 @@ public InteractionResult onUse(Player player, InteractionHand hand, BlockHitResu
if (ShapeHelper.shapeContains(PipeBoundingBoxes.INVENTORY_CONNECTIONS[i], posInBlock)) {
var side = Direction.from3DDataValue(i);
if (hasAttachment(side)) {
// Remove attachment
if (level.isClientSide()) {
// We will either remove the attachment or clear out its stuffed items.
// In any case, for the client it's a success.
if (isClientSide()) {
return InteractionResult.SUCCESS;
} else {
for (var host : getHosts()) {
var attachment = host.removeAttachment(side);
if (attachment != null) {
}

for (var host : getHosts()) {
var attachment = host.getAttachment(side);
if (attachment != null) {
// Try to clear contents
if (attachment.tryClearContents(this)) {
return InteractionResult.CONSUME;
} else {
// Remove attachment
host.removeAttachment(side);
if (!player.isCreative()) {
DropHelper.dropStacks(this, attachment.getDrops());
}
Expand Down

0 comments on commit 6fd5e69

Please sign in to comment.