Destroy containers that are created unnecessarily
This commit is contained in:
parent
ab3b81eac9
commit
3fa9b5c1ee
|
@ -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);
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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))) {
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue