use of buildcraft.lib.inventory.InventoryWrapper in project BuildCraft by BuildCraft.
the class ItemSchematicSingle method onItemUseFirst.
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
if (world.isRemote) {
return EnumActionResult.PASS;
}
ItemStack stack = player.getHeldItem(hand);
if (player.isSneaking()) {
NBTTagCompound itemData = NBTUtilBC.getItemData(StackUtil.asNonNull(stack));
itemData.removeTag(NBT_KEY);
if (itemData.hasNoTags()) {
stack.setTagCompound(null);
}
stack.setItemDamage(DAMAGE_CLEAN);
return EnumActionResult.SUCCESS;
}
int damage = stack.getItemDamage();
if (damage != DAMAGE_USED) {
IBlockState state = world.getBlockState(pos);
ISchematicBlock schematicBlock = SchematicBlockManager.getSchematicBlock(new SchematicBlockContext(world, pos, pos, state, state.getBlock()));
if (schematicBlock.isAir()) {
return EnumActionResult.FAIL;
}
NBTUtilBC.getItemData(stack).setTag(NBT_KEY, SchematicBlockManager.writeToNBT(schematicBlock));
stack.setItemDamage(DAMAGE_USED);
return EnumActionResult.SUCCESS;
} else {
BlockPos placePos = pos;
boolean replaceable = world.getBlockState(pos).getBlock().isReplaceable(world, pos);
if (!replaceable) {
placePos = placePos.offset(side);
}
if (!world.mayPlace(world.getBlockState(pos).getBlock(), placePos, false, side, null)) {
return EnumActionResult.FAIL;
}
if (replaceable && !world.isAirBlock(placePos)) {
world.setBlockToAir(placePos);
}
try {
ISchematicBlock schematicBlock = getSchematic(stack);
if (schematicBlock != null) {
if (!schematicBlock.isBuilt(world, placePos) && schematicBlock.canBuild(world, placePos)) {
List<FluidStack> requiredFluids = schematicBlock.computeRequiredFluids();
List<ItemStack> requiredItems = schematicBlock.computeRequiredItems();
if (requiredFluids.isEmpty()) {
InventoryWrapper itemTransactor = new InventoryWrapper(player.inventory);
if (StackUtil.mergeSameItems(requiredItems).stream().noneMatch(s -> itemTransactor.extract(extracted -> StackUtil.canMerge(s, extracted), s.getCount(), s.getCount(), true).isEmpty())) {
if (schematicBlock.build(world, placePos)) {
StackUtil.mergeSameItems(requiredItems).forEach(s -> itemTransactor.extract(extracted -> StackUtil.canMerge(s, extracted), s.getCount(), s.getCount(), false));
SoundUtil.playBlockPlace(world, placePos);
player.swingArm(hand);
return EnumActionResult.SUCCESS;
}
} else {
player.sendStatusMessage(new TextComponentString("Not enough items. Total needed: " + StackUtil.mergeSameItems(requiredItems).stream().map(s -> s.getTextComponent().getFormattedText() + " x " + s.getCount()).collect(Collectors.joining(", "))), true);
}
} else {
player.sendStatusMessage(new TextComponentString("Schematic requires fluids"), true);
}
}
}
} catch (InvalidInputDataException e) {
player.sendStatusMessage(new TextComponentString("Invalid schematic: " + e.getMessage()), true);
e.printStackTrace();
}
return EnumActionResult.FAIL;
}
}
Aggregations