Rewrite multipart placement logic
This commit is contained in:
parent
dd479376fd
commit
3cce66f53e
|
@ -2,7 +2,6 @@ package net.shadowfacts.simplemultipart.item;
|
||||||
|
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemPlacementContext;
|
|
||||||
import net.minecraft.item.ItemUsageContext;
|
import net.minecraft.item.ItemUsageContext;
|
||||||
import net.minecraft.util.ActionResult;
|
import net.minecraft.util.ActionResult;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
@ -27,67 +26,56 @@ public class ItemMultipart extends Item {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult useOnBlock(ItemUsageContext context) {
|
public ActionResult useOnBlock(ItemUsageContext context) {
|
||||||
return place(new ItemPlacementContext(context));
|
return tryPlace(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ActionResult place(ItemPlacementContext context) {
|
protected ActionResult tryPlace(ItemUsageContext context) {
|
||||||
MultipartContainerBlockEntity container = getOrCreateContainer(context.getWorld(), context.getPos());
|
// If a multipart inside an existing container was clicked, try inserting into that
|
||||||
if (container == null) {
|
MultipartContainerBlockEntity hitContainer = getContainer(context);
|
||||||
return ActionResult.FAILURE;
|
if (hitContainer != null && tryPlace(new MultipartPlacementContext(hitContainer, context))) {
|
||||||
}
|
|
||||||
|
|
||||||
MultipartPlacementContext partContext = new MultipartPlacementContext(container, context);
|
|
||||||
MultipartState state = part.getPlacementState(partContext);
|
|
||||||
|
|
||||||
if (!container.canInsert(state)) {
|
|
||||||
// container.destroyIfEmpty();
|
|
||||||
return ActionResult.FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
container.insert(state);
|
|
||||||
|
|
||||||
context.getItemStack().addAmount(-1);
|
|
||||||
|
|
||||||
return ActionResult.SUCCESS;
|
return ActionResult.SUCCESS;
|
||||||
|
|
||||||
// MultipartSlot slot = getSlotForPlacement(container, context);
|
|
||||||
// if (slot == null) {
|
|
||||||
// return ActionResult.FAILURE;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// MultipartState partState = part.getPlacementState(slot, container);
|
|
||||||
// if (!container.canInsert(partState, slot)) {
|
|
||||||
// return ActionResult.FAILURE;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// container.insert(partState, slot);
|
|
||||||
//
|
|
||||||
// context.getItemStack().addAmount(-1);
|
|
||||||
//
|
|
||||||
// return ActionResult.SUCCESS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected MultipartContainerBlockEntity getOrCreateContainer(World world, BlockPos pos) {
|
// Otherwise, get or create a new container and try inserting into that
|
||||||
BlockState current = world.getBlockState(pos);
|
ItemUsageContext offsetContext = new ItemUsageContext(context.getPlayer(), context.getItemStack(), context.getPos().offset(context.getFacing()), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ());
|
||||||
if (current.getBlock() == SimpleMultipart.containerBlock) {
|
MultipartContainerBlockEntity offsetContainer = getOrCreateContainer(offsetContext);
|
||||||
return (MultipartContainerBlockEntity)world.getBlockEntity(pos);
|
if (offsetContainer != null && tryPlace(new MultipartPlacementContext(offsetContainer, offsetContext))) {
|
||||||
} else if (current.isAir()) {
|
return ActionResult.SUCCESS;
|
||||||
world.setBlockState(pos, SimpleMultipart.containerBlock.getDefaultState());
|
}
|
||||||
return (MultipartContainerBlockEntity)world.getBlockEntity(pos);
|
|
||||||
|
return ActionResult.FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected MultipartContainerBlockEntity getContainer(ItemUsageContext context) {
|
||||||
|
BlockState state = context.getWorld().getBlockState(context.getPos());
|
||||||
|
if (state.getBlock() == SimpleMultipart.containerBlock) {
|
||||||
|
return (MultipartContainerBlockEntity)context.getWorld().getBlockEntity(context.getPos());
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// protected MultipartSlot getSlotForPlacement(MultipartContainerBlockEntity container, ItemPlacementContext context) {
|
protected MultipartContainerBlockEntity getOrCreateContainer(ItemUsageContext context) {
|
||||||
// MultipartSlot slot = MultipartSlot.fromClickedSide(context.getFacing());
|
MultipartContainerBlockEntity container = getContainer(context);
|
||||||
// if (part.isValidSlot(slot) && !container.hasPartInSlot(slot)) {
|
if (container == null) {
|
||||||
// return slot;
|
BlockState existing = context.getWorld().getBlockState(context.getPos());
|
||||||
// }
|
if (existing.isAir()) { // TODO: should check is replaceable (might not be mapped?)
|
||||||
// if (part.isValidSlot(MultipartSlot.CENTER) && !container.hasPartInSlot(MultipartSlot.CENTER)) {
|
context.getWorld().setBlockState(context.getPos(), SimpleMultipart.containerBlock.getDefaultState());
|
||||||
// return MultipartSlot.CENTER;
|
container = (MultipartContainerBlockEntity)context.getWorld().getBlockEntity(context.getPos());
|
||||||
// }
|
}
|
||||||
// return null;
|
}
|
||||||
// }
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean tryPlace(MultipartPlacementContext context) {
|
||||||
|
MultipartState placementState = part.getPlacementState(context);
|
||||||
|
|
||||||
|
if (context.getContainer().canInsert(placementState)) {
|
||||||
|
context.getContainer().insert(placementState);
|
||||||
|
context.getItemStack().addAmount(-1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package net.shadowfacts.simplemultipart.util;
|
package net.shadowfacts.simplemultipart.util;
|
||||||
|
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.ItemPlacementContext;
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.item.ItemUsageContext;
|
import net.minecraft.item.ItemUsageContext;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
@ -20,7 +19,7 @@ public class MultipartPlacementContext extends ItemUsageContext {
|
||||||
this.container = container;
|
this.container = container;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MultipartPlacementContext(MultipartContainerBlockEntity container, ItemPlacementContext context) {
|
public MultipartPlacementContext(MultipartContainerBlockEntity container, ItemUsageContext context) {
|
||||||
this(container, context.getPlayer(), context.getItemStack(), context.getPos(), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ());
|
this(container, context.getPlayer(), context.getItemStack(), context.getPos(), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue