From b601cdb516ad4ff12ced2d2167b7a7da7a2113c7 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 6 Jan 2019 10:25:08 -0500 Subject: [PATCH] Add multipart placement context isOffset --- .../simplemultipart/item/MultipartItem.java | 5 +++-- .../util/MultipartPlacementContext.java | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/shadowfacts/simplemultipart/item/MultipartItem.java b/src/main/java/net/shadowfacts/simplemultipart/item/MultipartItem.java index 2f26ae7..e354585 100644 --- a/src/main/java/net/shadowfacts/simplemultipart/item/MultipartItem.java +++ b/src/main/java/net/shadowfacts/simplemultipart/item/MultipartItem.java @@ -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 * 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. * @return The result of the placement. @@ -62,7 +63,7 @@ public class MultipartItem extends Item { protected ActionResult tryPlace(ItemUsageContext context) { // If a multipart inside an existing container was clicked, try inserting into that MultipartContainer hitContainer = getContainer(context); - if (hitContainer != null && tryPlace(new MultipartPlacementContext(hitContainer, context))) { + if (hitContainer != null && tryPlace(new MultipartPlacementContext(hitContainer, false, context))) { 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()); MultipartContainer offsetContainer = getOrCreateContainer(offsetContext); if (offsetContainer != null) { - if (tryPlace(new MultipartPlacementContext(offsetContainer, offsetContext))) { + if (tryPlace(new MultipartPlacementContext(offsetContainer, true, offsetContext))) { return ActionResult.SUCCESS; } else { // if the a new container was created, and no part was inserted, remove the empty container diff --git a/src/main/java/net/shadowfacts/simplemultipart/util/MultipartPlacementContext.java b/src/main/java/net/shadowfacts/simplemultipart/util/MultipartPlacementContext.java index eedc4c3..7e1fb9b 100644 --- a/src/main/java/net/shadowfacts/simplemultipart/util/MultipartPlacementContext.java +++ b/src/main/java/net/shadowfacts/simplemultipart/util/MultipartPlacementContext.java @@ -18,14 +18,16 @@ import net.shadowfacts.simplemultipart.container.MultipartContainer; public class MultipartPlacementContext extends ItemUsageContext { 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); this.container = container; + this.isOffset = isOffset; } - public MultipartPlacementContext(MultipartContainer container, ItemUsageContext context) { - this(container, context.getPlayer(), context.getItemStack(), context.getPos(), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ()); + public MultipartPlacementContext(MultipartContainer container, boolean isOffset, ItemUsageContext context) { + 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 {@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; + } }