2018-12-20 00:39:10 +00:00
|
|
|
package net.shadowfacts.simplemultipart.item;
|
|
|
|
|
|
|
|
import net.minecraft.block.BlockState;
|
2018-12-28 18:13:51 +00:00
|
|
|
import net.minecraft.block.Blocks;
|
2018-12-20 00:39:10 +00:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemUsageContext;
|
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.shadowfacts.simplemultipart.SimpleMultipart;
|
2018-12-28 18:13:51 +00:00
|
|
|
import net.shadowfacts.simplemultipart.container.AbstractContainerBlockEntity;
|
2018-12-28 02:55:51 +00:00
|
|
|
import net.shadowfacts.simplemultipart.container.MultipartContainer;
|
2018-12-20 00:39:10 +00:00
|
|
|
import net.shadowfacts.simplemultipart.multipart.Multipart;
|
|
|
|
import net.shadowfacts.simplemultipart.multipart.MultipartState;
|
2018-12-24 22:16:38 +00:00
|
|
|
import net.shadowfacts.simplemultipart.util.MultipartPlacementContext;
|
2018-12-20 00:39:10 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-28 18:10:59 +00:00
|
|
|
* An {@link Item} implementation that handles placing multiparts.
|
|
|
|
*
|
|
|
|
* Analogous to {@link net.minecraft.item.block.BlockItem}.
|
|
|
|
*
|
2018-12-20 00:39:10 +00:00
|
|
|
* @author shadowfacts
|
2018-12-28 18:10:59 +00:00
|
|
|
* @since 0.1.0
|
2018-12-20 00:39:10 +00:00
|
|
|
*/
|
|
|
|
public class ItemMultipart extends Item {
|
|
|
|
|
|
|
|
protected Multipart part;
|
|
|
|
|
|
|
|
public ItemMultipart(Multipart part) {
|
2018-12-28 18:10:59 +00:00
|
|
|
// TODO: expose settings object?
|
2018-12-20 00:39:10 +00:00
|
|
|
super(new Settings());
|
|
|
|
this.part = part;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ActionResult useOnBlock(ItemUsageContext context) {
|
2018-12-24 23:25:28 +00:00
|
|
|
return tryPlace(context);
|
2018-12-20 00:39:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 18:10:59 +00:00
|
|
|
/**
|
|
|
|
* Attempts to place this item's multipart in the given context.
|
|
|
|
*
|
|
|
|
* If the player clicked an existing multipart container, it will attempt to insert into that one, falling back on
|
|
|
|
* creating a new container.
|
|
|
|
*
|
|
|
|
* @param context The item usage context.
|
|
|
|
* @return The result of the placement.
|
|
|
|
*/
|
2018-12-24 23:25:28 +00:00
|
|
|
protected ActionResult tryPlace(ItemUsageContext context) {
|
|
|
|
// If a multipart inside an existing container was clicked, try inserting into that
|
2018-12-25 15:30:46 +00:00
|
|
|
MultipartContainer hitContainer = getContainer(context);
|
2018-12-24 23:25:28 +00:00
|
|
|
if (hitContainer != null && tryPlace(new MultipartPlacementContext(hitContainer, context))) {
|
|
|
|
return ActionResult.SUCCESS;
|
2018-12-20 00:39:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 23:25:28 +00:00
|
|
|
// 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());
|
2018-12-25 15:30:46 +00:00
|
|
|
MultipartContainer offsetContainer = getOrCreateContainer(offsetContext);
|
2018-12-28 18:13:51 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2018-12-20 00:39:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 23:25:28 +00:00
|
|
|
return ActionResult.FAILURE;
|
2018-12-20 00:39:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-25 15:30:46 +00:00
|
|
|
protected MultipartContainer getContainer(ItemUsageContext context) {
|
2018-12-24 23:25:28 +00:00
|
|
|
BlockState state = context.getWorld().getBlockState(context.getPos());
|
|
|
|
if (state.getBlock() == SimpleMultipart.containerBlock) {
|
2018-12-25 15:30:46 +00:00
|
|
|
return (MultipartContainer)context.getWorld().getBlockEntity(context.getPos());
|
2018-12-20 00:39:10 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-25 15:30:46 +00:00
|
|
|
protected MultipartContainer getOrCreateContainer(ItemUsageContext context) {
|
|
|
|
MultipartContainer container = getContainer(context);
|
2018-12-24 23:25:28 +00:00
|
|
|
if (container == null) {
|
|
|
|
BlockState existing = context.getWorld().getBlockState(context.getPos());
|
|
|
|
if (existing.isAir()) { // TODO: should check is replaceable (might not be mapped?)
|
|
|
|
context.getWorld().setBlockState(context.getPos(), SimpleMultipart.containerBlock.getDefaultState());
|
2018-12-25 15:30:46 +00:00
|
|
|
container = (MultipartContainer)context.getWorld().getBlockEntity(context.getPos());
|
2018-12-24 23:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:10:59 +00:00
|
|
|
/**
|
|
|
|
* Attempts to insert this item's multipart into the container specified by the given context.
|
|
|
|
*
|
|
|
|
* @param context The multipart placement context.
|
|
|
|
* @return If the placement succeeded.
|
|
|
|
*/
|
2018-12-24 23:25:28 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-12-20 00:39:10 +00:00
|
|
|
|
|
|
|
}
|