Destroy containers that are created unnecessarily

This commit is contained in:
Shadowfacts 2018-12-28 13:13:51 -05:00
parent ab3b81eac9
commit 3fa9b5c1ee
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 21 additions and 2 deletions

View File

@ -45,6 +45,11 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
return ImmutableSet.copyOf(parts);
}
@Override
public boolean hasParts() {
return !parts.isEmpty();
}
@Override
public boolean canInsert(MultipartState partState) {
VoxelShape newShape = partState.getBoundingShape(null);

View File

@ -22,6 +22,11 @@ public interface MultipartContainer {
*/
Set<MultipartView> getParts();
/**
* @return If this container has any parts in it.
*/
boolean hasParts();
/**
* Determines whether the given multipart state can be inserted into this container.
* Checks that the bounding box of the new part does not intersect with any existing ones.

View File

@ -1,10 +1,12 @@
package net.shadowfacts.simplemultipart.item;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.util.ActionResult;
import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.container.AbstractContainerBlockEntity;
import net.shadowfacts.simplemultipart.container.MultipartContainer;
import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
@ -52,8 +54,15 @@ public class ItemMultipart extends Item {
// Otherwise, get or create a new container and try inserting into that
ItemUsageContext offsetContext = new ItemUsageContext(context.getPlayer(), context.getItemStack(), context.getPos().offset(context.getFacing()), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ());
MultipartContainer offsetContainer = getOrCreateContainer(offsetContext);
if (offsetContainer != null && tryPlace(new MultipartPlacementContext(offsetContainer, offsetContext))) {
return ActionResult.SUCCESS;
if (offsetContainer != null) {
if (tryPlace(new MultipartPlacementContext(offsetContainer, offsetContext))) {
return ActionResult.SUCCESS;
} else {
// if the a new container was created, and no part was inserted, remove the empty container
if (!offsetContainer.hasParts()) {
context.getWorld().setBlockState(offsetContext.getPos(), Blocks.AIR.getDefaultState());
}
}
}
return ActionResult.FAILURE;