Add multipart placement context isOffset

This commit is contained in:
Shadowfacts 2019-01-06 10:25:08 -05:00
parent 5173175668
commit b601cdb516
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 15 additions and 5 deletions

View File

@ -55,6 +55,7 @@ public class MultipartItem extends Item {
* *
* If the player clicked an existing multipart container, it will attempt to insert into that one, falling back on * If the player clicked an existing multipart container, it will attempt to insert into that one, falling back on
* creating a new container. * creating a new container.
* Which container the placement is being attempted in can be determined from {@link MultipartPlacementContext#isOffset()}.
* *
* @param context The item usage context. * @param context The item usage context.
* @return The result of the placement. * @return The result of the placement.
@ -62,7 +63,7 @@ public class MultipartItem extends Item {
protected ActionResult tryPlace(ItemUsageContext context) { protected ActionResult tryPlace(ItemUsageContext context) {
// If a multipart inside an existing container was clicked, try inserting into that // If a multipart inside an existing container was clicked, try inserting into that
MultipartContainer hitContainer = getContainer(context); MultipartContainer hitContainer = getContainer(context);
if (hitContainer != null && tryPlace(new MultipartPlacementContext(hitContainer, context))) { if (hitContainer != null && tryPlace(new MultipartPlacementContext(hitContainer, false, context))) {
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} }
@ -70,7 +71,7 @@ public class MultipartItem extends Item {
ItemUsageContext offsetContext = new ItemUsageContext(context.getPlayer(), context.getItemStack(), context.getPos().offset(context.getFacing()), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ()); 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); MultipartContainer offsetContainer = getOrCreateContainer(offsetContext);
if (offsetContainer != null) { if (offsetContainer != null) {
if (tryPlace(new MultipartPlacementContext(offsetContainer, offsetContext))) { if (tryPlace(new MultipartPlacementContext(offsetContainer, true, offsetContext))) {
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} else { } else {
// if the a new container was created, and no part was inserted, remove the empty container // if the a new container was created, and no part was inserted, remove the empty container

View File

@ -18,14 +18,16 @@ import net.shadowfacts.simplemultipart.container.MultipartContainer;
public class MultipartPlacementContext extends ItemUsageContext { public class MultipartPlacementContext extends ItemUsageContext {
private final MultipartContainer container; private final MultipartContainer container;
private final boolean isOffset;
public MultipartPlacementContext(MultipartContainer container, PlayerEntity player, ItemStack stack, BlockPos pos, Direction side, float hitX, float hitY, float hitZ) { public MultipartPlacementContext(MultipartContainer container, boolean isOffset, PlayerEntity player, ItemStack stack, BlockPos pos, Direction side, float hitX, float hitY, float hitZ) {
super(player, stack, pos, side, hitX, hitY, hitZ); super(player, stack, pos, side, hitX, hitY, hitZ);
this.container = container; this.container = container;
this.isOffset = isOffset;
} }
public MultipartPlacementContext(MultipartContainer container, ItemUsageContext context) { public MultipartPlacementContext(MultipartContainer container, boolean isOffset, ItemUsageContext context) {
this(container, context.getPlayer(), context.getItemStack(), context.getPos(), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ()); this(container, isOffset, context.getPlayer(), context.getItemStack(), context.getPos(), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ());
} }
/** /**
@ -35,4 +37,11 @@ public class MultipartPlacementContext extends ItemUsageContext {
return container; return container;
} }
/**
* @return {@code false} if this container is the one clicked, {@code true} if this container is a newly created one offset from the block clicked.
* @since 0.1.2
*/
public boolean isOffset() {
return isOffset;
}
} }