2018-12-20 00:39:10 +00:00
|
|
|
package net.shadowfacts.simplemultipart.item;
|
|
|
|
|
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemPlacementContext;
|
|
|
|
import net.minecraft.item.ItemUsageContext;
|
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.shadowfacts.simplemultipart.SimpleMultipart;
|
|
|
|
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockEntity;
|
|
|
|
import net.shadowfacts.simplemultipart.multipart.Multipart;
|
|
|
|
import net.shadowfacts.simplemultipart.multipart.MultipartSlot;
|
|
|
|
import net.shadowfacts.simplemultipart.multipart.MultipartState;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
public class ItemMultipart extends Item {
|
|
|
|
|
|
|
|
protected Multipart part;
|
|
|
|
|
|
|
|
public ItemMultipart(Multipart part) {
|
|
|
|
super(new Settings());
|
|
|
|
this.part = part;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ActionResult useOnBlock(ItemUsageContext context) {
|
|
|
|
return place(new ItemPlacementContext(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ActionResult place(ItemPlacementContext context) {
|
|
|
|
MultipartContainerBlockEntity container = getOrCreateContainer(context.getWorld(), context.getPos());
|
|
|
|
if (container == null) {
|
|
|
|
return ActionResult.FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MultipartSlot slot = getSlotForPlacement(container, context);
|
|
|
|
if (slot == null) {
|
|
|
|
return ActionResult.FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-12-20 01:33:20 +00:00
|
|
|
MultipartState partState = part.getPlacementState(slot, container);
|
2018-12-20 00:39:10 +00:00
|
|
|
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) {
|
|
|
|
BlockState current = world.getBlockState(pos);
|
|
|
|
if (current.getBlock() == SimpleMultipart.containerBlock) {
|
|
|
|
return (MultipartContainerBlockEntity)world.getBlockEntity(pos);
|
|
|
|
} else if (current.isAir()) {
|
|
|
|
world.setBlockState(pos, SimpleMultipart.containerBlock.getDefaultState());
|
|
|
|
return (MultipartContainerBlockEntity)world.getBlockEntity(pos);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected MultipartSlot getSlotForPlacement(MultipartContainerBlockEntity container, ItemPlacementContext context) {
|
|
|
|
MultipartSlot slot = MultipartSlot.fromClickedSide(context.getFacing());
|
|
|
|
if (part.isValidSlot(slot) && !container.hasPartInSlot(slot)) {
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
if (part.isValidSlot(MultipartSlot.CENTER) && !container.hasPartInSlot(MultipartSlot.CENTER)) {
|
|
|
|
return MultipartSlot.CENTER;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|